Skip to content

Commit 5b96711

Browse files
authored
feat: support standard YAML frontmatter via pre-AST replacement (#700)
* feat(metadata): add support for standard YAML frontmatter blocks * fix: resolve merge conflict in ast generator
1 parent 1992207 commit 5b96711

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

src/generators/ast/generate.mjs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ export async function processChunk(inputSlice, itemIndices) {
2727

2828
for (const [path, parent] of filePaths) {
2929
const content = await readFile(path, 'utf-8');
30-
const value = content.replace(
31-
QUERIES.stabilityIndexPrefix,
32-
match => `[${match}](${STABILITY_INDEX_URL})`
33-
);
30+
const value = content
31+
.replace(
32+
QUERIES.standardYamlFrontmatter,
33+
(_, yaml) => '<!-- YAML\n' + yaml + '\n-->\n'
34+
)
35+
.replace(
36+
QUERIES.stabilityIndexPrefix,
37+
match => `[${match}](${STABILITY_INDEX_URL})`
38+
);
3439

3540
const relativePath = sep + withExt(relative(parent, path));
3641

src/utils/queries/__tests__/index.test.mjs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
1-
import { strictEqual } from 'node:assert';
1+
import { strictEqual, ok } from 'node:assert';
22
import { describe, it } from 'node:test';
33

44
import { u as createTree } from 'unist-builder';
55

6-
import { UNIST } from '../index.mjs';
6+
import { QUERIES, UNIST } from '../index.mjs';
7+
8+
describe('QUERIES', () => {
9+
describe('standardYamlFrontmatter', () => {
10+
it('matches standard YAML frontmatter at the beginning of the text', () => {
11+
const content = '---\nintroduced_in: v1.0.0\ntype: module\n---';
12+
ok(QUERIES.standardYamlFrontmatter.test(content));
13+
14+
const match = QUERIES.standardYamlFrontmatter.exec(content);
15+
strictEqual(match[1], 'introduced_in: v1.0.0\ntype: module');
16+
});
17+
18+
it('matches standard YAML frontmatter with Windows line endings (CRLF)', () => {
19+
const content = '---\r\nintroduced_in: v1.0.0\r\n---';
20+
ok(QUERIES.standardYamlFrontmatter.test(content));
21+
22+
const match = QUERIES.standardYamlFrontmatter.exec(content);
23+
strictEqual(match[1], 'introduced_in: v1.0.0');
24+
});
25+
26+
it('does not match horizontal rules or dashes not at the start of the string', () => {
27+
const content = '# Hello\n\n---\n\nSome text';
28+
strictEqual(QUERIES.standardYamlFrontmatter.test(content), false);
29+
});
30+
31+
it('does not match unclosed frontmatter blocks', () => {
32+
const content = '---\nintroduced_in: v1.0.0\nSome text...';
33+
strictEqual(QUERIES.standardYamlFrontmatter.test(content), false);
34+
});
35+
});
36+
});
737

838
describe('UNIST', () => {
939
describe('isStronglyTypedList', () => {

src/utils/queries/index.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export const QUERIES = {
1818
stabilityIndexPrefix: /Stability: ([0-5](?:\.[0-3])?)/,
1919
// ReGeX for retrieving the inner content from a YAML block
2020
yamlInnerContent: /^<!--[ ]?(?:YAML([\s\S]*?)|([ \S]*?))?[ ]?-->/,
21+
// ReGeX for standard Markdown YAML frontmatter
22+
standardYamlFrontmatter: /^---\r?\n([\s\S]*?)\r?\n---/,
2123
// ReGeX for finding references to Unix manuals
2224
unixManualPage: /\b([a-z.]+)\((\d)([a-z]?)\)/g,
2325
// ReGeX for determing a typed list's non-property names

0 commit comments

Comments
 (0)