Skip to content

Commit 5598f92

Browse files
authored
fix(legacy-html): keep $ sequences in template values as-is (#1186)
1 parent e3b0793 commit 5598f92

5 files changed

Lines changed: 93 additions & 19 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 template value replacement treating `$` sequences (such as `$$` in Makefile snippets) as special replacement patterns, which corrupted the rendered output.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
const template = '<main>__CONTENT__</main><nav>__GTOC__</nav>';
11+
12+
const config = { version: { version: '1.0.0' }, changelog: [] };
13+
14+
const createValues = overrides => ({
15+
path: '/deprecations',
16+
api: 'deprecations',
17+
added: 'v1.0.0',
18+
section: 'Deprecations',
19+
toc: '',
20+
nav: '',
21+
content: '',
22+
...overrides,
23+
});
24+
25+
describe('replaceTemplateValues', () => {
26+
before(() => setConfig({}));
27+
28+
it('inserts content containing `$$` as-is', () => {
29+
const content = 'dirname "$$(command -v node)"';
30+
31+
const output = replaceTemplateValues(
32+
template,
33+
createValues({ content }),
34+
config,
35+
{ skipGitHub: true }
36+
);
37+
38+
assert.strictEqual(output, `<main>${content}</main><nav></nav>`);
39+
});
40+
41+
it("inserts content containing `$&`, `$'` and `` $` `` as-is", () => {
42+
const content = "x $& y $' z $` w";
43+
44+
const output = replaceTemplateValues(
45+
template,
46+
createValues({ content }),
47+
config,
48+
{ skipGitHub: true }
49+
);
50+
51+
assert.strictEqual(output, `<main>${content}</main><nav></nav>`);
52+
});
53+
54+
it('inserts nav containing `$` sequences as-is', () => {
55+
const nav = "<a href='$&'>$'</a>";
56+
57+
const output = replaceTemplateValues(
58+
template,
59+
createValues({ nav }),
60+
config,
61+
{ skipGitHub: true }
62+
);
63+
64+
assert.strictEqual(output, `<main></main><nav>${nav}</nav>`);
65+
});
66+
});

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ export const replaceTemplateValues = (
2525
{ skipGitHub = false, skipGtocPicker = false } = {}
2626
) => {
2727
return apiTemplate
28-
.replace('__ID__', api)
29-
.replace(/__FILENAME__/g, api)
30-
.replace('__SECTION__', section)
31-
.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__',
28+
.replace('__ID__', () => api)
29+
.replace(/__FILENAME__/g, () => api)
30+
.replace('__SECTION__', () => section)
31+
.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, () =>
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)