@@ -5,10 +5,9 @@ import type { Node } from 'unist';
5
5
type Plugin = ( ) => ( tree : any ) => void ;
6
6
7
7
/**
8
- * Rehype plugin to transform text nodes with ==highlight== and __underline__ syntax
9
- * to proper HTML elements.
8
+ * Rehype plugin to transform text nodes with ==highlight== syntax to proper HTML elements.
10
9
*/
11
- const rehypeHighlightUnderline : Plugin = ( ) => {
10
+ const rehypeHighlight : Plugin = ( ) => {
12
11
return ( tree : Node ) => {
13
12
// Process text nodes for special syntax
14
13
visit ( tree , 'text' , ( node : any , index : number | null , parent : any ) => {
@@ -17,23 +16,21 @@ const rehypeHighlightUnderline: Plugin = () => {
17
16
const { value } = node ;
18
17
if ( typeof value !== 'string' ) return ;
19
18
20
- // Match patterns for highlight and underline
19
+ // Match patterns for highlight
21
20
const highlightRegex = / = = ( .* ?) = = / g;
22
- const underlineRegex = / _ _ ( .* ?) _ _ / g;
23
21
24
22
// Check if we have any matches
25
- if ( ! highlightRegex . test ( value ) && ! underlineRegex . test ( value ) ) return ;
23
+ if ( ! highlightRegex . test ( value ) ) return ;
26
24
27
25
// Reset regex lastIndex
28
26
highlightRegex . lastIndex = 0 ;
29
- underlineRegex . lastIndex = 0 ;
30
27
31
28
// Process the text to create HTML elements
32
29
const parts = [ ] ;
33
30
let lastIndex = 0 ;
34
31
const text = value ;
35
32
36
- // First process highlights
33
+ // Process highlights
37
34
let match : any = highlightRegex . exec ( text ) ;
38
35
while ( match !== null ) {
39
36
// Add text before the match
@@ -58,40 +55,6 @@ const rehypeHighlightUnderline: Plugin = () => {
58
55
parts . push ( { type : 'text' , value : text . slice ( lastIndex ) } ) ;
59
56
}
60
57
61
- // If we found any highlights, replace the original node
62
- if ( parts . length > 0 ) {
63
- parent . children . splice ( index , 1 , ...parts ) ;
64
- return ;
65
- }
66
-
67
- // Otherwise, check for underlines
68
- lastIndex = 0 ;
69
- parts . length = 0 ;
70
-
71
- match = underlineRegex . exec ( text ) ;
72
- while ( match !== null ) {
73
- // Add text before the match
74
- if ( match . index > lastIndex ) {
75
- parts . push ( { type : 'text' , value : text . slice ( lastIndex , match . index ) } ) ;
76
- }
77
-
78
- // Add the underline element
79
- parts . push ( {
80
- type : 'element' ,
81
- tagName : 'u' ,
82
- properties : { } ,
83
- children : [ { type : 'text' , value : match [ 1 ] } ] ,
84
- } ) ;
85
-
86
- lastIndex = match . index + match [ 0 ] . length ;
87
- match = underlineRegex . exec ( text ) ;
88
- }
89
-
90
- // Add any remaining text
91
- if ( lastIndex < text . length ) {
92
- parts . push ( { type : 'text' , value : text . slice ( lastIndex ) } ) ;
93
- }
94
-
95
58
// Replace the original node with our processed parts
96
59
if ( parts . length > 0 ) {
97
60
parent . children . splice ( index , 1 , ...parts ) ;
@@ -100,4 +63,4 @@ const rehypeHighlightUnderline: Plugin = () => {
100
63
} ;
101
64
} ;
102
65
103
- export default rehypeHighlightUnderline ;
66
+ export default rehypeHighlight ;
0 commit comments