|
| 1 | +import type { Element, Root } from "unist"; |
| 2 | +import { visit } from "unist-util-visit"; |
| 3 | + |
| 4 | +// Exemple de mapping startTag -> composant Astro |
| 5 | +const mdx: Record<string, string> = { |
| 6 | + "card-group": "CardGroup", |
| 7 | + card: "Card", |
| 8 | +}; |
| 9 | + |
| 10 | +interface Block { |
| 11 | + delimiter: string; |
| 12 | + startTag: string; |
| 13 | + attributes: Record<string, any>; |
| 14 | + children: Array<Block | { type: "text"; value: string }>; |
| 15 | +} |
| 16 | + |
| 17 | +// Parse les attributs de la ligne :::tag key="value" |
| 18 | +const parseAttributes = (str: string): Record<string, any> => { |
| 19 | + const regex = /(\w+)\s*=\s*(?:"([^"]*)"|(\S+))/g; |
| 20 | + const attrs: Record<string, any> = {}; |
| 21 | + let match: RegExpExecArray | null; |
| 22 | + |
| 23 | + while ((match = regex.exec(str)) !== null) { |
| 24 | + const key = match[1]; |
| 25 | + let value: any; |
| 26 | + |
| 27 | + if (match[2] !== undefined) { |
| 28 | + // Valeur entre guillemets |
| 29 | + const raw = match[2]; |
| 30 | + try { |
| 31 | + value = JSON.parse(raw); // Essayer de parser JSON |
| 32 | + } catch { |
| 33 | + value = raw; // fallback string |
| 34 | + } |
| 35 | + } else if (match[3] !== undefined) { |
| 36 | + // Valeur non-quoted (true, false, number, etc.) |
| 37 | + const raw = match[3]; |
| 38 | + if (raw === "true") value = true; |
| 39 | + else if (raw === "false") value = false; |
| 40 | + else if (!isNaN(Number(raw))) value = Number(raw); |
| 41 | + else value = raw; |
| 42 | + } |
| 43 | + |
| 44 | + attrs[key] = value; |
| 45 | + } |
| 46 | + |
| 47 | + return attrs; |
| 48 | +}; |
| 49 | + |
| 50 | +// Fonction principale : parse un node et retourne un Block[] |
| 51 | +const parseSingleNode = (node: Element): Block[] => { |
| 52 | + // On combine le node text + mdxTextExpression éventuel en un seul texte |
| 53 | + let combinedText = ""; |
| 54 | + |
| 55 | + for (const child of node.children || []) { |
| 56 | + if (child.type === "text") { |
| 57 | + combinedText += child.value; |
| 58 | + } else if ((child as any).type === "mdxTextExpression") { |
| 59 | + combinedText += " " + (child as any).value; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Fonction récursive interne pour parser un texte complet |
| 64 | + const parseBlockText = (text: string): Block[] => { |
| 65 | + const blocks: Block[] = []; |
| 66 | + const lines = text.split(/\r?\n/); |
| 67 | + const stack: Block[] = []; |
| 68 | + |
| 69 | + for (const line of lines) { |
| 70 | + const startMatch = line.match(/^:::(\w[\w-]*)\s*(.*)$/); |
| 71 | + const endMatch = line.match(/^:::/); |
| 72 | + |
| 73 | + if (startMatch) { |
| 74 | + const block: Block = { |
| 75 | + delimiter: ":::", |
| 76 | + startTag: startMatch[1], |
| 77 | + attributes: parseAttributes(startMatch[2] || ""), |
| 78 | + children: [], |
| 79 | + }; |
| 80 | + |
| 81 | + if (stack.length > 0) { |
| 82 | + // Ajoute comme enfant du dernier parent |
| 83 | + stack[stack.length - 1].children.push(block); |
| 84 | + } else { |
| 85 | + // Bloc racine |
| 86 | + blocks.push(block); |
| 87 | + } |
| 88 | + |
| 89 | + stack.push(block); // push pour gérer la fermeture |
| 90 | + } else if (endMatch) { |
| 91 | + stack.pop(); // on ferme le bloc courant |
| 92 | + } else { |
| 93 | + if (stack.length > 0) { |
| 94 | + stack[stack.length - 1].children.push({ type: "text", value: line }); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Pousser tout bloc restant dans stack (blocs non fermés) |
| 100 | + while (stack.length > 0) { |
| 101 | + const remaining = stack.pop()!; |
| 102 | + if (stack.length > 0) { |
| 103 | + stack[stack.length - 1].children.push(remaining); |
| 104 | + } else if (!blocks.includes(remaining)) { |
| 105 | + blocks.push(remaining); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return blocks; |
| 110 | + }; |
| 111 | + |
| 112 | + return parseBlockText(combinedText); |
| 113 | +}; |
| 114 | + |
| 115 | +// Plugin rehype |
| 116 | +export default function rehypeComponents() { |
| 117 | + return (tree: Root) => { |
| 118 | + visit(tree, "element", (node: Element, index, parent) => { |
| 119 | + if (!parent) return; |
| 120 | + |
| 121 | + const parsedBlocks = parseSingleNode(node); |
| 122 | + |
| 123 | + console.log(JSON.stringify(parsedBlocks, null, 2)); |
| 124 | + |
| 125 | + parent.children[index] = { |
| 126 | + type: "element", |
| 127 | + tagName: "BlockRenderer", |
| 128 | + properties: { |
| 129 | + ast: JSON.stringify(parsedBlocks), |
| 130 | + }, |
| 131 | + children: [], |
| 132 | + } as Element; |
| 133 | + }); |
| 134 | + }; |
| 135 | +} |
0 commit comments