summaryrefslogtreecommitdiff
path: root/tools/doc
diff options
context:
space:
mode:
authorRichard Lau <riclau@uk.ibm.com>2019-05-12 13:08:37 -0400
committerRich Trott <rtrott@gmail.com>2019-06-01 10:17:30 +0200
commit4ec6135c71f28d6c8691d46bfc09360cdcaf715c (patch)
tree5cb871fae85ba492aa0c3a4e1cc1f6f6b17297c0 /tools/doc
parent5aaa7fee2e4a075d9123b885f9e8cda3de2a780a (diff)
downloadandroid-node-v8-4ec6135c71f28d6c8691d46bfc09360cdcaf715c.tar.gz
android-node-v8-4ec6135c71f28d6c8691d46bfc09360cdcaf715c.tar.bz2
android-node-v8-4ec6135c71f28d6c8691d46bfc09360cdcaf715c.zip
doc,tools: get altDocs versions from CHANGELOG.md
Parse `CHANGELOG.md` for versions of Node.js used by the documentation feature `View another version` so that we don't have to manually update the list when we cut a new version or transition a release to LTS. PR-URL: https://github.com/nodejs/node/pull/27661 Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'tools/doc')
-rw-r--r--tools/doc/html.js21
-rw-r--r--tools/doc/versions.js45
2 files changed, 50 insertions, 16 deletions
diff --git a/tools/doc/html.js b/tools/doc/html.js
index efdc8b0d47..318feefe34 100644
--- a/tools/doc/html.js
+++ b/tools/doc/html.js
@@ -23,6 +23,7 @@
const common = require('./common.js');
const fs = require('fs');
+const getVersions = require('./versions.js');
const unified = require('unified');
const find = require('unist-util-find');
const visit = require('unist-util-visit');
@@ -62,7 +63,7 @@ const gtocHTML = unified()
const templatePath = path.join(docPath, 'template.html');
const template = fs.readFileSync(templatePath, 'utf8');
-function toHTML({ input, content, filename, nodeVersion }, cb) {
+async function toHTML({ input, content, filename, nodeVersion }, cb) {
filename = path.basename(filename, '.md');
const id = filename.replace(/\W+/g, '-');
@@ -80,7 +81,7 @@ function toHTML({ input, content, filename, nodeVersion }, cb) {
const docCreated = input.match(
/<!--\s*introduced_in\s*=\s*v([0-9]+)\.([0-9]+)\.[0-9]+\s*-->/);
if (docCreated) {
- HTML = HTML.replace('__ALTDOCS__', altDocs(filename, docCreated));
+ HTML = HTML.replace('__ALTDOCS__', await altDocs(filename, docCreated));
} else {
console.error(`Failed to add alternative version links to ${filename}`);
HTML = HTML.replace('__ALTDOCS__', '');
@@ -390,22 +391,10 @@ function getId(text, idCounters) {
return text;
}
-function altDocs(filename, docCreated) {
+async function altDocs(filename, docCreated) {
const [, docCreatedMajor, docCreatedMinor] = docCreated.map(Number);
const host = 'https://nodejs.org';
- const versions = [
- { num: '12.x' },
- { num: '11.x' },
- { num: '10.x', lts: true },
- { num: '9.x' },
- { num: '8.x', lts: true },
- { num: '7.x' },
- { num: '6.x' },
- { num: '5.x' },
- { num: '4.x' },
- { num: '0.12.x' },
- { num: '0.10.x' },
- ];
+ const versions = await getVersions.versions();
const getHref = (versionNum) =>
`${host}/docs/latest-v${versionNum}/api/${filename}.html`;
diff --git a/tools/doc/versions.js b/tools/doc/versions.js
new file mode 100644
index 0000000000..854329bd9a
--- /dev/null
+++ b/tools/doc/versions.js
@@ -0,0 +1,45 @@
+'use strict';
+
+let _versions;
+
+const getUrl = (url) => {
+ return new Promise((resolve, reject) => {
+ const https = require('https');
+ const request = https.get(url, (response) => {
+ if (response.statusCode !== 200) {
+ reject(new Error(
+ `Failed to get ${url}, status code ${response.statusCode}`));
+ }
+ response.setEncoding('utf8');
+ let body = '';
+ response.on('data', (data) => body += data);
+ response.on('end', () => resolve(body));
+ });
+ request.on('error', (err) => reject(err));
+ });
+};
+
+module.exports = {
+ async versions() {
+ if (_versions) {
+ return _versions;
+ }
+
+ // The CHANGELOG.md on release branches may not reference newer semver
+ // majors of Node.js so fetch and parse the version from the master branch.
+ const githubContentUrl = 'https://raw.githubusercontent.com/nodejs/node/';
+ const changelog = await getUrl(`${githubContentUrl}/master/CHANGELOG.md`);
+ const ltsRE = /Long Term Support/i;
+ const versionRE = /\* \[Node\.js ([0-9.]+)\][^-—]+[-—]\s*(.*)\n/g;
+ _versions = [];
+ let match;
+ while ((match = versionRE.exec(changelog)) != null) {
+ const entry = { num: `${match[1]}.x` };
+ if (ltsRE.test(match[2])) {
+ entry.lts = true;
+ }
+ _versions.push(entry);
+ }
+ return _versions;
+ }
+};