|
| 1 | +import { visit } from "unist-util-visit" |
| 2 | +import path from "path" |
| 3 | + |
| 4 | +// Helper function to escape quotes in the text |
| 5 | +const escapeQuotes = (text) => text.replace(/"/g, """) |
| 6 | + |
| 7 | +// Creates a Translate node for a given text |
| 8 | +const createTranslateNode = (text) => ({ |
| 9 | + type: "html", |
| 10 | + value: `<Translate text="${escapeQuotes(text)}" />`, |
| 11 | +}) |
| 12 | + |
| 13 | +export function remarkTranslate() { |
| 14 | + return (tree, file) => { |
| 15 | + // Skip processing if file is CHANGELOG.md |
| 16 | + const filename = file.filename || file.history?.[0] || "" |
| 17 | + const basename = path.basename(filename) |
| 18 | + |
| 19 | + if (basename === "CHANGELOG.md") { |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + // For headings, preserve original text in a property and add a wrapper with id |
| 24 | + visit(tree, "heading", (node) => { |
| 25 | + if (node.children && node.children.length) { |
| 26 | + const textNodes = node.children.filter((child) => child.type === "text") |
| 27 | + |
| 28 | + if (textNodes.length) { |
| 29 | + const headingText = textNodes.map((textNode) => textNode.value).join("") |
| 30 | + |
| 31 | + // Generate an ID for the heading based on the text |
| 32 | + const headingId = headingText |
| 33 | + .toLowerCase() |
| 34 | + .replace(/[^\w\s-]/g, "") |
| 35 | + .replace(/\s+/g, "-") |
| 36 | + |
| 37 | + // Create a wrapper span that contains the Translate component |
| 38 | + // but preserves the original text for ID generation |
| 39 | + const wrapperNode = { |
| 40 | + type: "html", |
| 41 | + value: `<span data-heading-text="${escapeQuotes(headingText)}">${createTranslateNode(headingText).value}</span>`, |
| 42 | + } |
| 43 | + |
| 44 | + // Replace text nodes with our wrapper |
| 45 | + node.children = node.children.map((child) => |
| 46 | + child.type === "text" ? wrapperNode : child, |
| 47 | + ) |
| 48 | + |
| 49 | + // Ensure the heading has the correct ID |
| 50 | + if (!node.data) node.data = {} |
| 51 | + if (!node.data.hProperties) node.data.hProperties = {} |
| 52 | + node.data.hProperties.id = headingId |
| 53 | + } |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + // Process table cells individually |
| 58 | + visit(tree, ["table"], (tableNode) => { |
| 59 | + if (tableNode.children) { |
| 60 | + // For each row |
| 61 | + tableNode.children.forEach((row) => { |
| 62 | + if (row.children) { |
| 63 | + // For each cell in the row |
| 64 | + row.children.forEach((cell) => { |
| 65 | + if (cell.children) { |
| 66 | + // For each element in the cell |
| 67 | + cell.children.forEach((child, index) => { |
| 68 | + if (child.type === "text" && child.value.trim()) { |
| 69 | + // Replace text with Translate component |
| 70 | + cell.children[index] = createTranslateNode(child.value.trim()) |
| 71 | + } else if (child.type === "paragraph" && child.children) { |
| 72 | + // For paragraphs inside cells |
| 73 | + child.children.forEach((grandChild, grandIndex) => { |
| 74 | + if (grandChild.type === "text" && grandChild.value.trim()) { |
| 75 | + child.children[grandIndex] = createTranslateNode(grandChild.value.trim()) |
| 76 | + } |
| 77 | + }) |
| 78 | + } else if (child.type === "inlineCode") { |
| 79 | + // Keep code formatting |
| 80 | + const code = child.value |
| 81 | + cell.children[index] = { |
| 82 | + type: "html", |
| 83 | + value: `<code>${code}</code>`, |
| 84 | + } |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | + }) |
| 93 | + |
| 94 | + // Process paragraphs |
| 95 | + visit(tree, "paragraph", (node) => { |
| 96 | + if (node.children && node.children.length) { |
| 97 | + node.children.forEach((child, index) => { |
| 98 | + if (child.type === "text") { |
| 99 | + // Split text by line breaks to preserve formatting |
| 100 | + const lines = child.value.split(/(\n+)/) |
| 101 | + if (lines.length > 1) { |
| 102 | + // If there are line breaks, create multiple translate nodes |
| 103 | + const newNodes = [] |
| 104 | + lines.forEach((line, i) => { |
| 105 | + if (i % 2 === 0 && line.trim()) { |
| 106 | + // Text content |
| 107 | + newNodes.push(createTranslateNode(line)) |
| 108 | + } else { |
| 109 | + // Line breaks |
| 110 | + newNodes.push({ type: "text", value: line }) |
| 111 | + } |
| 112 | + }) |
| 113 | + node.children.splice(index, 1, ...newNodes) |
| 114 | + } else if (child.value.trim()) { |
| 115 | + // Simple text without line breaks |
| 116 | + node.children[index] = createTranslateNode(child.value) |
| 117 | + } |
| 118 | + } |
| 119 | + }) |
| 120 | + } |
| 121 | + }) |
| 122 | + |
| 123 | + // Process list items |
| 124 | + visit(tree, "listItem", (node) => { |
| 125 | + if (node.children && node.children.length) { |
| 126 | + node.children.forEach((child) => { |
| 127 | + if (child.type === "paragraph" && child.children) { |
| 128 | + child.children.forEach((grandChild, index) => { |
| 129 | + if (grandChild.type === "text" && grandChild.value.trim()) { |
| 130 | + child.children[index] = createTranslateNode(grandChild.value) |
| 131 | + } |
| 132 | + }) |
| 133 | + } |
| 134 | + }) |
| 135 | + } |
| 136 | + }) |
| 137 | + |
| 138 | + // Process emphasis and strong |
| 139 | + visit(tree, ["emphasis", "strong"], (node) => { |
| 140 | + if (node.children && node.children.length) { |
| 141 | + node.children.forEach((child, index) => { |
| 142 | + if (child.type === "text" && child.value.trim()) { |
| 143 | + node.children[index] = createTranslateNode(child.value) |
| 144 | + } |
| 145 | + }) |
| 146 | + } |
| 147 | + }) |
| 148 | + |
| 149 | + // Process blockquotes |
| 150 | + visit(tree, "blockquote", (node) => { |
| 151 | + if (node.children && node.children.length) { |
| 152 | + node.children.forEach((child) => { |
| 153 | + if (child.type === "paragraph" && child.children) { |
| 154 | + child.children.forEach((grandChild, index) => { |
| 155 | + if (grandChild.type === "text" && grandChild.value.trim()) { |
| 156 | + child.children[index] = createTranslateNode(grandChild.value) |
| 157 | + } |
| 158 | + }) |
| 159 | + } |
| 160 | + }) |
| 161 | + } |
| 162 | + }) |
| 163 | + } |
| 164 | +} |
0 commit comments