Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: optimize HTML rendering #37301

Merged
merged 1 commit into from Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/api_assets/style.css
Expand Up @@ -372,6 +372,11 @@ dd + dt.pre {
#apicontent {
padding-top: 1rem;
}

#apicontent section {
content-visibility: auto;
contain-intrinsic-size: 1px 5000px;
}

#apicontent .line {
width: calc(50% - 1rem);
Expand Down
4 changes: 2 additions & 2 deletions tools/doc/allhtml.js
Expand Up @@ -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;
})
Expand Down
14 changes: 13 additions & 1 deletion tools/doc/html.js
Expand Up @@ -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');

Expand All @@ -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*-->/);
Expand Down