Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: update regexp
  • Loading branch information
Barbapapazes committed Jan 10, 2024
commit 39460fc398be4fd604bf2b1ca4648bb55f96d787
17 changes: 9 additions & 8 deletions utils/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ export function resolveMarkdownRelativeLinks(
if (path.startsWith("http") || path.startsWith("https")) {
return match;
}
// match a link (e.g. [./example](./example), will replace the link, not the text)
if (url) {
return match.replace(
`(${path})`,
`(${cdnBaseURL}/${path.replace(/^\.\//, "")})`,
);
}
return match.replace(path, `${cdnBaseURL}/${path.replace(/^\.\//, "")}`);
/**
* RegExp is used to avoid replacing texts in markdown links and targets only `href` and `src` attributes
* @example [link](./image.png) => [link](https://cdn.com/image.png)
* @example [./src/file.ts](./src/file.ts) => [./src/file.ts](https://cdn.com/src/file.ts)
*/
const searchRegExp = new RegExp(`(?<before>[^[])(?<url>${path})`, "g") // [^[] matches any character except [
return match.replace(searchRegExp, (_, before, url) => {
return `${before}${cdnBaseURL}/${url.replace(/^\.\//, "")}`
});
},
);
}