@@ -11,6 +11,25 @@ type Options = {
1111
1212const 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 } ) ;
0 commit comments