@@ -14,6 +14,8 @@ interface ParentNode extends Node {
1414interface CustomElementData extends ElementData {
1515 hProperties ?: {
1616 tooltip ?: string ;
17+ textBefore ?: string ;
18+ textAfter ?: string ;
1719 } ;
1820}
1921
@@ -28,59 +30,77 @@ export function remarkInfoTooltip() {
2830 ) ;
2931
3032 if ( matches ) {
33+ let lastIndex = 0 ;
34+ const newChildren : Node [ ] = [ ] ;
35+
3136 matches . forEach ( match => {
3237 const tooltipMatch = match . match (
3338 / \{ t o o l t i p : ( [ ^ } ] + ) \} ( .* ?) \{ \/ t o o l t i p \} /
3439 ) ;
3540 if ( tooltipMatch ) {
3641 const [ , tooltipContent , text ] = tooltipMatch ;
37- const index = node . value . indexOf ( match ) ;
38- if ( index !== - 1 ) {
39- // Create a new element node
40- const elementNode : Element = {
41- type : 'element' ,
42- tagName : 'div' ,
43- properties : {
42+ const index = node . value . indexOf ( match , lastIndex ) ;
43+
44+ // Add text before the tooltip
45+ const textBefore = node . value . slice (
46+ lastIndex ,
47+ index
48+ ) ;
49+ if ( textBefore ) {
50+ const textNode : TextNode = {
51+ type : 'text' ,
52+ value : textBefore ,
53+ } ;
54+ newChildren . push ( textNode ) ;
55+ }
56+
57+ // Create a new element node for the tooltip
58+ const elementNode : Element = {
59+ type : 'element' ,
60+ tagName : 'span' ,
61+ properties : {
62+ tooltip : tooltipContent ,
63+ } ,
64+ data : {
65+ hProperties : {
4466 tooltip : tooltipContent ,
45- // textBefore: node.value.slice(0, index),
46- // textAfter: node.value.slice(
47- // index + match.length
48- // ),
4967 } ,
50- data : {
51- hProperties : {
52- tooltip : tooltipContent ,
53- textBefore : node . value . slice (
54- 0 ,
55- index
56- ) ,
57- textAfter : node . value . slice (
58- index + match . length
59- ) ,
60- } ,
61- } as CustomElementData ,
62- children : [
63- {
64- type : 'text' ,
65- value : text ,
66- } ,
67- ] ,
68- } ;
68+ } as CustomElementData ,
69+ children : [
70+ {
71+ type : 'text' ,
72+ value : text ,
73+ } as TextNode ,
74+ ] ,
75+ } ;
76+ newChildren . push ( elementNode ) ;
6977
70- // Replace the text node with the element node
71- if ( ancestors . length > 0 ) {
72- const parent =
73- ancestors [ ancestors . length - 1 ] ;
74- const nodeIndex =
75- parent . children . indexOf ( node ) ;
76- if ( nodeIndex !== - 1 ) {
77- parent . children [ nodeIndex ] =
78- elementNode ;
79- }
80- }
81- }
78+ lastIndex = index + match . length ;
8279 }
8380 } ) ;
81+
82+ // Add any remaining text after the last tooltip
83+ const remainingText = node . value . slice ( lastIndex ) ;
84+ if ( remainingText ) {
85+ const textNode : TextNode = {
86+ type : 'text' ,
87+ value : remainingText ,
88+ } ;
89+ newChildren . push ( textNode ) ;
90+ }
91+
92+ // Replace the text node with the new children
93+ if ( ancestors . length > 0 ) {
94+ const parent = ancestors [ ancestors . length - 1 ] ;
95+ const nodeIndex = parent . children . indexOf ( node ) ;
96+ if ( nodeIndex !== - 1 ) {
97+ parent . children . splice (
98+ nodeIndex ,
99+ 1 ,
100+ ...newChildren
101+ ) ;
102+ }
103+ }
84104 }
85105 }
86106 ) ;
0 commit comments