Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gregsdennis/external links #1568

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
18 changes: 18 additions & 0 deletions remark/rehype-link-transformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { existsSync } from "fs";
import { visit } from "unist-util-visit";
import url from "url";


const rehypeLinkTransformer = () => (tree, vfile) => {
visit(tree, "element", (node) => {
if (node.tagName === "a") {
const href = url.parse(node.properties.href);
if (href.hostname === null && href.pathname?.endsWith(".md") && existsSync(vfile.history[0])) {
href.pathname = href.pathname.replace(/.md$/, ".html");
node.properties.href = url.format(href);
}
}
});
};

export default rehypeLinkTransformer;
2 changes: 2 additions & 0 deletions specs/.remarkrc-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import remarkRehype from "remark-rehype";
import rehypeStringify from "rehype-stringify";
import rehypeDocument from "rehype-document";
import specsPreset from "./.remarkrc-specs.js";
import rehypeLinkTransformer from "../remark/rehype-link-transformer.js";


dotenv.config();
Expand All @@ -15,6 +16,7 @@ export default {
specsPreset,
remarkTorchLight,
remarkRehype,
rehypeLinkTransformer,
[rehypeDocument, {
css: ["https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css"],
style: readFileSync(resolve(import.meta.dirname, "spec.css"), "utf8")
Expand Down
Loading
Loading