Skip to content

Commit d9d541d

Browse files
committed
tools,doc: enable changelogs for items
PR-URL: #11489 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Italo A. Casas <me@italoacasas.com>
1 parent 8fdb6c2 commit d9d541d

File tree

6 files changed

+77
-6
lines changed

6 files changed

+77
-6
lines changed

doc/api_assets/style.css

+6
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,12 @@ th > *:last-child, td > *:last-child {
470470
margin-bottom: 0;
471471
}
472472

473+
.changelog > summary {
474+
margin: .5rem 0;
475+
padding: .5rem 0;
476+
cursor: pointer;
477+
}
478+
473479
/* simpler clearfix */
474480
.clearfix:after {
475481
content: ".";

test/doctool/test-doctool-html.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ const testData = [
4747
'<p>Describe <code>Foobar</code> in more detail here.</p>' +
4848
'<h2>Foobar II<span><a class="mark" href="#foo_foobar_ii" ' +
4949
'id="foo_foobar_ii">#</a></span></h2>' +
50-
'<div class="api_metadata"><span>Added in: v5.3.0, v4.2.0</span></div> ' +
50+
'<div class="api_metadata">' +
51+
'<details class="changelog"><summary>History</summary>' +
52+
'<table><tr><th>Version</th><th>Changes</th></tr>' +
53+
'<tr><td>v5.3.0, v4.2.0</td>' +
54+
'<td><p><span>Added in: v5.3.0, v4.2.0</span></p>' +
55+
'</td></tr>' +
56+
'<tr><td>v4.2.0</td><td><p>The <code>error</code> parameter can now be' +
57+
'an arrow function.</p></td></tr></table></details>' +
58+
'</div> ' +
5159
'<p>Describe <code>Foobar II</code> in more detail here.</p>' +
5260
'<h2>Deprecated thingy<span><a class="mark" ' +
5361
'href="#foo_deprecated_thingy" id="foo_deprecated_thingy">#</a>' +

test/doctool/test-doctool-json.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ const testData = [
8989
textRaw: 'Foobar',
9090
name: 'foobar',
9191
meta: {
92-
added: ['v1.0.0']
92+
added: ['v1.0.0'],
93+
changes: []
9394
},
9495
desc: '<p>Describe <code>Foobar</code> in more detail ' +
9596
'here.</p>\n',
@@ -100,7 +101,14 @@ const testData = [
100101
textRaw: 'Foobar II',
101102
name: 'foobar_ii',
102103
meta: {
103-
added: ['v5.3.0', 'v4.2.0']
104+
added: ['v5.3.0', 'v4.2.0'],
105+
changes: [
106+
{ version: 'v4.2.0',
107+
'pr-url': 'https://github.com/nodejs/node/pull/3276',
108+
description: 'The `error` parameter can now be ' +
109+
'an arrow function.'
110+
}
111+
]
104112
},
105113
desc: '<p>Describe <code>Foobar II</code> in more detail ' +
106114
'here.</p>\n',
@@ -112,7 +120,8 @@ const testData = [
112120
name: 'deprecated_thingy',
113121
meta: {
114122
added: ['v1.0.0'],
115-
deprecated: ['v2.0.0']
123+
deprecated: ['v2.0.0'],
124+
changes: []
116125
},
117126
desc: '<p>Describe <code>Deprecated thingy</code> in more ' +
118127
'detail here.</p>\n',

test/fixtures/doc_with_yaml.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Describe `Foobar` in more detail here.
1212
added:
1313
- v5.3.0
1414
- v4.2.0
15+
changes:
16+
- version: v4.2.0
17+
pr-url: https://github.com/nodejs/node/pull/3276
18+
description: The `error` parameter can now be an arrow function.
1519
-->
1620

1721
Describe `Foobar II` in more detail here.

tools/doc/common.js

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ function extractAndParseYAML(text) {
3434
meta.deprecated = arrify(deprecated);
3535
}
3636

37+
meta.changes = meta.changes || [];
38+
meta.changes.forEach((entry) => {
39+
entry.description = entry.description.replace(/^\^\s*/, '');
40+
});
41+
3742
return meta;
3843
}
3944

tools/doc/html.js

+41-2
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,40 @@ function parseYAML(text) {
269269
const meta = common.extractAndParseYAML(text);
270270
const html = ['<div class="api_metadata">'];
271271

272+
const added = { description: '' };
273+
const deprecated = { description: '' };
274+
272275
if (meta.added) {
273-
html.push(`<span>Added in: ${meta.added.join(', ')}</span>`);
276+
added.version = meta.added.join(', ');
277+
added.description = `<span>Added in: ${added.version}</span>`;
274278
}
275279

276280
if (meta.deprecated) {
277-
html.push(`<span>Deprecated since: ${meta.deprecated.join(', ')} </span>`);
281+
deprecated.version = meta.deprecated.join(', ');
282+
deprecated.description =
283+
`<span>Deprecated since: ${deprecated.version}</span>`;
284+
}
285+
286+
if (meta.changes.length > 0) {
287+
let changes = meta.changes.slice();
288+
if (added.description) changes.push(added);
289+
if (deprecated.description) changes.push(deprecated);
290+
291+
changes = changes.sort((a, b) => versionSort(a.version, b.version));
292+
293+
html.push('<details class="changelog"><summary>History</summary>');
294+
html.push('<table>');
295+
html.push('<tr><th>Version</th><th>Changes</th></tr>');
296+
297+
changes.forEach((change) => {
298+
html.push(`<tr><td>${change.version}</td>`);
299+
html.push(`<td>${marked(change.description)}</td></tr>`);
300+
});
301+
302+
html.push('</table>');
303+
html.push('</details>');
304+
} else {
305+
html.push(`${added.description}${deprecated.description}`);
278306
}
279307

280308
html.push('</div>');
@@ -390,3 +418,14 @@ function getId(text) {
390418
}
391419
return text;
392420
}
421+
422+
const numberRe = /^(\d*)/;
423+
function versionSort(a, b) {
424+
a = a.trim();
425+
b = b.trim();
426+
let i = 0; // common prefix length
427+
while (i < a.length && i < b.length && a[i] === b[i]) i++;
428+
a = a.substr(i);
429+
b = b.substr(i);
430+
return +b.match(numberRe)[1] - +a.match(numberRe)[1];
431+
}

0 commit comments

Comments
 (0)