|
| 1 | +import type { Plugin } from "unified"; |
| 2 | +import type { Root } from "hast"; |
| 3 | +import { rehypeCodeHook, type MapLike } from "@beoe/rehype-code-hook"; |
| 4 | +// @ts-ignore |
| 5 | +import d2 from "./d2"; |
| 6 | +import svgToMiniDataURI from "mini-svg-data-uri"; |
| 7 | +import { h } from "hastscript"; |
| 8 | +// SVGO is an experiment. I'm not sure it can compress a lot, plus it can break some diagrams |
| 9 | +import { optimize, type Config as SvgoConfig } from "svgo"; |
| 10 | + |
| 11 | +const svgoConfig: SvgoConfig = { |
| 12 | + plugins: [ |
| 13 | + { |
| 14 | + name: "preset-default", |
| 15 | + params: { |
| 16 | + overrides: { |
| 17 | + // we need viewbox for inline SVGs |
| 18 | + removeViewBox: false, |
| 19 | + // this breaks statediagram |
| 20 | + convertShapeToPath: false, |
| 21 | + }, |
| 22 | + }, |
| 23 | + }, |
| 24 | + ], |
| 25 | +}; |
| 26 | + |
| 27 | +export type RehypeD2Config = { |
| 28 | + cache?: MapLike; |
| 29 | + class?: string; |
| 30 | + /** |
| 31 | + * be carefull. It may break some diagrams. For example, stateDiagram-v2 |
| 32 | + */ |
| 33 | + svgo?: SvgoConfig | boolean; |
| 34 | + strategy?: "inline" | "img"; |
| 35 | +}; |
| 36 | + |
| 37 | +export const rehypeD2: Plugin<[RehypeD2Config?], Root> = ( |
| 38 | + options = {} |
| 39 | +) => { |
| 40 | + const { svgo, strategy, ...rest } = options; |
| 41 | + |
| 42 | + const salt = options; |
| 43 | + |
| 44 | + const render = async (code: string) => { |
| 45 | + let svg: string = await d2(code); |
| 46 | + if (svgo !== false) { |
| 47 | + svg = optimize( |
| 48 | + svg, |
| 49 | + svgo === undefined || svgo === true ? svgoConfig : svgo |
| 50 | + ).data; |
| 51 | + } |
| 52 | + |
| 53 | + const widthMatch = svg.match(/width="(\d+[^"]+)"\s+/); |
| 54 | + const width = widthMatch ? widthMatch[1] : undefined; |
| 55 | + |
| 56 | + const heightMatch = svg.match(/height="(\d+[^"]+)"\s+/); |
| 57 | + const height = heightMatch ? heightMatch[1] : undefined; |
| 58 | + |
| 59 | + svg = svg.replace(/width="\d+[^"]+"\s+/, ""); |
| 60 | + svg = svg.replace(/height="\d+[^"]+"\s+/, ""); |
| 61 | + return { svg, width, height }; |
| 62 | + }; |
| 63 | + |
| 64 | + // @ts-expect-error |
| 65 | + return rehypeCodeHook({ |
| 66 | + ...rest, |
| 67 | + salt, |
| 68 | + language: "d2", |
| 69 | + code: async ({ code }) => { |
| 70 | + switch (strategy) { |
| 71 | + case "img": { |
| 72 | + const { svg, width, height } = await render(code); |
| 73 | + return h( |
| 74 | + "figure", |
| 75 | + { |
| 76 | + class: `beoe d2 ${rest.class || ""}`, |
| 77 | + }, |
| 78 | + // wrapp in additional div for svg-pan-zoom |
| 79 | + h("img", { |
| 80 | + width, |
| 81 | + height, |
| 82 | + alt: "", |
| 83 | + src: svgToMiniDataURI(svg), |
| 84 | + }) |
| 85 | + ); |
| 86 | + } |
| 87 | + default: { |
| 88 | + const { svg } = await render(code); |
| 89 | + return `<figure class="beoe d2 ${ |
| 90 | + rest.class || "" |
| 91 | + }">${svg}</figure>`; |
| 92 | + } |
| 93 | + } |
| 94 | + }, |
| 95 | + }); |
| 96 | +}; |
| 97 | + |
| 98 | +export default rehypeD2; |
0 commit comments