diff --git a/.changeset/drop-positions.md b/.changeset/drop-positions.md new file mode 100644 index 000000000..6d2e789e0 --- /dev/null +++ b/.changeset/drop-positions.md @@ -0,0 +1,5 @@ +--- +'@doc-kit/core': minor +--- + +The nodes of `metadata`'s entries no longer keep their `position` in the source once they are parsed, since nothing after `metadata` reads it. Builds hold less in memory and are faster, and the output of `json-simple` no longer has them either, now about a third of its size. diff --git a/packages/core/src/generators/metadata/utils/__tests__/parse.test.mjs b/packages/core/src/generators/metadata/utils/__tests__/parse.test.mjs index 4458f5d24..17872567e 100644 --- a/packages/core/src/generators/metadata/utils/__tests__/parse.test.mjs +++ b/packages/core/src/generators/metadata/utils/__tests__/parse.test.mjs @@ -4,6 +4,7 @@ import assert from 'node:assert/strict'; import { describe, it } from 'node:test'; import { u } from 'unist-builder'; +import { visit } from 'unist-util-visit'; import { parseApiDoc } from '../parse.mjs'; @@ -338,4 +339,36 @@ describe('parseApiDoc', () => { assert.strictEqual(secondContent[0].type, 'heading'); }); }); + + describe('source positions', () => { + it('drops where each node of an entry came from', () => { + const tree = u('root', [ + h('fs'), + stability('Stability: 2 - Stable'), + u('paragraph', [u('text', 'Content.')]), + ]); + const position = { + start: { line: 1, column: 1 }, + end: { line: 1, column: 1 }, + }; + + visit(tree, node => { + node.position = position; + }); + + const [entry] = parseApiDoc({ path, tree }, typeMap); + const positioned = []; + + visit(entry.content, node => { + if ('position' in node) { + positioned.push(node.type); + } + }); + + assert.strictEqual(entry.content.children.length, 3); + assert.deepStrictEqual(positioned, []); + assert.ok(!('position' in entry.heading)); + assert.ok(entry.stability && !('position' in entry.stability)); + }); + }); }); diff --git a/packages/core/src/generators/metadata/utils/parse.mjs b/packages/core/src/generators/metadata/utils/parse.mjs index c3eb41fb5..df81dd243 100644 --- a/packages/core/src/generators/metadata/utils/parse.mjs +++ b/packages/core/src/generators/metadata/utils/parse.mjs @@ -155,5 +155,14 @@ export const parseApiDoc = ({ path, tree, mdx = false }, typeMap) => { return SKIP; }); + // Nothing after this reads where a node came from: letting it go saves + // memory, and copying it to and from the worker threads. An entry's heading + // and stability are nodes of its content + for (const { content } of metadataCollection) { + visit(content, node => { + delete node.position; + }); + } + return metadataCollection; };