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/drop-positions.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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));
});
});
});
9 changes: 9 additions & 0 deletions packages/core/src/generators/metadata/utils/parse.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}

Comment on lines +158 to +166

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a more intuitive way to do this (a double for loop w/ an if seems annoying, iMO)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was clunky. It's a single visit(content) per entry now, since the heading and stability are already part of the content. The test checks those two as well.

return metadataCollection;
};
Loading