Skip to content

Commit

Permalink
doc,tools: Replace client-side syntax highlighting for API docs compi…
Browse files Browse the repository at this point in the history
…le 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.
  • Loading branch information
tolmasky committed Jul 3, 2020
1 parent 1dc837e commit 8beb944
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 31 deletions.
19 changes: 0 additions & 19 deletions doc/api_assets/README.md
@@ -1,28 +1,9 @@
# API Reference Document Assets

## highlight.pack.js

_Generated by [highlightjs.org/download][] on 2020-06-07._

Grammars included in the custom bundle:

* Bash
* C
* C++
* CoffeeScript
* HTTP
* JavaScript
* JSON
* Markdown
* Plaintext
* Shell Session

## hljs.css

The syntax theme for code snippets in API reference documents.

## style.css

The main stylesheet for API reference documents.

[highlightjs.org/download]: https://highlightjs.org/download/
6 changes: 0 additions & 6 deletions doc/api_assets/highlight.pack.js

This file was deleted.

4 changes: 2 additions & 2 deletions doc/template.html
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta name="nodejs.org:node-version" content="__VERSION__">
<title>__SECTION__ | Node.js __VERSION__ Documentation</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
<link rel="stylesheet" href="assets/style.css">
Expand Down Expand Up @@ -48,10 +49,9 @@ <h2>Table of Contents</h2>

<div id="apicontent">
__CONTENT__
<!-- API END -->
</div>
</div>
</div>
<script src="assets/highlight.pack.js"></script>
<script>document.addEventListener('DOMContentLoaded', () => { hljs.initHighlightingOnLoad(); });</script>
</body>
</html>
6 changes: 3 additions & 3 deletions tools/doc/allhtml.js
Expand Up @@ -37,7 +37,7 @@ for (const link of toc.match(/<a.*?>/g)) {
.replace(/[\s\S]*?<div id="toc">\s*<h2>.*?<\/h2>\s*(<ul>\s*)?/, '');

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

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

// Write results.
fs.writeFileSync(source + '/all.html', all, 'utf8');
Expand Down
16 changes: 16 additions & 0 deletions tools/doc/html.js
Expand Up @@ -32,6 +32,7 @@ const raw = require('rehype-raw');
const htmlStringify = require('rehype-stringify');
const path = require('path');
const typeParser = require('./type-parser.js');
const { highlight, getLanguage } = require('highlightjs');

module.exports = {
toHTML, firstHeader, preprocessText, preprocessElements, buildToc
Expand Down Expand Up @@ -183,6 +184,21 @@ function preprocessElements({ filename }) {
if (node.type === 'heading') {
headingIndex = index;
heading = node;
} else if (node.type === 'code') {
if (!node.lang) {
console.warn(
`No language set in ${filename}, ` +
`line ${node.position.start.line}`);
}
const language = (node.lang || '').split(' ')[0];
const highlighted = getLanguage(language) ?
highlight(language, node.value).value :
node.value;
node.type = 'html';
node.value = '<pre>' +
`<code class = 'language-${node.lang}'>` +
highlighted +
'</code></pre>';
} else if (node.type === 'html' && common.isYAMLBlock(node.value)) {
node.value = parseYAML(node.value);

Expand Down
5 changes: 5 additions & 0 deletions tools/doc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tools/doc/package.json
Expand Up @@ -16,7 +16,8 @@
"unified": "^7.0.0",
"unist-util-find": "^1.0.1",
"unist-util-select": "^1.5.0",
"unist-util-visit": "^1.4.0"
"unist-util-visit": "^1.4.0",
"highlightjs": "^9.10.0"
},
"devDependencies": {
"js-yaml": "^3.13.1"
Expand Down

0 comments on commit 8beb944

Please sign in to comment.