main
1import type { RehypePlugin } from "@astrojs/markdown-remark";
2import { visit } from "unist-util-visit";
3
4// Relative links break inside feed readers; point them back at the site.
5export function rehypeAbsoluteLinks(options: { site: string }): RehypePlugin {
6 return () => (tree) => {
7 visit(tree, "element", (node) => {
8 const href = node.properties?.href;
9 if (
10 node.tagName !== "a" ||
11 typeof href !== "string" ||
12 !href.startsWith("/") ||
13 href.startsWith("//")
14 ) {
15 return;
16 }
17 node.properties.href = new URL(href, options.site).toString();
18 });
19 };
20}