-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (66 loc) · 2.1 KB
/
index.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
var markdown = require('markdown').markdown;
var hljs = require('highlight.js');
function markdownAndHighlight(text){
var html = markdown.toHTML( text);
return html.replace(/(<code(?:\s+\w+[^>]*)*?>)([\s\S]*?)(<\/code>)/g,function(a,prefix,content,postfix){
content = content.replace(/&(lt|gt|amp|quot|#\d+);/g,function(a,c){
if(c == 'lt')return '<';
if(c == 'gt')return '>';
if(c == 'amp')return '&';
if(c=='quot')return '"';
return String.fromCharCode(c.substring(1))
}).replace(/<\!\[CDATA\[([\s\S]*?)\]\s*\]>/g,'<![CDATA[$1]]>')
//return prefix + hljs.highlight('java',content).value+postfix;
return prefix + hljs.highlightAuto(content).value+postfix;
//return prefix + hljs.highlightAuto(content,['java','javascript','xml']).value+postfix;
}).replace(/(<\/?h\d>\s*)?<a\s+name="([\w\-_]+)"\s*(?:\/>|>\s*<\/a>)(\s*<\/?h\d>)?/g,function(a,h1,id,h2){
if (h1|| h2){
return (h1||'')+'<a name=\"'+id+'"></a>'+(h2||'');
}else{
return a;
}
})
}
function seekMarkdown(text){
var html = markdownAndHighlight( text);
this.append(html)
return text.length;
}
function parseCode(node){
try{
var text = String(node.textContent || node.text);
text = text.replace(/^\s*[\r\n]+|[\r\n]+\s*$/g,'');
text = text.split(/\r\n?|\n/);
var len = text.length;
text = text.join('\n');
while(true){
var text2 = text.replace(/^[\t ]/mg,'');
if(text.length - text2.length != len){
break;
}
text = text2;
}
var html = hljs.highlightAuto(text).value;
this.append("<pre><code>" + html+"</code></pre>");
}catch(e){
console.log(e);
throw e;
}
}
function parseMarkdown(node){
try{
var html = markdownAndHighlight(node.textContent);
this.append(html);
}catch(e){
console.log(e);
throw e;
}
}
exports.markdownAndHighlight = markdownAndHighlight;
exports.parseCode = parseCode;
exports.parseMarkdown = parseMarkdown;
exports.seekMarkdown = seekMarkdown;
exports.start = function(root){
//console.log('## start!')
require('./server').start(root);
};