diff options
author | Antoine du Hamel <duhamelantoine1995@gmail.com> | 2021-02-10 00:41:47 +0300 |
---|---|---|
committer | Danielle Adams <adamzdanielle@gmail.com> | 2021-02-16 17:16:17 +0300 |
commit | dd054ca37fc2c91a3a5ba34ef73c54d60539fc6d (patch) | |
tree | 8bf5d4ce99f9eed0b6f4a1b067c4e908ed0ac9e5 /tools | |
parent | c188466a1855793bebb9048c6de73c6fbdd203eb (diff) |
doc: optimize HTML rendering
Defer rendering sections of docs until they are displayed on
the user's screen.
PR-URL: https://github.com/nodejs/node/pull/37301
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/doc/allhtml.js | 4 | ||||
-rw-r--r-- | tools/doc/html.js | 14 |
2 files changed, 15 insertions, 3 deletions
diff --git a/tools/doc/allhtml.js b/tools/doc/allhtml.js index 93197f812e1..fad74e59d5c 100644 --- a/tools/doc/allhtml.js +++ b/tools/doc/allhtml.js @@ -36,8 +36,8 @@ for (const link of toc.match(/<a.*?>/g)) { contents += data.slice(0, match.index) .replace(/[\s\S]*?id="toc"[^>]*>\s*<\w+>.*?<\/\w+>\s*(<ul>\s*)?/, ''); - apicontent += data.slice(match.index + match[0].length) - .replace(/<!-- API END -->[\s\S]*/, '') + apicontent += '<section>' + data.slice(match.index + match[0].length) + .replace(/<!-- API END -->[\s\S]*/, '</section>') .replace(/<a href="(\w[^#"]*)#/g, (match, href) => { return htmlFiles.includes(href) ? '<a href="#' : match; }) diff --git a/tools/doc/html.js b/tools/doc/html.js index 07a3e5ef405..b519fd9c5a8 100644 --- a/tools/doc/html.js +++ b/tools/doc/html.js @@ -66,6 +66,18 @@ const gtocHTML = unified() const templatePath = path.join(docPath, 'template.html'); const template = fs.readFileSync(templatePath, 'utf8'); +function wrapSections(content) { + let firstTime = true; + return content.toString() + .replace(/<h2/g, (heading) => { + if (firstTime) { + firstTime = false; + return '<section>' + heading; + } + return '</section><section>' + heading; + }) + (firstTime ? '' : '</section>'); +} + function toHTML({ input, content, filename, nodeVersion, versions }) { filename = path.basename(filename, '.md'); @@ -79,7 +91,7 @@ function toHTML({ input, content, filename, nodeVersion, versions }) { .replace('__GTOC__', gtocHTML.replace( `class="nav-${id}"`, `class="nav-${id} active"`)) .replace('__EDIT_ON_GITHUB__', editOnGitHub(filename)) - .replace('__CONTENT__', content.toString()); + .replace('__CONTENT__', wrapSections(content)); const docCreated = input.match( /<!--\s*introduced_in\s*=\s*v([0-9]+)\.([0-9]+)\.[0-9]+\s*-->/); |