Skip to content

Commit 7b29c91

Browse files
tolmaskyaddaleax
authored andcommitted
doc,tools: syntax highlight api docs at compile-time
Prior to this commit, API docs would be highlighted after the page loaded using `highlightjs`. This commit still uses `highlightjs`, but runs the generation during the compilation of the documentation, making it so no script needs to load on the client. This results in a faster load, as well as allowing the pages to fully functional even when JavaScript is turned off. PR-URL: #34148 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent c3ac5e9 commit 7b29c91

File tree

7 files changed

+27
-30
lines changed

7 files changed

+27
-30
lines changed

doc/api_assets/README.md

-19
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

-6
This file was deleted.

doc/template.html

+2-2
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

+3-3
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

+16
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('highlight.js');
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

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/doc/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"node": ">=12.10.0"
88
},
99
"dependencies": {
10+
"highlight.js": "^9.18.1",
1011
"rehype-raw": "^2.0.0",
1112
"rehype-stringify": "^3.0.0",
1213
"remark-html": "^7.0.0",

0 commit comments

Comments
 (0)