forked from GitbookIO/plugin-highlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (118 loc) · 3.18 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const hljs = require('highlight.js')
const MAP = {
'py': 'python',
'js': 'javascript',
'json': 'javascript',
'rb': 'ruby',
'csharp': 'cs'
}
function normalize (lang) {
if (!lang) { return null }
const lower = lang.toLowerCase()
return MAP[lower] || lower
}
function highlight (lang, code) {
if (!lang) {
return {
body: code,
html: false
}
}
// Normalize lang
lang = normalize(lang)
if (lang === 'diff') {
const diff2html = require('diff2html').Diff2Html
let element = diff2html.getPrettyHtmlFromDiff(code)
// console.log(element)
const diff = highlightCode(element)
// console.log(diff)
return diff
}
try {
return hljs.highlight(lang, code).value
} catch (e) { }
return {
body: code,
html: false
}
}
function highlightCode (target) {
// var that = this
var highlightJS = require('diff2html/src/ui/js/highlight.js-internals.js').HighlightJS
let cheerio = require('cheerio')
// var $target = cheerio.load(target)
let $ = cheerio.load(target)
// collect all the diff files and execute the highlight on their lines
var $files = $('.d2h-file-wrapper')
$files.map(function (_i, file) {
var oldLinesState
var newLinesState
var $file = $(file)
var language = $file.data('lang')
// collect all the code lines and execute the highlight on them
var $codeLines = $file.find('.d2h-code-line-ctn')
$codeLines.map(function (_j, line) {
var $line = $(line)
var text = $line.text()
var lineParent = $line.parent()
var lineState
if ($(lineParent).hasClass('d2h-del')) {
lineState = oldLinesState
} else {
lineState = newLinesState
}
var result = hljs.getLanguage(language) ? hljs.highlight(language, text, true, lineState) : hljs.highlightAuto(text)
if ($(lineParent).hasClass('d2h-del')) {
oldLinesState = result.top
} else if ($(lineParent).hasClass('d2h-ins')) {
newLinesState = result.top
} else {
oldLinesState = result.top
newLinesState = result.top
}
var originalStream = highlightJS.nodeStream(line)
if (originalStream.length) {
var resultNode = document.createElementNS('http://www.w3.org/1999/xhtml', 'div')
resultNode.innerHTML = result.value
result.value = highlightJS.mergeStreams(originalStream, highlightJS.nodeStream(resultNode), text)
}
// $line.addClass('hljs')
// $line.addClass(result.language)
$line.html(result.value)
})
})
return $.html()
};
module.exports = {
book: {
assets: './css',
css: [
'website.css',
'diff2html.css'
]
},
ebook: {
assets: './css',
css: [
'ebook.css',
'diff2html.css'
]
},
blocks: {
code: function (block) {
return highlight(block.kwargs.language, block.body)
}
},
hooks: {
page: function (page) {
const cheerio = require('cheerio')
const $ = cheerio.load(page.content)
$('pre code.lang-diff').each(function () {
const diff = highlight('diff', $(this).text())
$(this).parent().replaceWith(diff)
})
page.content = $.html()
return page
}
}
}