|
| 1 | +import { visit } from 'unist-util-visit'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Remark plugin to transform directives (:::tip, :::note, :::steps, etc.) into custom components |
| 5 | + * Works with remark-directive to convert container directives into Svelte components |
| 6 | + * |
| 7 | + * Supported directives: |
| 8 | + * - :::tip - renders as Note component with variant="tip" |
| 9 | + * - :::note - renders as Note component with variant="note" |
| 10 | + * - :::warning - renders as Note component with variant="warning" |
| 11 | + * - :::caution - renders as Note component with variant="caution" |
| 12 | + * - :::steps - renders as Steps component |
| 13 | + * |
| 14 | + * @returns {(tree: any) => void} A remark transformer function |
| 15 | + */ |
| 16 | +export function remarkDirectives() { |
| 17 | + return (tree) => { |
| 18 | + const componentsToImport = new Set(); |
| 19 | + |
| 20 | + visit(tree, (node) => { |
| 21 | + // Handle container directives (:::directive) |
| 22 | + if (node.type === 'containerDirective') { |
| 23 | + const directiveName = node.name; |
| 24 | + |
| 25 | + // Alert variants all use the Note component |
| 26 | + const alertVariants = ['tip', 'note', 'warning', 'caution']; |
| 27 | + |
| 28 | + // Map directive names to component names and variants |
| 29 | + let componentName; |
| 30 | + let variant; |
| 31 | + |
| 32 | + if (alertVariants.includes(directiveName)) { |
| 33 | + componentName = 'Note'; |
| 34 | + variant = directiveName; |
| 35 | + } else if (directiveName === 'steps') { |
| 36 | + componentName = 'Steps'; |
| 37 | + } else { |
| 38 | + // Unknown directive, skip |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // Track which components we need to import |
| 43 | + componentsToImport.add(componentName); |
| 44 | + |
| 45 | + // Get directive attributes |
| 46 | + const attributes = node.attributes || {}; |
| 47 | + |
| 48 | + // Convert the directive into a custom component in the HTML tree |
| 49 | + // We set data.hName to tell rehype to convert this to the component |
| 50 | + const data = node.data || (node.data = {}); |
| 51 | + data.hName = componentName; |
| 52 | + data.hProperties = { |
| 53 | + ...attributes, |
| 54 | + // Pass variant for alert components |
| 55 | + ...(variant && { variant }), |
| 56 | + // Pass any directive label as a prop |
| 57 | + ...(node.attributes?.label && { label: node.attributes.label }) |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + // Handle text directives (:directive[text]) if needed |
| 62 | + if (node.type === 'textDirective') { |
| 63 | + // Can be used for inline directives if needed in the future |
| 64 | + } |
| 65 | + |
| 66 | + // Handle leaf directives (::directive) if needed |
| 67 | + if (node.type === 'leafDirective') { |
| 68 | + // Can be used for self-closing directives if needed in the future |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + // Inject component imports at the beginning of the file |
| 73 | + if (componentsToImport.size > 0) { |
| 74 | + const componentArray = Array.from(componentsToImport); |
| 75 | + const importStatements = componentArray |
| 76 | + .map((comp) => `import ${comp} from '$lib/markdown/components/${comp}.svelte';`) |
| 77 | + .join('\n'); |
| 78 | + |
| 79 | + // Check if there's already a script tag |
| 80 | + let hasScript = false; |
| 81 | + visit(tree, 'html', (node) => { |
| 82 | + if (node.value.startsWith('<script') && !hasScript) { |
| 83 | + hasScript = true; |
| 84 | + node.value = node.value.replace( |
| 85 | + /<script[^>]*>/, |
| 86 | + (match) => `${match}\n${importStatements}` |
| 87 | + ); |
| 88 | + return visit.EXIT; |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + if (!hasScript) { |
| 93 | + // Create new script tag at the beginning |
| 94 | + tree.children.unshift({ |
| 95 | + type: 'html', |
| 96 | + value: `<script>\n${importStatements}\n</script>` |
| 97 | + }); |
| 98 | + } |
| 99 | + } |
| 100 | + }; |
| 101 | +} |
0 commit comments