Skip to content

Commit 0cd7414

Browse files
committed
fix(legacy-html,man-page,sitemap): treat $$, $&, $` and $' in values as literal text
String.prototype.replace(placeholder, value) treats some $ sequences in a string replacement as patterns ($$ -> $, $& -> the match, $` / $' -> the template before/after the match). Page content, navigation, man page options/environment text, and sitemap URL fields are filled this way, so any of those sequences in the source produced broken output (e.g. the addons.md Makefile example $$(command -v node) rendered with one $ missing). Pass each value through a replacer function instead, since a function's return value is inserted as-is. Fixes nodejs#1181
1 parent e3b0793 commit 0cd7414

5 files changed

Lines changed: 81 additions & 16 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@node-core/doc-kit-legacy': patch
3+
'@node-core/doc-kit': patch
4+
'@doc-kit/generator-react': patch
5+
---
6+
7+
Fix `$$`, `$&`, `` $` `` and `$'` in page content being treated as `String.prototype.replace` replacement patterns instead of literal text. This affected the legacy HTML generator's page and navigation content, the man page generator's options/environment sections, and the sitemap generator's URL entries.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
import assert from 'node:assert/strict';
4+
import { before, describe, it } from 'node:test';
5+
6+
import { setConfig } from '@doc-kit/core/utils/configuration/index.mjs';
7+
8+
import { replaceTemplateValues } from '../replaceTemplateValues.mjs';
9+
10+
describe('replaceTemplateValues', () => {
11+
before(() => setConfig({}));
12+
13+
const baseValues = {
14+
path: '/test',
15+
api: 'test',
16+
added: undefined,
17+
section: 'section',
18+
toc: [],
19+
nav: 'nav',
20+
content: 'content',
21+
};
22+
23+
const config = { version: { version: '1.0.0' }, changelog: [] };
24+
25+
it('does not treat `$$`, `$&`, "$`" and `$\'` in content as replacement patterns', () => {
26+
const template = '<main>__CONTENT__</main><nav>__GTOC__</nav>';
27+
28+
const dollarContent = 'dirname "$$(command -v node)" $& $` $\'';
29+
30+
const output = replaceTemplateValues(
31+
template,
32+
{ ...baseValues, content: dollarContent },
33+
config,
34+
{ skipGitHub: true }
35+
);
36+
37+
assert.match(
38+
output,
39+
/<main>dirname "\$\$\(command -v node\)" \$& \$` \$'<\/main>/
40+
);
41+
});
42+
43+
it('does not treat `$$`, `$&`, "$`" and `$\'` in nav as replacement patterns', () => {
44+
const template = '<main>__CONTENT__</main><nav>__GTOC__</nav>';
45+
46+
const dollarNav = "nav $$ $& $` $'";
47+
48+
const output = replaceTemplateValues(
49+
template,
50+
{ ...baseValues, nav: dollarNav },
51+
config,
52+
{ skipGitHub: true }
53+
);
54+
55+
assert.match(output, /<nav>nav \$\$ \$& \$` \$'<\/nav>/);
56+
});
57+
});

‎packages/node-legacy/src/legacy-html/utils/replaceTemplateValues.mjs‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@ export const replaceTemplateValues = (
2727
return apiTemplate
2828
.replace('__ID__', api)
2929
.replace(/__FILENAME__/g, api)
30-
.replace('__SECTION__', section)
30+
.replace('__SECTION__', () => section)
3131
.replace(/__VERSION__/g, `v${config.version.version}`)
32-
.replace(/__TOC__/g, tableOfContents.wrapToC(toc))
33-
.replace(/__GTOC__/g, nav)
34-
.replace('__CONTENT__', content)
35-
.replace(/__TOC_PICKER__/g, buildToC(toc))
36-
.replace(/__GTOC_PICKER__/g, skipGtocPicker ? '' : buildNavigation(nav))
37-
.replace('__ALTDOCS__', buildVersions(path, added, config.changelog))
38-
.replace(
39-
'__EDIT_ON_GITHUB__',
32+
.replace(/__TOC__/g, () => tableOfContents.wrapToC(toc))
33+
.replace(/__GTOC__/g, () => nav)
34+
.replace('__CONTENT__', () => content)
35+
.replace(/__TOC_PICKER__/g, () => buildToC(toc))
36+
.replace(/__GTOC_PICKER__/g, () =>
37+
skipGtocPicker ? '' : buildNavigation(nav)
38+
)
39+
.replace('__ALTDOCS__', () => buildVersions(path, added, config.changelog))
40+
.replace('__EDIT_ON_GITHUB__', () =>
4041
skipGitHub
4142
? ''
4243
: buildGitHub(populate(config.editURL, { ...config, path }))

‎packages/node/src/man-page/generate.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ export async function generate(input) {
6868
const template = await readFile(config.templatePath, 'utf-8');
6969

7070
const filledTemplate = template
71-
.replace('__OPTIONS__', output.options)
72-
.replace('__ENVIRONMENT__', output.env);
71+
.replace('__OPTIONS__', () => output.options)
72+
.replace('__ENVIRONMENT__', () => output.env);
7373

7474
if (config.output) {
7575
await writeFile(join(config.output, config.fileName), filledTemplate);

‎packages/react/src/sitemap/generate.mjs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ export async function generate(entries) {
5050
const urlset = apiPages
5151
.map(page =>
5252
entryTemplate
53-
.replace('__LOC__', page.loc)
54-
.replace('__LASTMOD__', page.lastmod)
55-
.replace('__CHANGEFREQ__', page.changefreq)
56-
.replace('__PRIORITY__', page.priority)
53+
.replace('__LOC__', () => page.loc)
54+
.replace('__LASTMOD__', () => page.lastmod)
55+
.replace('__CHANGEFREQ__', () => page.changefreq)
56+
.replace('__PRIORITY__', () => page.priority)
5757
)
5858
.join('');
5959

60-
const sitemap = template.replace('__URLSET__', urlset);
60+
const sitemap = template.replace('__URLSET__', () => urlset);
6161

6262
if (config.output) {
6363
await writeFile(join(config.output, 'sitemap.xml'), sitemap, 'utf-8');

0 commit comments

Comments
 (0)