-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathrender_helper.js
98 lines (80 loc) · 2.7 KB
/
render_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*!
* nodeclub - common/render_helpers.js
* Copyright(c) 2013 fengmk2 <[email protected]>
* MIT Licensed
*/
"use strict";
/**
* Module dependencies.
*/
var MarkdownIt = require('markdown-it');
var _ = require('lodash');
var config = require('../config');
var validator = require('validator');
var multiline = require('multiline');
var jsxss = require('xss');
// Set default options
var md = new MarkdownIt();
md.set({
html: true, // Enable HTML tags in source
xhtmlOut: false, // Use '/' to close single tags (<br />)
breaks: false, // Convert '\n' in paragraphs into <br>
linkify: true, // Autoconvert URL-like text to links
typographer: true, // Enable smartypants and other sweet transforms
});
md.renderer.rules.fence = function (tokens, idx) {
var token = tokens[idx];
var language = token.params && ('language-' + token.params) || '';
language = validator.escape(language);
return '<pre class="prettyprint ' + language + '">'
+ '<code>' + validator.escape(token.content) + '</code>'
+ '</pre>';
};
md.renderer.rules.code_block = function (tokens, idx /*, options*/) {
var token = tokens[idx];
var language = token.params && ('language-' + token.params) || '';
language = validator.escape(language);
return '<pre class="prettyprint ' + language + '">'
+ '<code>' + validator.escape(token.content) + '</code>'
+ '</pre>';
};
md.renderer.rules.code_inline = function (tokens, idx /*, options*/) {
return '<code>' + validator.escape(tokens[idx].content) + '</code>';
};
var myxss = new jsxss.FilterXSS({
onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) {
// 让 prettyprint 可以工作
if (tag === 'pre' && name === 'class') {
return name + '="' + jsxss.escapeAttrValue(value) + '"';
}
}
});
exports.markdown = function (text) {
return '<div class="markdown-text">' + myxss.process(md.render(text || '')) + '</div>';
};
exports.multiline = multiline;
exports.escapeSignature = function (signature) {
return signature.split(/\r\n|\n/).map(function (p) {
return _.escape(p);
}).join('<br>');
};
exports.staticFile = function (filePath) {
if (filePath.indexOf('http') === 0 || filePath.indexOf('//') === 0) {
return filePath;
}
return config.site_static_host + filePath;
};
exports.tabName = function (tab) {
var pair = _.find(config.tabs, function (pair) {
return pair[0] === tab;
});
if (pair) {
return pair[1];
}
};
exports.proxy = function (url) {
return url;
// 当 google 和 github 封锁严重时,则需要通过服务器代理访问它们的静态资源
// return '/agent?url=' + encodeURIComponent(url);
};
exports._ = _;