Skip to content

Commit a9b6527

Browse files
authored
fix(metadata): ensure YAML frontmatter is captured when preceding first heading (#735)
1 parent 91081a3 commit a9b6527

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/generators/metadata/utils/__tests__/parse.test.mjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,28 @@ describe('parseApiDoc', () => {
239239
assert.strictEqual(results.length, 0);
240240
});
241241
});
242+
243+
describe('top-level nodes (YAML frontmatter)', () => {
244+
it('captures top-level frontmatter in the first entry', () => {
245+
const tree = u('root', [
246+
u('yaml', 'layout: home\ncontributors: [shams]'),
247+
h('First Heading'),
248+
u('paragraph', [u('text', 'First content.')]),
249+
h('Second Heading', 2),
250+
u('paragraph', [u('text', 'Second content.')]),
251+
]);
252+
const results = parseApiDoc({ path, tree }, typeMap);
253+
254+
assert.strictEqual(results.length, 2);
255+
256+
const firstContent = results[0].content.children;
257+
assert.strictEqual(firstContent.length, 3);
258+
assert.strictEqual(firstContent[0].type, 'yaml');
259+
assert.strictEqual(firstContent[1].type, 'heading');
260+
261+
const secondContent = results[1].content.children;
262+
assert.strictEqual(secondContent.length, 2);
263+
assert.strictEqual(secondContent[0].type, 'heading');
264+
});
265+
});
242266
});

src/generators/metadata/utils/parse.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ export const parseApiDoc = ({ path, tree }, typeMap) => {
102102
? tree.children.length
103103
: tree.children.indexOf(nextHeadingNode);
104104

105-
// Create subtree for this section
106-
const subTree = createTree('root', tree.children.slice(index, stop));
105+
// Create subtree for this section. If it's the first entry, we start from the
106+
// beginning of the tree to ensure top-level nodes (like YAML) are captured.
107+
const startIndex = metadataCollection.length === 0 ? 0 : index;
108+
const subTree = createTree('root', tree.children.slice(startIndex, stop));
107109

108110
visit(subTree, UNIST.isStabilityNode, node =>
109111
visitStability(node, ignoreStability ? undefined : metadata)

0 commit comments

Comments
 (0)