@@ -21,7 +21,7 @@ const getKeywordRanges = (
2121 const ranges : { start : number ; end : number } [ ] = [ ]
2222 // This regex is a combination of all keyword types
2323 const regex =
24- / ` ( [ ^ \s ` ] * \. [ ^ \s ` ] + ) ` | ( # C h a n g e s : [ ^ \s , ; : ! ? ] + ) | ( # S e l e c t i o n ) | ( # S a v e d C o n t e x t : (?: W o r k s p a c e S t a t e | J S O N ) \s + " [ ^ " ] + " ) | ( # (?: C o m m i t | C o n t e x t A t C o m m i t ) : [ ^ : ] + : [ ^ \s " ] + \s + " [ ^ " ] * " ) / g
24+ / ` ( [ ^ \s ` ] * \. [ ^ \s ` ] + ) ` | ( # C h a n g e s : [ ^ \s , ; : ! ? ] + ) | ( # S e l e c t i o n ) | ( # S a v e d C o n t e x t : (?: W o r k s p a c e S t a t e | J S O N ) \s + " [ ^ " ] + " ) | ( # (?: C o m m i t | C o n t e x t A t C o m m i t ) : [ ^ : ] + : [ ^ \s " ] + \s + " (?: \\ . | [ ^ " \\ ] ) * " ) / g
2525
2626 let match
2727 while ( ( match = regex . exec ( text ) ) !== null ) {
@@ -48,25 +48,33 @@ const reconstruct_raw_value_from_node = (node: Node): string => {
4848
4949 if ( node . nodeType === Node . ELEMENT_NODE ) {
5050 const el = node as HTMLElement
51+ let inner_content = ''
52+ for ( const child of Array . from ( el . childNodes ) ) {
53+ inner_content += reconstruct_raw_value_from_node ( child )
54+ }
5155
5256 switch ( el . dataset . type ) {
5357 case 'file-keyword' : {
5458 const path = el . dataset . path
5559 if ( ! path ) return ''
5660 const filename = path . split ( '/' ) . pop ( ) || path
57- if ( el . textContent ?. startsWith ( filename ) ) {
58- const extra = el . textContent . substring ( filename . length )
59- return `\`${ path } \`${ extra } `
61+ const index = inner_content . indexOf ( filename )
62+ if ( index !== - 1 ) {
63+ const prefix = inner_content . substring ( 0 , index )
64+ const suffix = inner_content . substring ( index + filename . length )
65+ return `${ prefix } \`${ path } \`${ suffix } `
6066 }
6167 break
6268 }
6369 case 'changes-keyword' : {
6470 const branchName = el . dataset . branchName
6571 if ( ! branchName ) return ''
6672 const expected_text = `Diff with ${ branchName } `
67- if ( el . textContent ?. startsWith ( expected_text ) ) {
68- const extra = el . textContent . substring ( expected_text . length )
69- return `#Changes:${ branchName } ${ extra } `
73+ const index = inner_content . indexOf ( expected_text )
74+ if ( index !== - 1 ) {
75+ const prefix = inner_content . substring ( 0 , index )
76+ const suffix = inner_content . substring ( index + expected_text . length )
77+ return `${ prefix } #Changes:${ branchName } ${ suffix } `
7078 }
7179 break
7280 }
@@ -75,17 +83,21 @@ const reconstruct_raw_value_from_node = (node: Node): string => {
7583 const contextName = el . dataset . contextName
7684 if ( ! contextType || ! contextName ) return ''
7785 const expected_text = `Context "${ contextName } "`
78- if ( el . textContent ?. startsWith ( expected_text ) ) {
79- const extra = el . textContent . substring ( expected_text . length )
80- return `#SavedContext:${ contextType } "${ contextName } "${ extra } `
86+ const index = inner_content . indexOf ( expected_text )
87+ if ( index !== - 1 ) {
88+ const prefix = inner_content . substring ( 0 , index )
89+ const suffix = inner_content . substring ( index + expected_text . length )
90+ return `${ prefix } #SavedContext:${ contextType } "${ contextName } "${ suffix } `
8191 }
8292 break
8393 }
8494 case 'selection-keyword' : {
8595 const expected_text = 'Selection'
86- if ( el . textContent ?. startsWith ( expected_text ) ) {
87- const extra = el . textContent . substring ( expected_text . length )
88- return `#Selection${ extra } `
96+ const index = inner_content . indexOf ( expected_text )
97+ if ( index !== - 1 ) {
98+ const prefix = inner_content . substring ( 0 , index )
99+ const suffix = inner_content . substring ( index + expected_text . length )
100+ return `${ prefix } #Selection${ suffix } `
89101 }
90102 break
91103 }
@@ -97,9 +109,14 @@ const reconstruct_raw_value_from_node = (node: Node): string => {
97109 return ''
98110 }
99111 const short_hash = commit_hash . substring ( 0 , 7 )
100- if ( el . textContent ?. startsWith ( short_hash ) ) {
101- const extra = el . textContent . substring ( short_hash . length )
102- return `#Commit:${ repo_name } :${ commit_hash } "${ commit_message } "${ extra } `
112+ const index = inner_content . indexOf ( short_hash )
113+ if ( index !== - 1 ) {
114+ const prefix = inner_content . substring ( 0 , index )
115+ const suffix = inner_content . substring ( index + short_hash . length )
116+ return `${ prefix } #Commit:${ repo_name } :${ commit_hash } "${ commit_message . replace (
117+ / " / g,
118+ '\\"'
119+ ) } "${ suffix } `
103120 }
104121 break
105122 }
@@ -111,23 +128,20 @@ const reconstruct_raw_value_from_node = (node: Node): string => {
111128 return ''
112129 }
113130 const short_hash = commit_hash . substring ( 0 , 7 )
114- if ( el . textContent ?. startsWith ( short_hash ) ) {
115- const extra = el . textContent . substring ( short_hash . length )
116- return `#ContextAtCommit:${ repo_name } :${ commit_hash } "${ commit_message } "${ extra } `
131+ const index = inner_content . indexOf ( short_hash )
132+ if ( index !== - 1 ) {
133+ const prefix = inner_content . substring ( 0 , index )
134+ const suffix = inner_content . substring ( index + short_hash . length )
135+ return `${ prefix } #ContextAtCommit:${ repo_name } :${ commit_hash } "${ commit_message . replace (
136+ / " / g,
137+ '\\"'
138+ ) } "${ suffix } `
117139 }
118140 break
119141 }
120142 }
121143
122- if ( el . childNodes . length > 0 ) {
123- let content = ''
124- for ( const child of Array . from ( el . childNodes ) ) {
125- content += reconstruct_raw_value_from_node ( child )
126- }
127- return content
128- }
129-
130- return el . textContent || ''
144+ return inner_content
131145 }
132146
133147 return ''
@@ -292,7 +306,7 @@ export const use_handlers = (
292306 const commit_message = keyword_element . dataset . commitMessage
293307 if ( ! repo_name || ! commit_hash || commit_message === undefined ) return
294308
295- const search_pattern = `#Commit:${ repo_name } :${ commit_hash } "${ commit_message } "`
309+ const search_pattern = `#Commit:${ repo_name } :${ commit_hash } "${ commit_message . replace ( / " / g , '\\"' ) } "`
296310 const start_index = props . value . indexOf ( search_pattern )
297311
298312 if ( start_index !== - 1 ) {
@@ -309,7 +323,7 @@ export const use_handlers = (
309323 const commit_message = keyword_element . dataset . commitMessage
310324 if ( ! repo_name || ! commit_hash || commit_message === undefined ) return
311325
312- const search_pattern = `#ContextAtCommit:${ repo_name } :${ commit_hash } "${ commit_message } "`
326+ const search_pattern = `#ContextAtCommit:${ repo_name } :${ commit_hash } "${ commit_message . replace ( / " / g , '\\"' ) } "`
313327 const start_index = props . value . indexOf ( search_pattern )
314328
315329 if ( start_index !== - 1 ) {
@@ -544,7 +558,7 @@ export const use_handlers = (
544558 ) : boolean => {
545559 const text_before_cursor = props . value . substring ( 0 , raw_pos )
546560 const match = text_before_cursor . match (
547- / # (?: C o m m i t | C o n t e x t A t C o m m i t ) : [ ^ : ] + : [ ^ \s " ] + \s + " [ ^ " ] * " $ /
561+ / # (?: C o m m i t | C o n t e x t A t C o m m i t ) : [ ^ : ] + : [ ^ \s " ] + \s + " (?: \\ . | [ ^ " \\ ] ) * " $ /
548562 )
549563
550564 if ( match ) {
0 commit comments