File tree Expand file tree Collapse file tree 4 files changed +12
-8
lines changed Expand file tree Collapse file tree 4 files changed +12
-8
lines changed Original file line number Diff line number Diff line change
1
+ import { escapeRegExp } from "./regexp" ;
2
+
3
+ export function getEntryPatternByHref ( href : string ) : RegExp {
4
+ const existingItemPattern = String . raw `^- \[.+\]\(${ escapeRegExp ( href ) } \).*$` ;
5
+ return new RegExp ( existingItemPattern , "m" ) ;
6
+ }
Original file line number Diff line number Diff line change 1
- import { escapeRegExp } from "./regexp " ;
1
+ import { getEntryPatternByHref } from "./markdown " ;
2
2
3
3
export function mergeContent ( newEntryHref : string , newEntry : string , existinContent : string ) : string {
4
- const existingItemPattern = String . raw `^- \[.+\]\(${ escapeRegExp ( newEntryHref ) } \).*$` ;
5
- const replaceResult = existinContent . replace ( new RegExp ( existingItemPattern , "m" ) , newEntry ) ;
4
+ const replaceResult = existinContent . replace ( getEntryPatternByHref ( newEntryHref ) , newEntry ) ;
6
5
return replaceResult === existinContent ? `${ newEntry } \n${ existinContent } ` : replaceResult ;
7
6
}
Original file line number Diff line number Diff line change 1
1
// ref: https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
2
- export function escapeRegExp ( string ) {
3
- return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ; // $& means the whole matched string
2
+ export function escapeRegExp ( input : string ) {
3
+ return input . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ; // $& means the whole matched string
4
4
}
Original file line number Diff line number Diff line change 1
1
import { fitTextareaToContent } from "../lib/utils/fit-textarea-to-content" ;
2
- import { escapeRegExp } from "../lib/utils/regexp " ;
2
+ import { getEntryPatternByHref } from "../lib/utils/markdown " ;
3
3
import type { FullModel } from "./model" ;
4
4
5
5
const $ = document . querySelector . bind ( document ) ;
@@ -101,8 +101,7 @@ export class View {
101
101
}
102
102
103
103
if ( href !== previousState . href || markdownString !== previousState . markdownString ) {
104
- const existingItemPattern = String . raw `^- \[.+\]\(${ escapeRegExp ( href ) } \).*$` ;
105
- const isExistingUrl = markdownString ?. match ( new RegExp ( existingItemPattern , "m" ) ) ;
104
+ const isExistingUrl = markdownString ?. match ( getEntryPatternByHref ( href ! ) ) ;
106
105
existingLinkMarker . hidden = ! isExistingUrl ;
107
106
}
108
107
You can’t perform that action at this time.
0 commit comments