Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flat-bushes-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@doc-kit/core': patch
---

Keep json-simple output generation from mutating shared metadata entries.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';

import { setConfig } from '#utils/configuration/index.mjs';

import { generate } from '../generate.mjs';

const config = await setConfig({ target: ['json-simple'] });
config['json-simple'].output = undefined;

describe('json-simple generate', () => {
it('does not mutate input while removing headings and stability nodes', async () => {
const heading = {
type: 'heading',
depth: 1,
children: [{ type: 'text', value: 'fs' }],
};
const stability = {
type: 'blockquote',
children: [
{
type: 'paragraph',
children: [{ type: 'text', value: 'Stability: 2 - Stable' }],
},
],
};
const paragraph = {
type: 'paragraph',
children: [{ type: 'text', value: 'The fs module.' }],
};
const input = [
{
heading,
content: { type: 'root', children: [heading, stability, paragraph] },
},
];
const snapshot = structuredClone(input);

const output = await generate(input);

assert.deepEqual(input, snapshot);
assert.deepEqual(output[0].content.children, [paragraph]);
assert.notEqual(output[0].content, input[0].content);
});
});
18 changes: 13 additions & 5 deletions packages/core/src/generators/json-simple/generate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ import { UNIST } from '#utils/queries/index.mjs';
export async function generate(input) {
const config = getConfig('json-simple');

input.forEach(node =>
remove(node.content, [UNIST.isStabilityNode, UNIST.isHeading])
);
const output = input.map(entry => {
const copy = { ...entry, content: structuredClone(entry.content) };

remove(copy.content, [UNIST.isStabilityNode, UNIST.isHeading]);

return copy;
});

if (config.output) {
await writeJSON(join(config.output, 'api-docs.json'), input, config.minify);
await writeJSON(
join(config.output, 'api-docs.json'),
output,
config.minify
);
}

return input;
return output;
}
Loading