Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/flo-bit/tiny-docs. quick setup, simple, minimalistic docs for your github project flo-bit.dev/tiny-docs
Something went wrong. Try again.
1.4 kB · 52 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253import type { AstroIntegration } from "astro";import { config, contentRoot } from "../config.server";import { type Node, selectAll } from "unist-util-select";import { turnIdIntoSlug } from "../src/utils";import { dirname, relative, resolve } from "node:path";
function createPlugin() { function transformer(tree: Node, file: any) { const links = selectAll("link", tree); links.forEach((l) => { let link = l as any as { url: string }; if (!link.url) return; if (link.url.startsWith("http")) return;
if (!link.url.startsWith("/")) { const pathWithoutFile = dirname(file.path); const absolutePath = resolve(pathWithoutFile, link.url); link.url = "/" + relative(contentRoot, absolutePath); }
link.url = ( config.BASE + turnIdIntoSlug(link.url.replace(/\.mdx?$/, "")) ).toLowerCase(); });
return tree; }
return function attacher() { return transformer; };}
/** * Astro embed MDX integration. */export default function fixLinks() { const AstroLinkReplacer: AstroIntegration = { name: "astro-replace-links", hooks: { "astro:config:setup": async ({ updateConfig }) => { updateConfig({ markdown: { remarkPlugins: [createPlugin()], }, }); }, }, };
return [AstroLinkReplacer];}