File tree Expand file tree Collapse file tree 1 file changed +9
-8
lines changed Expand file tree Collapse file tree 1 file changed +9
-8
lines changed Original file line number Diff line number Diff line change @@ -16,14 +16,15 @@ export function resolveMarkdownRelativeLinks(
1616 if ( path . startsWith ( "http" ) || path . startsWith ( "https" ) ) {
1717 return match ;
1818 }
19- // match a link (e.g. [./example](./example), will replace the link, not the text)
20- if ( url ) {
21- return match . replace (
22- `(${ path } )` ,
23- `(${ cdnBaseURL } /${ path . replace ( / ^ \. \/ / , "" ) } )` ,
24- ) ;
25- }
26- return match . replace ( path , `${ cdnBaseURL } /${ path . replace ( / ^ \. \/ / , "" ) } ` ) ;
19+ /**
20+ * RegExp is used to avoid replacing texts in markdown links and targets only `href` and `src` attributes
21+ * @example [link](./image.png) => [link](https://cdn.com/image.png)
22+ * @example [./src/file.ts](./src/file.ts) => [./src/file.ts](https://cdn.com/src/file.ts)
23+ */
24+ const searchRegExp = new RegExp ( `(?<before>[^[])(?<url>${ path } )` , "g" ) // [^[] matches any character except [
25+ return match . replace ( searchRegExp , ( _ , before , url ) => {
26+ return `${ before } ${ cdnBaseURL } /${ url . replace ( / ^ \. \/ / , "" ) } `
27+ } ) ;
2728 } ,
2829 ) ;
2930}
You can’t perform that action at this time.
0 commit comments