Skip to content

Commit 8beb944

Browse files
committed
doc,tools: Replace client-side syntax highlighting for API docs compile compile-time syntax highlighting
1. The page will load much faster since there is much less JavaScript taking place. 2. Users with slow connections or JavaScript turned off will still see highlighted code. 3. I've changed the allhtml.js code to look for a special comment instead of the script tag for where to put the contents.
1 parent 1dc837e commit 8beb944

File tree

7 files changed

+28
-31
lines changed

7 files changed

+28
-31
lines changed

doc/api_assets/README.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
11
# API Reference Document Assets
22

3-
## highlight.pack.js
4-
5-
_Generated by [highlightjs.org/download][] on 2020-06-07._
6-
7-
Grammars included in the custom bundle:
8-
9-
* Bash
10-
* C
11-
* C++
12-
* CoffeeScript
13-
* HTTP
14-
* JavaScript
15-
* JSON
16-
* Markdown
17-
* Plaintext
18-
* Shell Session
19-
203
## hljs.css
214

225
The syntax theme for code snippets in API reference documents.
236

247
## style.css
258

269
The main stylesheet for API reference documents.
27-
28-
[highlightjs.org/download]: https://highlightjs.org/download/

doc/api_assets/highlight.pack.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

doc/template.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<head>
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width">
6+
<meta name="nodejs.org:node-version" content="__VERSION__">
67
<title>__SECTION__ | Node.js __VERSION__ Documentation</title>
78
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
89
<link rel="stylesheet" href="assets/style.css">
@@ -48,10 +49,9 @@ <h2>Table of Contents</h2>
4849

4950
<div id="apicontent">
5051
__CONTENT__
52+
<!-- API END -->
5153
</div>
5254
</div>
5355
</div>
54-
<script src="assets/highlight.pack.js"></script>
55-
<script>document.addEventListener('DOMContentLoaded', () => { hljs.initHighlightingOnLoad(); });</script>
5656
</body>
5757
</html>

tools/doc/allhtml.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ for (const link of toc.match(/<a.*?>/g)) {
3737
.replace(/[\s\S]*?<div id="toc">\s*<h2>.*?<\/h2>\s*(<ul>\s*)?/, '');
3838

3939
apicontent += data.slice(match.index + match[0].length)
40-
.replace(/(<\/div>\s*)*\s*<script[\s\S]*/, '')
40+
.replace(/<!-- API END -->[\s\S]*/, '')
4141
.replace(/<a href="(\w[^#"]*)#/g, (match, href) => {
4242
return htmlFiles.includes(href) ? '<a href="#' : match;
4343
})
@@ -66,10 +66,10 @@ all = all.slice(0, tocStart.index + tocStart[0].length) +
6666

6767
// Replace apicontent with the concatenated set of apicontents from each source.
6868
const apiStart = /<div id="apicontent">\s*/.exec(all);
69-
const apiEnd = /(\s*<\/div>)*\s*<script /.exec(all);
69+
const apiEnd = all.lastIndexOf('<!-- API END -->');
7070
all = all.slice(0, apiStart.index + apiStart[0].length) +
7171
apicontent +
72-
all.slice(apiEnd.index);
72+
all.slice(apiEnd);
7373

7474
// Write results.
7575
fs.writeFileSync(source + '/all.html', all, 'utf8');

tools/doc/html.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const raw = require('rehype-raw');
3232
const htmlStringify = require('rehype-stringify');
3333
const path = require('path');
3434
const typeParser = require('./type-parser.js');
35+
const { highlight, getLanguage } = require('highlightjs');
3536

3637
module.exports = {
3738
toHTML, firstHeader, preprocessText, preprocessElements, buildToc
@@ -183,6 +184,21 @@ function preprocessElements({ filename }) {
183184
if (node.type === 'heading') {
184185
headingIndex = index;
185186
heading = node;
187+
} else if (node.type === 'code') {
188+
if (!node.lang) {
189+
console.warn(
190+
`No language set in ${filename}, ` +
191+
`line ${node.position.start.line}`);
192+
}
193+
const language = (node.lang || '').split(' ')[0];
194+
const highlighted = getLanguage(language) ?
195+
highlight(language, node.value).value :
196+
node.value;
197+
node.type = 'html';
198+
node.value = '<pre>' +
199+
`<code class = 'language-${node.lang}'>` +
200+
highlighted +
201+
'</code></pre>';
186202
} else if (node.type === 'html' && common.isYAMLBlock(node.value)) {
187203
node.value = parseYAML(node.value);
188204

tools/doc/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/doc/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"unified": "^7.0.0",
1717
"unist-util-find": "^1.0.1",
1818
"unist-util-select": "^1.5.0",
19-
"unist-util-visit": "^1.4.0"
19+
"unist-util-visit": "^1.4.0",
20+
"highlightjs": "^9.10.0"
2021
},
2122
"devDependencies": {
2223
"js-yaml": "^3.13.1"

0 commit comments

Comments
 (0)