11import type { Transformer } from 'unified' ;
2- import type { Root , Text } from 'mdast' ;
2+ import type { Root , Text , Parent } from 'mdast' ;
33
4- async function processAuthors ( text : Text ) {
4+ async function processAuthors ( text : Text , index : number , parent : Parent ) {
55 let children = [ ] ;
66 let lastIndex = 0 ;
77 let match ;
@@ -10,7 +10,7 @@ async function processAuthors(text: Text) {
1010 while ( ( match = authorRegex . exec ( text . value ) ) !== null ) {
1111 if ( match . index > lastIndex ) {
1212 children . push ( {
13- type : text . type ,
13+ type : ' text' ,
1414 value : text . value . slice ( lastIndex , match . index )
1515 } ) ;
1616 }
@@ -21,7 +21,7 @@ async function processAuthors(text: Text) {
2121
2222 if ( authors . length > 0 ) {
2323 children . push ( {
24- type : 'mdxJsxFlowElement ' ,
24+ type : 'mdxJsxTextElement ' ,
2525 name : 'GithubAuthors' ,
2626 attributes : [
2727 {
@@ -39,18 +39,17 @@ async function processAuthors(text: Text) {
3939 if ( lastIndex > 0 ) {
4040 if ( lastIndex < text . value . length ) {
4141 children . push ( {
42- type : text . type ,
42+ type : ' text' ,
4343 value : text . value . slice ( lastIndex )
4444 } ) ;
4545 }
4646
4747 if ( children . length > 0 ) {
48- const node : Root = text as unknown as Root ;
49- node . type = 'root' ;
50- node . children = children ;
48+ parent . children . splice ( index , 1 , ...children ) ;
5149 }
5250 }
5351}
52+
5453export default function plugin ( ) : Transformer < Root > {
5554 return async ( tree ) => {
5655 const promises = [ ] ;
@@ -59,7 +58,7 @@ export default function plugin(): Transformer<Root> {
5958 if ( ! parent || index === undefined ) {
6059 return ;
6160 }
62- promises . push ( processAuthors ( node ) ) ;
61+ promises . push ( processAuthors ( node as Text , index , parent as Parent ) ) ;
6362 } ) ;
6463 await Promise . all ( promises ) ;
6564 } ;
0 commit comments