2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2021-06-08 10:44:33 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
import highlightJs from 'highlight.js';
|
|
|
|
import raw from 'rehype-raw';
|
|
|
|
import htmlStringify from 'rehype-stringify';
|
|
|
|
import gfm from 'remark-gfm';
|
|
|
|
import markdown from 'remark-parse';
|
|
|
|
import remark2rehype from 'remark-rehype';
|
2021-09-08 19:49:55 +00:00
|
|
|
import { unified } from 'unified';
|
2021-06-08 10:44:33 +00:00
|
|
|
import { visit } from 'unist-util-visit';
|
|
|
|
|
|
|
|
import * as common from './common.mjs';
|
|
|
|
import * as typeParser from './type-parser.mjs';
|
|
|
|
|
|
|
|
const { highlight, getLanguage } = highlightJs;
|
|
|
|
|
|
|
|
const docPath = new URL('../../doc/', import.meta.url);
|
2018-04-25 20:44:38 +00:00
|
|
|
|
2018-06-23 19:42:12 +00:00
|
|
|
// Add class attributes to index navigation links.
|
|
|
|
function navClasses() {
|
|
|
|
return (tree) => {
|
|
|
|
visit(tree, { type: 'element', tagName: 'a' }, (node) => {
|
|
|
|
node.properties.class = 'nav-' +
|
|
|
|
node.properties.href.replace('.html', '').replace(/\W+/g, '-');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-08 10:44:33 +00:00
|
|
|
const gtocPath = new URL('./api/index.md', docPath);
|
2020-09-14 15:09:13 +00:00
|
|
|
const gtocMD = fs.readFileSync(gtocPath, 'utf8')
|
|
|
|
.replace(/\(([^#?]+?)\.md\)/ig, (_, filename) => `(${filename}.html)`)
|
|
|
|
.replace(/^<!--.*?-->/gms, '');
|
2018-06-23 19:42:12 +00:00
|
|
|
const gtocHTML = unified()
|
|
|
|
.use(markdown)
|
2020-11-09 13:45:59 +00:00
|
|
|
.use(gfm)
|
2020-09-17 16:12:06 +00:00
|
|
|
.use(remark2rehype, { allowDangerousHtml: true })
|
2018-06-23 19:42:12 +00:00
|
|
|
.use(raw)
|
|
|
|
.use(navClasses)
|
2018-07-07 01:46:38 +00:00
|
|
|
.use(htmlStringify)
|
2018-06-23 19:42:12 +00:00
|
|
|
.processSync(gtocMD).toString();
|
2018-04-25 20:44:38 +00:00
|
|
|
|
2021-06-08 10:44:33 +00:00
|
|
|
const templatePath = new URL('./template.html', docPath);
|
2018-04-25 20:44:38 +00:00
|
|
|
const template = fs.readFileSync(templatePath, 'utf8');
|
|
|
|
|
2021-03-20 14:21:36 +00:00
|
|
|
function processContent(content) {
|
|
|
|
content = content.toString();
|
|
|
|
// Increment header tag levels to avoid multiple h1 tags in a doc.
|
|
|
|
// This means we can't already have an <h6>.
|
|
|
|
if (content.includes('<h6>')) {
|
|
|
|
throw new Error('Cannot increment a level 6 header');
|
|
|
|
}
|
|
|
|
// `++level` to convert the string to a number and increment it.
|
|
|
|
content = content.replace(/(?<=<\/?h)[1-5](?=[^<>]*>)/g, (level) => ++level);
|
|
|
|
// Wrap h3 tags in section tags.
|
2021-02-09 21:41:47 +00:00
|
|
|
let firstTime = true;
|
2021-03-20 14:21:36 +00:00
|
|
|
return content
|
|
|
|
.replace(/<h3/g, (heading) => {
|
2021-02-09 21:41:47 +00:00
|
|
|
if (firstTime) {
|
|
|
|
firstTime = false;
|
|
|
|
return '<section>' + heading;
|
|
|
|
}
|
|
|
|
return '</section><section>' + heading;
|
|
|
|
}) + (firstTime ? '' : '</section>');
|
|
|
|
}
|
|
|
|
|
2021-06-08 10:44:33 +00:00
|
|
|
export function toHTML({ input, content, filename, nodeVersion, versions }) {
|
2018-04-25 15:24:27 +00:00
|
|
|
filename = path.basename(filename, '.md');
|
2015-11-18 22:40:03 +00:00
|
|
|
|
2018-04-25 15:24:27 +00:00
|
|
|
const id = filename.replace(/\W+/g, '-');
|
2012-02-27 18:59:01 +00:00
|
|
|
|
2018-04-25 15:24:27 +00:00
|
|
|
let HTML = template.replace('__ID__', id)
|
|
|
|
.replace(/__FILENAME__/g, filename)
|
2018-06-23 19:42:12 +00:00
|
|
|
.replace('__SECTION__', content.section)
|
2018-04-25 15:24:27 +00:00
|
|
|
.replace(/__VERSION__/g, nodeVersion)
|
2018-06-23 19:42:12 +00:00
|
|
|
.replace('__TOC__', content.toc)
|
2018-04-25 15:24:27 +00:00
|
|
|
.replace('__GTOC__', gtocHTML.replace(
|
2020-08-29 18:50:29 +00:00
|
|
|
`class="nav-${id}"`, `class="nav-${id} active"`))
|
2018-06-23 19:42:12 +00:00
|
|
|
.replace('__EDIT_ON_GITHUB__', editOnGitHub(filename))
|
2021-03-20 14:21:36 +00:00
|
|
|
.replace('__CONTENT__', processContent(content));
|
2012-02-27 18:59:01 +00:00
|
|
|
|
2018-04-25 15:24:27 +00:00
|
|
|
const docCreated = input.match(
|
|
|
|
/<!--\s*introduced_in\s*=\s*v([0-9]+)\.([0-9]+)\.[0-9]+\s*-->/);
|
|
|
|
if (docCreated) {
|
2020-03-27 14:04:40 +00:00
|
|
|
HTML = HTML.replace('__ALTDOCS__', altDocs(filename, docCreated, versions));
|
2018-04-25 15:24:27 +00:00
|
|
|
} else {
|
|
|
|
console.error(`Failed to add alternative version links to ${filename}`);
|
|
|
|
HTML = HTML.replace('__ALTDOCS__', '');
|
2017-01-23 03:16:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 06:15:28 +00:00
|
|
|
return HTML;
|
2017-01-23 03:16:21 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 19:42:12 +00:00
|
|
|
// Set the section name based on the first header. Default to 'Index'.
|
2021-06-08 10:44:33 +00:00
|
|
|
export function firstHeader() {
|
2018-06-23 19:42:12 +00:00
|
|
|
return (tree, file) => {
|
2021-02-07 16:18:16 +00:00
|
|
|
let heading;
|
|
|
|
visit(tree, (node) => {
|
|
|
|
if (node.type === 'heading') {
|
|
|
|
heading = node;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2020-08-31 23:12:54 +00:00
|
|
|
|
|
|
|
if (heading && heading.children.length) {
|
|
|
|
const recursiveTextContent = (node) =>
|
|
|
|
node.value || node.children.map(recursiveTextContent).join('');
|
|
|
|
file.section = recursiveTextContent(heading);
|
|
|
|
} else {
|
|
|
|
file.section = 'Index';
|
2016-02-04 12:11:17 +00:00
|
|
|
}
|
2018-06-23 19:42:12 +00:00
|
|
|
};
|
2016-02-04 12:11:17 +00:00
|
|
|
}
|
2012-02-27 18:59:01 +00:00
|
|
|
|
2018-06-23 19:42:12 +00:00
|
|
|
// Handle general body-text replacements.
|
|
|
|
// For example, link man page references to the actual page.
|
2021-06-08 10:44:33 +00:00
|
|
|
export function preprocessText({ nodeVersion }) {
|
2018-06-23 19:42:12 +00:00
|
|
|
return (tree) => {
|
|
|
|
visit(tree, null, (node) => {
|
2020-06-22 17:56:08 +00:00
|
|
|
if (common.isSourceLink(node.value)) {
|
|
|
|
const [path] = node.value.match(/(?<=<!-- source_link=).*(?= -->)/);
|
|
|
|
node.value = `<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/${nodeVersion}/${path}">${path}</a></p>`;
|
|
|
|
} else if (node.type === 'text' && node.value) {
|
2018-06-23 19:42:12 +00:00
|
|
|
const value = linkJsTypeDocs(linkManPages(node.value));
|
|
|
|
if (value !== node.value) {
|
|
|
|
node.type = 'html';
|
|
|
|
node.value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2018-04-25 15:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Syscalls which appear in the docs, but which only exist in BSD / macOS.
|
|
|
|
const BSD_ONLY_SYSCALLS = new Set(['lchmod']);
|
|
|
|
const MAN_PAGE = /(^|\s)([a-z.]+)\((\d)([a-z]?)\)/gm;
|
|
|
|
|
|
|
|
// Handle references to man pages, eg "open(2)" or "lchmod(2)".
|
|
|
|
// Returns modified text, with such refs replaced with HTML links, for example
|
|
|
|
// '<a href="http://man7.org/linux/man-pages/man2/open.2.html">open(2)</a>'.
|
|
|
|
function linkManPages(text) {
|
|
|
|
return text.replace(
|
|
|
|
MAN_PAGE, (match, beginning, name, number, optionalCharacter) => {
|
|
|
|
// Name consists of lowercase letters,
|
|
|
|
// number is a single digit with an optional lowercase letter.
|
2018-05-16 18:44:09 +00:00
|
|
|
const displayAs = `<code>${name}(${number}${optionalCharacter})</code>`;
|
2018-04-25 15:24:27 +00:00
|
|
|
|
|
|
|
if (BSD_ONLY_SYSCALLS.has(name)) {
|
2021-04-05 14:19:42 +00:00
|
|
|
return `${beginning}<a href="https://www.freebsd.org/cgi/man.cgi?query=${name}&sektion=${number}">${displayAs}</a>`;
|
2018-04-25 15:24:27 +00:00
|
|
|
}
|
2019-04-10 03:30:03 +00:00
|
|
|
|
2021-04-05 14:19:42 +00:00
|
|
|
return `${beginning}<a href="http://man7.org/linux/man-pages/man${number}/${name}.${number}${optionalCharacter}.html">${displayAs}</a>`;
|
2018-04-25 15:24:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const TYPE_SIGNATURE = /\{[^}]+\}/g;
|
|
|
|
function linkJsTypeDocs(text) {
|
|
|
|
const parts = text.split('`');
|
|
|
|
|
|
|
|
// Handle types, for example the source Markdown might say
|
|
|
|
// "This argument should be a {number} or {string}".
|
|
|
|
for (let i = 0; i < parts.length; i += 2) {
|
|
|
|
const typeMatches = parts[i].match(TYPE_SIGNATURE);
|
|
|
|
if (typeMatches) {
|
|
|
|
typeMatches.forEach((typeMatch) => {
|
|
|
|
parts[i] = parts[i].replace(typeMatch, typeParser.toLink(typeMatch));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts.join('`');
|
|
|
|
}
|
|
|
|
|
2021-01-31 22:10:11 +00:00
|
|
|
const isJSFlavorSnippet = (node) => node.lang === 'cjs' || node.lang === 'mjs';
|
|
|
|
|
2018-07-22 21:13:56 +00:00
|
|
|
// Preprocess headers, stability blockquotes, and YAML blocks.
|
2021-06-08 10:44:33 +00:00
|
|
|
export function preprocessElements({ filename }) {
|
2018-08-05 19:09:15 +00:00
|
|
|
return (tree) => {
|
2018-06-23 19:42:12 +00:00
|
|
|
const STABILITY_RE = /(.*:)\s*(\d)([\s\S]*)/;
|
|
|
|
let headingIndex = -1;
|
|
|
|
let heading = null;
|
|
|
|
|
2021-01-31 22:10:11 +00:00
|
|
|
visit(tree, null, (node, index, parent) => {
|
2018-06-23 19:42:12 +00:00
|
|
|
if (node.type === 'heading') {
|
|
|
|
headingIndex = index;
|
|
|
|
heading = node;
|
2020-06-30 19:35:28 +00:00
|
|
|
} else if (node.type === 'code') {
|
|
|
|
if (!node.lang) {
|
|
|
|
console.warn(
|
2021-04-05 14:19:42 +00:00
|
|
|
`No language set in ${filename}, line ${node.position.start.line}`
|
|
|
|
);
|
2020-06-30 19:35:28 +00:00
|
|
|
}
|
2021-01-31 22:10:11 +00:00
|
|
|
const className = isJSFlavorSnippet(node) ?
|
|
|
|
`language-js ${node.lang}` :
|
|
|
|
`language-${node.lang}`;
|
|
|
|
const highlighted =
|
2021-06-08 10:44:33 +00:00
|
|
|
`<code class='${className}'>${(getLanguage(node.lang || '') ? highlight(node.value, { language: node.lang }) : node).value}</code>`;
|
2020-06-30 19:35:28 +00:00
|
|
|
node.type = 'html';
|
2021-01-31 22:10:11 +00:00
|
|
|
|
|
|
|
if (isJSFlavorSnippet(node)) {
|
|
|
|
const previousNode = parent.children[index - 1] || {};
|
|
|
|
const nextNode = parent.children[index + 1] || {};
|
|
|
|
|
|
|
|
if (!isJSFlavorSnippet(previousNode) &&
|
|
|
|
isJSFlavorSnippet(nextNode) &&
|
|
|
|
nextNode.lang !== node.lang) {
|
|
|
|
// Saving the highlight code as value to be added in the next node.
|
|
|
|
node.value = highlighted;
|
2021-09-18 19:03:19 +00:00
|
|
|
} else if (isJSFlavorSnippet(previousNode) &&
|
|
|
|
previousNode.lang !== node.lang) {
|
2021-01-31 22:10:11 +00:00
|
|
|
node.value = '<pre>' +
|
|
|
|
'<input class="js-flavor-selector" type="checkbox"' +
|
|
|
|
// If CJS comes in second, ESM should display by default.
|
|
|
|
(node.lang === 'cjs' ? ' checked' : '') +
|
|
|
|
' aria-label="Show modern ES modules syntax">' +
|
|
|
|
previousNode.value +
|
|
|
|
highlighted +
|
|
|
|
'</pre>';
|
|
|
|
node.lang = null;
|
|
|
|
previousNode.value = '';
|
|
|
|
previousNode.lang = null;
|
|
|
|
} else {
|
|
|
|
// Isolated JS snippet, no need to add the checkbox.
|
|
|
|
node.value = `<pre>${highlighted}</pre>`;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
node.value = `<pre>${highlighted}</pre>`;
|
|
|
|
}
|
2018-06-23 19:42:12 +00:00
|
|
|
} else if (node.type === 'html' && common.isYAMLBlock(node.value)) {
|
|
|
|
node.value = parseYAML(node.value);
|
|
|
|
|
|
|
|
} else if (node.type === 'blockquote') {
|
|
|
|
const paragraph = node.children[0].type === 'paragraph' &&
|
|
|
|
node.children[0];
|
|
|
|
const text = paragraph && paragraph.children[0].type === 'text' &&
|
|
|
|
paragraph.children[0];
|
|
|
|
if (text && text.value.includes('Stability:')) {
|
|
|
|
const [, prefix, number, explication] =
|
|
|
|
text.value.match(STABILITY_RE);
|
|
|
|
|
2021-03-27 18:22:03 +00:00
|
|
|
// Stability indices are never more than 3 nodes away from their
|
|
|
|
// heading.
|
|
|
|
const isStabilityIndex = index - headingIndex <= 3;
|
2018-06-23 19:42:12 +00:00
|
|
|
|
|
|
|
if (heading && isStabilityIndex) {
|
|
|
|
heading.stability = number;
|
|
|
|
headingIndex = -1;
|
|
|
|
heading = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not link to the section we are already in.
|
2018-08-05 15:44:22 +00:00
|
|
|
const noLinking = filename.includes('documentation') &&
|
2020-11-26 13:52:46 +00:00
|
|
|
heading !== null && heading.children[0].value === 'Stability index';
|
2018-06-23 19:42:12 +00:00
|
|
|
|
2018-12-10 12:27:32 +00:00
|
|
|
// Collapse blockquote and paragraph into a single node
|
2018-06-23 19:42:12 +00:00
|
|
|
node.type = 'paragraph';
|
|
|
|
node.children.shift();
|
|
|
|
node.children.unshift(...paragraph.children);
|
|
|
|
|
2019-03-07 00:03:53 +00:00
|
|
|
// Insert div with prefix and number
|
2018-06-23 19:42:12 +00:00
|
|
|
node.children.unshift({
|
|
|
|
type: 'html',
|
|
|
|
value: `<div class="api_stability api_stability_${number}">` +
|
|
|
|
(noLinking ? '' :
|
2021-07-05 03:39:17 +00:00
|
|
|
'<a href="documentation.html#stability-index">') +
|
2018-06-23 19:42:12 +00:00
|
|
|
`${prefix} ${number}${noLinking ? '' : '</a>'}`
|
|
|
|
.replace(/\n/g, ' ')
|
|
|
|
});
|
|
|
|
|
2019-03-07 00:03:53 +00:00
|
|
|
// Remove prefix and number from text
|
2018-06-23 19:42:12 +00:00
|
|
|
text.value = explication;
|
|
|
|
|
|
|
|
// close div
|
|
|
|
node.children.push({ type: 'html', value: '</div>' });
|
2016-09-02 20:27:01 +00:00
|
|
|
}
|
2012-02-27 18:59:01 +00:00
|
|
|
}
|
2018-06-23 19:42:12 +00:00
|
|
|
});
|
|
|
|
};
|
2012-02-27 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 02:40:38 +00:00
|
|
|
function parseYAML(text) {
|
|
|
|
const meta = common.extractAndParseYAML(text);
|
2018-06-23 19:42:12 +00:00
|
|
|
let result = '<div class="api_metadata">\n';
|
2015-11-14 02:40:38 +00:00
|
|
|
|
2017-01-10 19:43:57 +00:00
|
|
|
const added = { description: '' };
|
|
|
|
const deprecated = { description: '' };
|
2018-08-07 05:00:26 +00:00
|
|
|
const removed = { description: '' };
|
2017-01-10 19:43:57 +00:00
|
|
|
|
2016-04-30 23:51:38 +00:00
|
|
|
if (meta.added) {
|
2017-01-10 19:43:57 +00:00
|
|
|
added.version = meta.added.join(', ');
|
|
|
|
added.description = `<span>Added in: ${added.version}</span>`;
|
2016-04-30 23:51:38 +00:00
|
|
|
}
|
2015-11-14 02:40:38 +00:00
|
|
|
|
2016-04-30 23:51:38 +00:00
|
|
|
if (meta.deprecated) {
|
2017-01-10 19:43:57 +00:00
|
|
|
deprecated.version = meta.deprecated.join(', ');
|
|
|
|
deprecated.description =
|
|
|
|
`<span>Deprecated since: ${deprecated.version}</span>`;
|
|
|
|
}
|
|
|
|
|
2018-08-07 05:00:26 +00:00
|
|
|
if (meta.removed) {
|
|
|
|
removed.version = meta.removed.join(', ');
|
|
|
|
removed.description = `<span>Removed in: ${removed.version}</span>`;
|
|
|
|
}
|
|
|
|
|
2017-01-10 19:43:57 +00:00
|
|
|
if (meta.changes.length > 0) {
|
2018-04-25 15:24:27 +00:00
|
|
|
if (added.description) meta.changes.push(added);
|
|
|
|
if (deprecated.description) meta.changes.push(deprecated);
|
2018-08-07 05:00:26 +00:00
|
|
|
if (removed.description) meta.changes.push(removed);
|
2017-01-10 19:43:57 +00:00
|
|
|
|
2018-04-25 15:24:27 +00:00
|
|
|
meta.changes.sort((a, b) => versionSort(a.version, b.version));
|
2017-01-10 19:43:57 +00:00
|
|
|
|
2018-06-23 19:42:12 +00:00
|
|
|
result += '<details class="changelog"><summary>History</summary>\n' +
|
2018-04-25 15:24:27 +00:00
|
|
|
'<table>\n<tr><th>Version</th><th>Changes</th></tr>\n';
|
2017-01-10 19:43:57 +00:00
|
|
|
|
2018-04-25 15:24:27 +00:00
|
|
|
meta.changes.forEach((change) => {
|
2018-06-23 19:42:12 +00:00
|
|
|
const description = unified()
|
|
|
|
.use(markdown)
|
2020-11-09 13:45:59 +00:00
|
|
|
.use(gfm)
|
2020-09-17 16:12:06 +00:00
|
|
|
.use(remark2rehype, { allowDangerousHtml: true })
|
2018-06-23 19:42:12 +00:00
|
|
|
.use(raw)
|
2018-07-07 01:46:38 +00:00
|
|
|
.use(htmlStringify)
|
2018-06-23 19:42:12 +00:00
|
|
|
.processSync(change.description).toString();
|
|
|
|
|
2018-09-08 09:40:05 +00:00
|
|
|
const version = common.arrify(change.version).join(', ');
|
|
|
|
|
|
|
|
result += `<tr><td>${version}</td>\n` +
|
2018-06-23 19:42:12 +00:00
|
|
|
`<td>${description}</td></tr>\n`;
|
2017-01-10 19:43:57 +00:00
|
|
|
});
|
|
|
|
|
2018-06-23 19:42:12 +00:00
|
|
|
result += '</table>\n</details>\n';
|
2017-01-10 19:43:57 +00:00
|
|
|
} else {
|
2021-04-05 14:19:42 +00:00
|
|
|
result += `${added.description}${deprecated.description}${removed.description}\n`;
|
2013-12-25 15:59:25 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 23:01:13 +00:00
|
|
|
if (meta.napiVersion) {
|
2018-06-23 19:42:12 +00:00
|
|
|
result += `<span>N-API version: ${meta.napiVersion.join(', ')}</span>\n`;
|
2017-12-07 23:01:13 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 19:42:12 +00:00
|
|
|
result += '</div>';
|
|
|
|
return result;
|
2012-02-27 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 09:40:05 +00:00
|
|
|
function minVersion(a) {
|
|
|
|
return common.arrify(a).reduce((min, e) => {
|
|
|
|
return !min || versionSort(min, e) < 0 ? e : min;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-25 15:24:27 +00:00
|
|
|
const numberRe = /^\d*/;
|
|
|
|
function versionSort(a, b) {
|
2018-09-08 09:40:05 +00:00
|
|
|
a = minVersion(a).trim();
|
|
|
|
b = minVersion(b).trim();
|
2018-04-25 15:24:27 +00:00
|
|
|
let i = 0; // Common prefix length.
|
|
|
|
while (i < a.length && i < b.length && a[i] === b[i]) i++;
|
|
|
|
a = a.substr(i);
|
|
|
|
b = b.substr(i);
|
|
|
|
return +b.match(numberRe)[0] - +a.match(numberRe)[0];
|
2017-11-05 17:36:50 +00:00
|
|
|
}
|
2012-02-27 18:59:01 +00:00
|
|
|
|
2020-08-31 22:30:03 +00:00
|
|
|
const DEPRECATION_HEADING_PATTERN = /^DEP\d+:/;
|
2021-06-08 10:44:33 +00:00
|
|
|
export function buildToc({ filename, apilinks }) {
|
2018-06-23 19:42:12 +00:00
|
|
|
return (tree, file) => {
|
|
|
|
const idCounters = Object.create(null);
|
2021-07-05 03:39:17 +00:00
|
|
|
const legacyIdCounters = Object.create(null);
|
2018-06-23 19:42:12 +00:00
|
|
|
let toc = '';
|
|
|
|
let depth = 0;
|
|
|
|
|
|
|
|
visit(tree, null, (node) => {
|
|
|
|
if (node.type !== 'heading') return;
|
2018-04-25 15:24:27 +00:00
|
|
|
|
2018-06-23 19:42:12 +00:00
|
|
|
if (node.depth - depth > 1) {
|
|
|
|
throw new Error(
|
|
|
|
`Inappropriate heading level:\n${JSON.stringify(node)}`
|
|
|
|
);
|
|
|
|
}
|
2012-02-27 18:59:01 +00:00
|
|
|
|
2018-06-23 19:42:12 +00:00
|
|
|
depth = node.depth;
|
2020-04-18 12:22:07 +00:00
|
|
|
const realFilename = path.basename(filename, '.md');
|
2021-09-08 19:49:55 +00:00
|
|
|
const headingText = file.value.slice(
|
2018-07-22 21:13:56 +00:00
|
|
|
node.children[0].position.start.offset,
|
|
|
|
node.position.end.offset).trim();
|
2021-07-05 03:39:17 +00:00
|
|
|
const id = getId(headingText, idCounters);
|
|
|
|
// Use previous ID generator to create alias
|
|
|
|
const legacyId = getLegacyId(`${realFilename}_${headingText}`, legacyIdCounters);
|
2018-05-14 22:23:55 +00:00
|
|
|
|
2020-08-31 22:30:03 +00:00
|
|
|
const isDeprecationHeading =
|
|
|
|
DEPRECATION_HEADING_PATTERN.test(headingText);
|
|
|
|
if (isDeprecationHeading) {
|
|
|
|
if (!node.data) node.data = {};
|
|
|
|
if (!node.data.hProperties) node.data.hProperties = {};
|
|
|
|
node.data.hProperties.id =
|
|
|
|
headingText.substring(0, headingText.indexOf(':'));
|
|
|
|
}
|
|
|
|
|
2018-06-23 19:42:12 +00:00
|
|
|
const hasStability = node.stability !== undefined;
|
|
|
|
toc += ' '.repeat((depth - 1) * 2) +
|
|
|
|
(hasStability ? `* <span class="stability_${node.stability}">` : '* ') +
|
2021-04-05 14:19:42 +00:00
|
|
|
`<a href="#${isDeprecationHeading ? node.data.hProperties.id : id}">${headingText}</a>${hasStability ? '</span>' : ''}\n`;
|
2018-05-14 22:23:55 +00:00
|
|
|
|
2018-06-23 19:42:12 +00:00
|
|
|
let anchor =
|
|
|
|
`<span><a class="mark" href="#${id}" id="${id}">#</a></span>`;
|
|
|
|
|
2021-07-05 03:39:17 +00:00
|
|
|
// Add alias anchor to preserve old links
|
|
|
|
anchor += `<a aria-hidden="true" class="legacy" id="${legacyId}"></a>`;
|
|
|
|
|
2018-06-23 19:42:12 +00:00
|
|
|
if (realFilename === 'errors' && headingText.startsWith('ERR_')) {
|
2021-04-05 14:19:42 +00:00
|
|
|
anchor +=
|
|
|
|
`<span><a class="mark" href="#${headingText}" id="${headingText}">#</a></span>`;
|
2018-06-23 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 18:03:21 +00:00
|
|
|
const api = headingText.replace(/^.*:\s+/, '').replace(/\(.*/, '');
|
|
|
|
if (apilinks[api]) {
|
|
|
|
anchor = `<a class="srclink" href=${apilinks[api]}>[src]</a>${anchor}`;
|
|
|
|
}
|
|
|
|
|
2018-06-23 19:42:12 +00:00
|
|
|
node.children.push({ type: 'html', value: anchor });
|
|
|
|
});
|
2012-02-27 18:59:01 +00:00
|
|
|
|
2021-01-24 02:58:26 +00:00
|
|
|
if (toc !== '') {
|
|
|
|
file.toc = '<details id="toc" open><summary>Table of contents</summary>' +
|
|
|
|
unified()
|
|
|
|
.use(markdown)
|
|
|
|
.use(gfm)
|
|
|
|
.use(remark2rehype, { allowDangerousHtml: true })
|
|
|
|
.use(raw)
|
|
|
|
.use(htmlStringify)
|
|
|
|
.processSync(toc).toString() +
|
|
|
|
'</details>';
|
|
|
|
} else {
|
|
|
|
file.toc = '<!-- TOC -->';
|
|
|
|
}
|
2018-06-23 19:42:12 +00:00
|
|
|
};
|
2012-02-27 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 03:39:17 +00:00
|
|
|
// ID generator that mirrors Github's heading anchor parser
|
|
|
|
const punctuation = /[^\w\- ]/g;
|
|
|
|
function getId(text, idCounters) {
|
|
|
|
text = text.toLowerCase()
|
|
|
|
.replace(punctuation, '')
|
|
|
|
.replace(/ /g, '-');
|
|
|
|
if (idCounters[text] !== undefined) {
|
|
|
|
return `${text}_${++idCounters[text]}`;
|
|
|
|
}
|
|
|
|
idCounters[text] = 0;
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This ID generator is purely to generate aliases
|
|
|
|
// so we can preserve old doc links
|
2018-04-25 15:24:27 +00:00
|
|
|
const notAlphaNumerics = /[^a-z0-9]+/g;
|
|
|
|
const edgeUnderscores = /^_+|_+$/g;
|
|
|
|
const notAlphaStart = /^[^a-z]/;
|
2021-07-05 03:39:17 +00:00
|
|
|
function getLegacyId(text, idCounters) {
|
2018-04-25 15:24:27 +00:00
|
|
|
text = text.toLowerCase()
|
|
|
|
.replace(notAlphaNumerics, '_')
|
|
|
|
.replace(edgeUnderscores, '')
|
|
|
|
.replace(notAlphaStart, '_$&');
|
|
|
|
if (idCounters[text] !== undefined) {
|
|
|
|
return `${text}_${++idCounters[text]}`;
|
2012-02-27 18:59:01 +00:00
|
|
|
}
|
2018-04-25 15:24:27 +00:00
|
|
|
idCounters[text] = 0;
|
2012-02-27 18:59:01 +00:00
|
|
|
return text;
|
|
|
|
}
|
2017-01-10 19:43:57 +00:00
|
|
|
|
2020-03-27 14:04:40 +00:00
|
|
|
function altDocs(filename, docCreated, versions) {
|
2018-04-25 15:24:27 +00:00
|
|
|
const [, docCreatedMajor, docCreatedMinor] = docCreated.map(Number);
|
|
|
|
const host = 'https://nodejs.org';
|
|
|
|
|
|
|
|
const getHref = (versionNum) =>
|
|
|
|
`${host}/docs/latest-v${versionNum}/api/${filename}.html`;
|
|
|
|
|
|
|
|
const wrapInListItem = (version) =>
|
2021-04-05 14:19:42 +00:00
|
|
|
`<li><a href="${getHref(version.num)}">${version.num}${version.lts ? ' <b>LTS</b>' : ''}</a></li>`;
|
2018-04-25 15:24:27 +00:00
|
|
|
|
|
|
|
function isDocInVersion(version) {
|
|
|
|
const [versionMajor, versionMinor] = version.num.split('.').map(Number);
|
|
|
|
if (docCreatedMajor > versionMajor) return false;
|
|
|
|
if (docCreatedMajor < versionMajor) return true;
|
2018-11-25 07:06:55 +00:00
|
|
|
if (Number.isNaN(versionMinor)) return true;
|
2018-04-25 15:24:27 +00:00
|
|
|
return docCreatedMinor <= versionMinor;
|
|
|
|
}
|
|
|
|
|
|
|
|
const list = versions.filter(isDocInVersion).map(wrapInListItem).join('\n');
|
|
|
|
|
|
|
|
return list ? `
|
|
|
|
<li class="version-picker">
|
|
|
|
<a href="#">View another version <span>▼</span></a>
|
|
|
|
<ol class="version-picker">${list}</ol>
|
|
|
|
</li>
|
|
|
|
` : '';
|
2017-01-10 19:43:57 +00:00
|
|
|
}
|
2018-07-07 22:33:06 +00:00
|
|
|
|
|
|
|
function editOnGitHub(filename) {
|
2021-07-03 17:18:24 +00:00
|
|
|
return `<li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/${filename}.md">Edit on GitHub</a></li>`;
|
2018-07-07 22:33:06 +00:00
|
|
|
}
|