-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathhtml-to-doc.js
141 lines (123 loc) · 3.43 KB
/
html-to-doc.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
139
140
141
const { parentPort, workerData } = require('worker_threads')
// unified imports
const unified = require('unified')
const parse = require('rehype-parse')
const select = require('hast-util-select').select
const selectAll = require('hast-util-select').selectAll
const toText = require('hast-util-to-text')
const is = require('unist-util-is')
const toVfile = require('to-vfile')
const sectionHeaderTest = ({ tagName }) => ['h2', 'h3'].includes(tagName)
// Build search data for a html
function* scanDocuments({ path, url }) {
let vfile
try {
vfile = toVfile.readSync(path)
} catch (e) {
if (e.code !== 'ENOENT') {
console.error(`docusaurus-lunr-search:: unable to read file ${path}`)
console.error(e)
}
return
}
const hast = unified()
.use(parse, { emitParseErrors: false })
.parse(vfile)
const article = select('article', hast)
if (!article) {
return
}
const markdown = select('.markdown', article)
if (!markdown) {
return
}
const pageTitleElement = select('h1', article)
if (!pageTitleElement) {
return
}
const pageTitle = toText(pageTitleElement)
const sectionHeaders = getSectionHeaders(markdown)
const keywords = selectAll('meta[name="keywords"]', hast).reduce((acc, metaNode) => {
if (metaNode.properties.content) {
return acc.concat(metaNode.properties.content.replace(/,/g, ' '))
}
return acc
}, []).join(' ')
let version = null;
if (workerData.loadedVersions) {
const docsearchVersionElement = select('meta[name="docsearch:version"]', hast);
version = docsearchVersionElement
? workerData.loadedVersions[docsearchVersionElement.properties.content]
: null;
}
yield {
title: pageTitle,
type: 0,
sectionRef: '#',
url,
// If there is no sections then push the complete content under page title
content: sectionHeaders.length === 0 ? getContent(markdown) : '',
keywords,
version,
}
for (const sectionDesc of sectionHeaders) {
const { title, content, ref, tagName } = sectionDesc;
yield {
title,
type: 1,
pageTitle,
url: `${url}#${ref}`,
content,
version,
tagName
}
}
}
function getContent(element) {
return toText(element).replace(/\s\s+/g, ' ').replace(/(\r\n|\n|\r)/gm, ' ').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"')
}
function getSectionHeaders(markdown) {
let currentSection = null
const result = []
let contentsAcc = ''
const emitCurrent = () => {
const ref = select('.anchor', currentSection)
result.push({
title: toText(currentSection).replace(/^#+/, '').replace(/#$/, ''),
ref: ref ? ref.properties.id : '#',
tagName: currentSection.tagName || '#',
content: contentsAcc,
})
contentsAcc = ''
currentSection = null
}
for (const node of markdown.children) {
if (is(node, sectionHeaderTest)) {
if (currentSection) {
emitCurrent()
}
currentSection = node
} else if (currentSection) {
contentsAcc += getContent(node) + ' '
}
}
if (currentSection) {
emitCurrent()
}
return result
}
function processFile(file) {
let scanned = 0
for (const doc of scanDocuments(file)) {
scanned = 1
parentPort.postMessage([true, doc])
}
parentPort.postMessage([null, scanned])
}
parentPort.on('message', (maybeFile) => {
if (maybeFile) {
processFile(maybeFile)
} else {
parentPort.close()
}
})