|
1 | 1 | import '@tanstack/react-start/server-only' |
2 | 2 |
|
3 | | -import type { Root } from 'fumadocs-core/page-tree' |
| 3 | +import type { Node, Root } from 'fumadocs-core/page-tree' |
4 | 4 |
|
5 | 5 | import { visit } from 'fumadocs-core/page-tree' |
6 | 6 | import dynamicIconImports from 'lucide-react/dynamicIconImports.mjs' |
@@ -57,3 +57,123 @@ export const loadDocsPageTreeIcons = async ( |
57 | 57 |
|
58 | 58 | return Object.fromEntries(entries.flat()) |
59 | 59 | } |
| 60 | + |
| 61 | +const numbers = (attributes: Record<string, string>, ...names: string[]) => |
| 62 | + names.map((name) => Number(attributes[name] ?? 0)) |
| 63 | + |
| 64 | +const pointsPath = (points: string | undefined, closed: boolean) => { |
| 65 | + const coordinates = (points ?? '').trim() |
| 66 | + |
| 67 | + return coordinates ? `M${coordinates}${closed ? 'Z' : ''}` : '' |
| 68 | +} |
| 69 | + |
| 70 | +const ellipsePath = (cx: number, cy: number, rx: number, ry: number) => |
| 71 | + `M${cx - rx} ${cy}a${rx} ${ry} 0 1 0 ${rx * 2} 0a${rx} ${ry} 0 1 0 ${-rx * 2} 0` |
| 72 | + |
| 73 | +const rectPath = (attributes: Record<string, string>) => { |
| 74 | + const [x, y, width, height] = numbers(attributes, 'x', 'y', 'width', 'height') |
| 75 | + const rx = Math.min(Number(attributes.rx ?? attributes.ry ?? 0), width / 2) |
| 76 | + const ry = Math.min(Number(attributes.ry ?? attributes.rx ?? 0), height / 2) |
| 77 | + |
| 78 | + if (rx === 0 && ry === 0) return `M${x} ${y}h${width}v${height}h${-width}Z` |
| 79 | + |
| 80 | + return [ |
| 81 | + `M${x + rx} ${y}`, |
| 82 | + `H${x + width - rx}`, |
| 83 | + `A${rx} ${ry} 0 0 1 ${x + width} ${y + ry}`, |
| 84 | + `V${y + height - ry}`, |
| 85 | + `A${rx} ${ry} 0 0 1 ${x + width - rx} ${y + height}`, |
| 86 | + `H${x + rx}`, |
| 87 | + `A${rx} ${ry} 0 0 1 ${x} ${y + height - ry}`, |
| 88 | + `V${y + ry}`, |
| 89 | + `A${rx} ${ry} 0 0 1 ${x + rx} ${y}`, |
| 90 | + 'Z', |
| 91 | + ].join('') |
| 92 | +} |
| 93 | + |
| 94 | +const NUMBER = String.raw`-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?` |
| 95 | +const LEADING_RELATIVE_MOVE = new RegExp( |
| 96 | + String.raw`^\s*m\s*(${NUMBER})[\s,]*(${NUMBER})[\s,]*`, |
| 97 | + 'i', |
| 98 | +) |
| 99 | + |
| 100 | +const standalonePath = (d: string) => { |
| 101 | + const match = LEADING_RELATIVE_MOVE.exec(d) |
| 102 | + if (!match || d.trimStart().startsWith('M')) return d |
| 103 | + |
| 104 | + const rest = d.slice(match[0].length) |
| 105 | + const implicitLines = rest !== '' && !/^[a-z]/i.test(rest) |
| 106 | + |
| 107 | + return `M${match[1]} ${match[2]}${implicitLines ? 'l' : ''}${rest}` |
| 108 | +} |
| 109 | + |
| 110 | +const shapePath = ([tag, attributes]: DocsIconNode[number]) => { |
| 111 | + switch (tag) { |
| 112 | + case 'circle': { |
| 113 | + const [cx, cy, r] = numbers(attributes, 'cx', 'cy', 'r') |
| 114 | + |
| 115 | + return ellipsePath(cx, cy, r, r) |
| 116 | + } |
| 117 | + case 'ellipse': { |
| 118 | + const [cx, cy, rx, ry] = numbers(attributes, 'cx', 'cy', 'rx', 'ry') |
| 119 | + |
| 120 | + return ellipsePath(cx, cy, rx, ry) |
| 121 | + } |
| 122 | + case 'line': { |
| 123 | + const [x1, y1, x2, y2] = numbers(attributes, 'x1', 'y1', 'x2', 'y2') |
| 124 | + |
| 125 | + return `M${x1} ${y1}L${x2} ${y2}` |
| 126 | + } |
| 127 | + case 'path': |
| 128 | + return standalonePath(attributes.d ?? '') |
| 129 | + case 'polygon': |
| 130 | + return pointsPath(attributes.points, true) |
| 131 | + case 'polyline': |
| 132 | + return pointsPath(attributes.points, false) |
| 133 | + case 'rect': |
| 134 | + return rectPath(attributes) |
| 135 | + default: |
| 136 | + return '' |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +export const loadDocsIconPathData = async ( |
| 141 | + name: string | undefined, |
| 142 | +): Promise<string | undefined> => { |
| 143 | + const load = name ? iconImports.get(name) : undefined |
| 144 | + if (!load) return undefined |
| 145 | + |
| 146 | + const node = toDocsIconNode((await load()).__iconData.node) |
| 147 | + |
| 148 | + return node.map(shapePath).filter(Boolean).join('') || undefined |
| 149 | +} |
| 150 | + |
| 151 | +const treePathOf = (nodes: readonly Node[], url: string): Node[] | null => { |
| 152 | + for (const node of nodes) { |
| 153 | + if (node.type === 'page' && node.url === url) return [node] |
| 154 | + if (node.type !== 'folder') continue |
| 155 | + if (node.index?.url === url) return [node, node.index] |
| 156 | + |
| 157 | + const path = treePathOf(node.children, url) |
| 158 | + if (path) return [node, ...path] |
| 159 | + } |
| 160 | + |
| 161 | + return null |
| 162 | +} |
| 163 | + |
| 164 | +const iconNameOf = (node: Node) => |
| 165 | + 'icon' in node && typeof node.icon === 'string' ? node.icon : undefined |
| 166 | + |
| 167 | +export const docsPagePlacement = (tree: Root, url: string) => { |
| 168 | + const path = treePathOf(tree.children, url) ?? [] |
| 169 | + const [top] = path |
| 170 | + const sectionTitle = |
| 171 | + top?.type === 'folder' && top.root && typeof top.name === 'string' |
| 172 | + ? top.name |
| 173 | + : undefined |
| 174 | + |
| 175 | + return { |
| 176 | + iconName: path.map(iconNameOf).filter(Boolean).at(-1), |
| 177 | + sectionTitle, |
| 178 | + } |
| 179 | +} |
0 commit comments