Skip to content

Commit 5002d79

Browse files
Add option to ignore relative links in removeMarkdownExtensions
Introduce the `ignoreRelativeLinks` option to the `removeMarkdownExtensions` plugin, allowing users to bypass processing for relative links. Updated `astro.config.mjs` to enable this option by default.
1 parent 94283d8 commit 5002d79

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

astro.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export default defineConfig({
183183
}),
184184
],
185185
markdown: {
186-
remarkPlugins: [removeMarkdownExtensions],
186+
remarkPlugins: [[removeMarkdownExtensions, { ignoreRelativeLinks: true }]],
187187
rehypePlugins: [
188188
[
189189
rehypeAstroRelativeMarkdownLinks,

src/plugins/remove-markdown-extensions.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,22 @@ interface Element extends Parent {
88
children: Node[];
99
}
1010

11-
const removeMarkdownExtensions: Plugin = function () {
11+
interface RemoveMarkdownExtensionsOptions {
12+
ignoreRelativeLinks?: boolean;
13+
}
14+
15+
const match = /(?:\/index)?\.(md|mdx)(#.*)?$/;
16+
17+
const removeMarkdownExtensions: Plugin = function ({
18+
ignoreRelativeLinks = false,
19+
}: RemoveMarkdownExtensionsOptions = {}) {
1220
return (tree: Node) => {
1321
visit(tree, "link", (node: Element) => {
14-
const match = /(?:\/index)?\.(md|mdx)(#.*)?$/;
22+
// ignore relative links if configured
23+
if (ignoreRelativeLinks && node.url.startsWith("./")) {
24+
return;
25+
}
26+
1527
if (match.test(node.url)) {
1628
let date = new Date().toLocaleTimeString("en-US", { hour12: false });
1729

0 commit comments

Comments
 (0)