Skip to content

Commit e49eb52

Browse files
authored
fix(core): preserve shared metadata in json-simple (#1187)
Signed-off-by: dxnnystyles <[email protected]>
1 parent 5598f92 commit e49eb52

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

‎.changeset/flat-bushes-learn.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@doc-kit/core': patch
3+
---
4+
5+
Keep json-simple output generation from mutating shared metadata entries.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import assert from 'node:assert/strict';
2+
import { describe, it } from 'node:test';
3+
4+
import { setConfig } from '#utils/configuration/index.mjs';
5+
6+
import { generate } from '../generate.mjs';
7+
8+
const config = await setConfig({ target: ['json-simple'] });
9+
config['json-simple'].output = undefined;
10+
11+
describe('json-simple generate', () => {
12+
it('does not mutate input while removing headings and stability nodes', async () => {
13+
const heading = {
14+
type: 'heading',
15+
depth: 1,
16+
children: [{ type: 'text', value: 'fs' }],
17+
};
18+
const stability = {
19+
type: 'blockquote',
20+
children: [
21+
{
22+
type: 'paragraph',
23+
children: [{ type: 'text', value: 'Stability: 2 - Stable' }],
24+
},
25+
],
26+
};
27+
const paragraph = {
28+
type: 'paragraph',
29+
children: [{ type: 'text', value: 'The fs module.' }],
30+
};
31+
const input = [
32+
{
33+
heading,
34+
content: { type: 'root', children: [heading, stability, paragraph] },
35+
},
36+
];
37+
const snapshot = structuredClone(input);
38+
39+
const output = await generate(input);
40+
41+
assert.deepEqual(input, snapshot);
42+
assert.deepEqual(output[0].content.children, [paragraph]);
43+
assert.notEqual(output[0].content, input[0].content);
44+
});
45+
});

‎packages/core/src/generators/json-simple/generate.mjs‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@ import { UNIST } from '#utils/queries/index.mjs';
1616
export async function generate(input) {
1717
const config = getConfig('json-simple');
1818

19-
input.forEach(node =>
20-
remove(node.content, [UNIST.isStabilityNode, UNIST.isHeading])
21-
);
19+
const output = input.map(entry => {
20+
const copy = { ...entry, content: structuredClone(entry.content) };
21+
22+
remove(copy.content, [UNIST.isStabilityNode, UNIST.isHeading]);
23+
24+
return copy;
25+
});
2226

2327
if (config.output) {
24-
await writeJSON(join(config.output, 'api-docs.json'), input, config.minify);
28+
await writeJSON(
29+
join(config.output, 'api-docs.json'),
30+
output,
31+
config.minify
32+
);
2533
}
2634

27-
return input;
35+
return output;
2836
}

0 commit comments

Comments
 (0)