Skip to content

Commit

Permalink
tools,doc: enable changelogs for items
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
addaleax committed Feb 24, 2017
1 parent 8fdb6c2 commit d9d541d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
6 changes: 6 additions & 0 deletions doc/api_assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,12 @@ th > *:last-child, td > *:last-child {
margin-bottom: 0;
}

.changelog > summary {
margin: .5rem 0;
padding: .5rem 0;
cursor: pointer;
}

/* simpler clearfix */
.clearfix:after {
content: ".";
Expand Down
10 changes: 9 additions & 1 deletion test/doctool/test-doctool-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ const testData = [
'<p>Describe <code>Foobar</code> in more detail here.</p>' +
'<h2>Foobar II<span><a class="mark" href="#foo_foobar_ii" ' +
'id="foo_foobar_ii">#</a></span></h2>' +
'<div class="api_metadata"><span>Added in: v5.3.0, v4.2.0</span></div> ' +
'<div class="api_metadata">' +
'<details class="changelog"><summary>History</summary>' +
'<table><tr><th>Version</th><th>Changes</th></tr>' +
'<tr><td>v5.3.0, v4.2.0</td>' +
'<td><p><span>Added in: v5.3.0, v4.2.0</span></p>' +
'</td></tr>' +
'<tr><td>v4.2.0</td><td><p>The <code>error</code> parameter can now be' +
'an arrow function.</p></td></tr></table></details>' +
'</div> ' +
'<p>Describe <code>Foobar II</code> in more detail here.</p>' +
'<h2>Deprecated thingy<span><a class="mark" ' +
'href="#foo_deprecated_thingy" id="foo_deprecated_thingy">#</a>' +
Expand Down
15 changes: 12 additions & 3 deletions test/doctool/test-doctool-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ const testData = [
textRaw: 'Foobar',
name: 'foobar',
meta: {
added: ['v1.0.0']
added: ['v1.0.0'],
changes: []
},
desc: '<p>Describe <code>Foobar</code> in more detail ' +
'here.</p>\n',
Expand All @@ -100,7 +101,14 @@ const testData = [
textRaw: 'Foobar II',
name: 'foobar_ii',
meta: {
added: ['v5.3.0', 'v4.2.0']
added: ['v5.3.0', 'v4.2.0'],
changes: [
{ version: 'v4.2.0',
'pr-url': 'https://github.com/nodejs/node/pull/3276',
description: 'The `error` parameter can now be ' +
'an arrow function.'
}
]
},
desc: '<p>Describe <code>Foobar II</code> in more detail ' +
'here.</p>\n',
Expand All @@ -112,7 +120,8 @@ const testData = [
name: 'deprecated_thingy',
meta: {
added: ['v1.0.0'],
deprecated: ['v2.0.0']
deprecated: ['v2.0.0'],
changes: []
},
desc: '<p>Describe <code>Deprecated thingy</code> in more ' +
'detail here.</p>\n',
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/doc_with_yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Describe `Foobar` in more detail here.
added:
- v5.3.0
- v4.2.0
changes:
- version: v4.2.0
pr-url: https://github.com/nodejs/node/pull/3276
description: The `error` parameter can now be an arrow function.
-->

Describe `Foobar II` in more detail here.
Expand Down
5 changes: 5 additions & 0 deletions tools/doc/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ function extractAndParseYAML(text) {
meta.deprecated = arrify(deprecated);
}

meta.changes = meta.changes || [];
meta.changes.forEach((entry) => {
entry.description = entry.description.replace(/^\^\s*/, '');
});

return meta;
}

Expand Down
43 changes: 41 additions & 2 deletions tools/doc/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,40 @@ function parseYAML(text) {
const meta = common.extractAndParseYAML(text);
const html = ['<div class="api_metadata">'];

const added = { description: '' };
const deprecated = { description: '' };

if (meta.added) {
html.push(`<span>Added in: ${meta.added.join(', ')}</span>`);
added.version = meta.added.join(', ');
added.description = `<span>Added in: ${added.version}</span>`;
}

if (meta.deprecated) {
html.push(`<span>Deprecated since: ${meta.deprecated.join(', ')} </span>`);
deprecated.version = meta.deprecated.join(', ');
deprecated.description =
`<span>Deprecated since: ${deprecated.version}</span>`;
}

if (meta.changes.length > 0) {
let changes = meta.changes.slice();
if (added.description) changes.push(added);
if (deprecated.description) changes.push(deprecated);

changes = changes.sort((a, b) => versionSort(a.version, b.version));

html.push('<details class="changelog"><summary>History</summary>');
html.push('<table>');
html.push('<tr><th>Version</th><th>Changes</th></tr>');

changes.forEach((change) => {
html.push(`<tr><td>${change.version}</td>`);
html.push(`<td>${marked(change.description)}</td></tr>`);
});

html.push('</table>');
html.push('</details>');
} else {
html.push(`${added.description}${deprecated.description}`);
}

html.push('</div>');
Expand Down Expand Up @@ -390,3 +418,14 @@ function getId(text) {
}
return text;
}

const numberRe = /^(\d*)/;
function versionSort(a, b) {
a = a.trim();
b = b.trim();
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)[1] - +a.match(numberRe)[1];
}

0 comments on commit d9d541d

Please sign in to comment.