-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (71 loc) · 2.45 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
const fs = require('fs')
const marked = require('marked')
const sourcePath = 'index.md'
const targetPath = 'index.html'
const hljsStyle = 'googlecode'
const hljsVersion = '9.12.0'
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: false,
smartLists: true,
smartypants: false
})
Object.prototype.pipe = function(fn) {
return fn(this)
}
const esc = s => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
const externalLink = '<i class="fas fa-xs fa-external-link-alt"></i>'
const html = fs
.readFileSync(sourcePath)
.toString()
.pipe(s => marked(String(s)))
.replace(/ id="a-[^"]*"/g, '')
.replace(/ +$/gm, '')
.replace(
/<a href="(http[^"]*)">(([^<]|<[^/]|<[/][^a])*)<[/]a>/g,
(match, href, content) =>
content.startsWith('<img ')
? `<a target="_blank" href="${href}">${content}</a>`
: `<a target="_blank" href="${href}">${content} ${externalLink}</a>`
)
.replace(/<code class="language-([a-z]*)">/g, '<code class="hljs lang-$1">')
.pipe(
s => `<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="github.css">
<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/${hljsVersion}/styles/${hljsStyle}.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
<meta charset="utf-8">
<title>C++ submodule manager</title>
</head>
<body class="markdown-body">
${s}
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/${hljsVersion}/highlight.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/${hljsVersion}/languages/bash.min.js"></script>
<script type="text/javascript" src="init-hljs.js"></script>
<script type="text/javascript" src="tooltips.js"></script>
</body>
</html>`
)
fs.writeFileSync(targetPath, html)
const idRE = /\bid\s*=\s*"([^"]+)"/g
const ids = new Map()
for (;;) {
const m = idRE.exec(html)
if (!m) break
if (ids.has(m[1])) console.warn(`Duplicate id '${m[1]}'`)
ids.set(m[1], 1)
}
const hhrefRE = /\bhref="#([^"]+)"/g
for (;;) {
const m = hhrefRE.exec(html)
if (!m) break
if (!ids.has(m[1]))
console.warn(`Target of internal link '${m[1]}' does not exist.`)
}