Skip to content

Commit cb6b9ed

Browse files
committed
feat(metadata): add support for standard YAML frontmatter blocks
1 parent 38c58bc commit cb6b9ed

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { strictEqual, deepStrictEqual } from 'node:assert';
22
import { describe, it } from 'node:test';
33

4-
import { parseYAMLIntoMetadata, normalizeYamlSyntax } from '../yaml.mjs';
4+
import {
5+
parseYAMLIntoMetadata,
6+
normalizeYamlSyntax,
7+
extractYamlContent,
8+
} from '../yaml.mjs';
59

610
describe('normalizeYamlSyntax', () => {
711
it('should normalize YAML syntax by fixing noncompliant properties', () => {
@@ -65,3 +69,18 @@ describe('parseYAMLIntoMetadata', () => {
6569
deepStrictEqual(parseYAMLIntoMetadata(input), expectedOutput);
6670
});
6771
});
72+
73+
describe('extractYamlContent', () => {
74+
it('should extract content from a legacy HTML comment node', () => {
75+
const node = {
76+
type: 'html',
77+
value: '<!-- YAML\nintroduced_in=v1.0.0\n-->',
78+
};
79+
strictEqual(extractYamlContent(node).trim(), 'introduced_in=v1.0.0');
80+
});
81+
82+
it('should extract content directly from a standard YAML node', () => {
83+
const node = { type: 'yaml', value: 'introduced_in: v1.0.0' };
84+
strictEqual(extractYamlContent(node), 'introduced_in: v1.0.0');
85+
});
86+
});

src/generators/metadata/utils/yaml.mjs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ import { QUERIES } from '../../../utils/queries/index.mjs';
77
/**
88
* Extracts raw YAML content from a node
99
*
10-
* @param {import('mdast').Node} node A HTML node containing the YAML content
10+
* @param {import('mdast').Node} node A HTML or YAML node containing the YAML content
1111
* @returns {string} The extracted raw YAML content
1212
*/
1313
export const extractYamlContent = node => {
14-
return node.value.replace(
15-
QUERIES.yamlInnerContent,
16-
// Either capture a YAML multinline block, or a simple single-line YAML block
17-
(_, simple, yaml) => simple || yaml
18-
);
14+
return node.type === 'yaml'
15+
? node.value
16+
: node.value.replace(
17+
QUERIES.yamlInnerContent,
18+
// Either capture a YAML multinline block, or a simple single-line YAML block
19+
(_, simple, yaml) => simple || yaml
20+
);
1921
};
2022

2123
/**

src/utils/queries/index.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ export const UNIST = {
3434
QUERIES.stabilityIndex.test(transformNodesToString(children)),
3535

3636
/**
37-
* @param {import('@types/mdast').Html} html
37+
* @param {import('@types/mdast').Html | import('mdast').Node} node
3838
* @returns {boolean}
3939
*/
4040
isYamlNode: ({ type, value }) =>
41-
type === 'html' && QUERIES.yamlInnerContent.test(value),
41+
type === 'yaml' ||
42+
(type === 'html' && QUERIES.yamlInnerContent.test(value)),
4243

4344
/**
4445
* @param {import('@types/mdast').Text} text

0 commit comments

Comments
 (0)