Skip to content

Commit 39460fc

Browse files
committed
fix: update regexp
1 parent 457880f commit 39460fc

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

utils/markdown.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)