[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/
0

Configure Feed

Select the types of activity you want to include in your feed.

tiny-docs / transform-links / index.ts
1.4 kB 52 lines
1import type { AstroIntegration } from "astro"; 2import { config, contentRoot } from "../config.server"; 3import { type Node, selectAll } from "unist-util-select"; 4import { turnIdIntoSlug } from "../src/utils"; 5import { dirname, relative, resolve } from "node:path"; 6 7function createPlugin() { 8 function transformer(tree: Node, file: any) { 9 const links = selectAll("link", tree); 10 links.forEach((l) => { 11 let link = l as any as { url: string }; 12 if (!link.url) return; 13 if (link.url.startsWith("http")) return; 14 15 if (!link.url.startsWith("/")) { 16 const pathWithoutFile = dirname(file.path); 17 const absolutePath = resolve(pathWithoutFile, link.url); 18 link.url = "/" + relative(contentRoot, absolutePath); 19 } 20 21 link.url = ( 22 config.BASE + turnIdIntoSlug(link.url.replace(/\.mdx?$/, "")) 23 ).toLowerCase(); 24 }); 25 26 return tree; 27 } 28 29 return function attacher() { 30 return transformer; 31 }; 32} 33 34/** 35 * Astro embed MDX integration. 36 */ 37export default function fixLinks() { 38 const AstroLinkReplacer: AstroIntegration = { 39 name: "astro-replace-links", 40 hooks: { 41 "astro:config:setup": async ({ updateConfig }) => { 42 updateConfig({ 43 markdown: { 44 remarkPlugins: [createPlugin()], 45 }, 46 }); 47 }, 48 }, 49 }; 50 51 return [AstroLinkReplacer]; 52}