File tree Expand file tree Collapse file tree 3 files changed +31
-9
lines changed
generators/metadata/utils Expand file tree Collapse file tree 3 files changed +31
-9
lines changed Original file line number Diff line number Diff line change 11import { strictEqual , deepStrictEqual } from 'node:assert' ;
22import { 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
610describe ( '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+ } ) ;
Original file line number Diff line number Diff 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 */
1313export 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/**
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments