Skip to content

Commit ae61234

Browse files
authored
🔄 Refactor admonition parsing to expose header function (#2244)
1 parent 80bc241 commit ae61234

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"myst-transforms": patch
3+
---
4+
5+
Expose admonition header test function

packages/myst-transforms/src/admonitions.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@ type Options = {
1111

1212
const githubAdmonitionKinds = ['note', 'tip', 'important', 'warning', 'caution'];
1313

14+
/**
15+
* Extract header children from a possible heading node (paragraph with strong text or heading)
16+
* This function can be used by other packages to get header content for directives
17+
*/
18+
export function getPossibleAdmonitionHeaderChildren(
19+
possibleHeading: FlowContent,
20+
): any[] | undefined {
21+
if (
22+
possibleHeading?.type === 'paragraph' &&
23+
possibleHeading.children?.length === 1 &&
24+
possibleHeading.children[0].type === 'strong'
25+
) {
26+
return possibleHeading.children[0].children;
27+
} else if (possibleHeading?.type === 'heading') {
28+
return possibleHeading.children;
29+
}
30+
return undefined;
31+
}
32+
1433
/**
1534
* Visit all admonitions and add headers if necessary
1635
*/
@@ -37,17 +56,10 @@ export function admonitionHeadersTransform(tree: GenericParent, opts?: Options)
3756
AdmonitionTitle,
3857
FlowContent,
3958
];
40-
if (
41-
possibleHeading?.type === 'paragraph' &&
42-
possibleHeading.children?.length === 1 &&
43-
possibleHeading.children[0].type === 'strong'
44-
) {
45-
const strongTextChildren = possibleHeading.children[0].children;
46-
admonitionHeader.children = strongTextChildren; // Replace the admonition text with the strong chidren
47-
node.children = [admonitionHeader, ...rest]; // remove the strong text
48-
} else if (possibleHeading?.type === 'heading') {
49-
admonitionHeader.children = possibleHeading.children; // Replace the admonition text with the heading chidren
50-
node.children = [admonitionHeader, ...rest]; // remove the strong text
59+
const headerChildren = getPossibleAdmonitionHeaderChildren(possibleHeading);
60+
if (headerChildren) {
61+
admonitionHeader.children = headerChildren; // Replace the admonition text with the header children
62+
node.children = [admonitionHeader, ...rest]; // remove the header text
5163
}
5264
}
5365
});

packages/myst-transforms/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export {
55
admonitionBlockquoteTransform,
66
admonitionQmdTransform,
77
admonitionQmdPlugin,
8+
getPossibleAdmonitionHeaderChildren,
89
} from './admonitions.js';
910
export { captionParagraphPlugin, captionParagraphTransform } from './caption.js';
1011
export { footnotesPlugin, footnotesTransform } from './footnotes.js';

0 commit comments

Comments
 (0)