-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathremove-markdown-extensions.ts
More file actions
43 lines (35 loc) · 1.04 KB
/
remove-markdown-extensions.ts
File metadata and controls
43 lines (35 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { visit } from "unist-util-visit";
import type { Node, Parent } from "unist";
import type { Plugin } from "unified";
interface Element extends Parent {
type: "link";
url: string;
children: Node[];
}
interface RemoveMarkdownExtensionsOptions {
ignoreRelativeLinks?: boolean;
}
const match = /(?:\/index)?\.(md|mdx)(#.*)?$/;
const removeMarkdownExtensions: Plugin = function ({
ignoreRelativeLinks = false,
}: RemoveMarkdownExtensionsOptions = {}) {
return (tree: Node) => {
visit(tree, "link", (node: Element) => {
// ignore relative links if configured
if (
ignoreRelativeLinks &&
(node.url.startsWith("./") || node.url.startsWith("../"))
) {
return;
}
if (match.test(node.url)) {
let date = new Date().toLocaleTimeString("en-US", { hour12: false });
console.log(
`\x1b[90m${date}\x1b[0m \x1b[95m[🔥 *.md(x)]\x1b[0m ${node.url}`,
);
node.url = node.url.replace(match, "$2");
}
});
};
};
export default removeMarkdownExtensions;