diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index 383891d5c..eee5eb223 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -15,6 +15,7 @@ import { import { getFontSize, isJetBrains } from "../../util"; import "./katex.css"; import "./markdown.css"; +import "./markdown-tables.css"; import { ctxItemToRifWithContents } from "core/commands/util"; import FilenameLink from "./FilenameLink"; import StepContainerPreToolbar from "./StepContainerPreToolbar"; @@ -26,6 +27,7 @@ import { ContextItemWithId, SymbolWithRange } from "core"; import SymbolLink from "./SymbolLink"; import { useSelector } from "react-redux"; import useUpdatingRef from "../../hooks/useUpdatingRef"; +import { remarkTables } from "./utils/remarkTables"; const StyledMarkdown = styled.div<{ fontSize?: number; @@ -177,11 +179,10 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( const [reactContent, setMarkdownSource] = useRemark({ remarkPlugins: [ + // remarkTables, // remarkGfm, remarkMath, () => processCodeBlocks, - // c - //c ], rehypePlugins: [ rehypeKatex as any, diff --git a/gui/src/components/markdown/markdown-tables.css b/gui/src/components/markdown/markdown-tables.css new file mode 100644 index 000000000..e0f80b6db --- /dev/null +++ b/gui/src/components/markdown/markdown-tables.css @@ -0,0 +1,11 @@ +table { + background-color: red; +} + +tr { + color: red; +} + +td { + color: red; +} diff --git a/gui/src/components/markdown/utils/remarkTables.tsx b/gui/src/components/markdown/utils/remarkTables.tsx new file mode 100644 index 000000000..d8e5bc84e --- /dev/null +++ b/gui/src/components/markdown/utils/remarkTables.tsx @@ -0,0 +1,233 @@ +// import { visit } from "unist-util-visit"; + +// // const markdownTableRegex = /(\|.+?\|)(\n\|(?:[-:]+?\|)+)(\n\|.+?\|)+/gm; +// const tableRegex = /(\|(?:[^\|]*\|)+(\s*\n\s*\|(?:[^\|]*\|)+)*\s*(\n|\r|\r\n)?\|(?:[-:\s]*\|)+(\s*\n\s*\|(?:[^\|]*\|)+)*)/g; + +// const parseMarkdownTable = (markdown) => { +// const rows = markdown.trim().split("\n"); +// const header = rows[0] +// .split("|") +// .slice(1, -1) +// .map((cell) => cell.trim()); +// const alignments = rows[1] +// .split("|") +// .slice(1, -1) +// .map((cell) => { +// if (cell.startsWith(":") && cell.endsWith(":")) return "center"; +// if (cell.startsWith(":")) return "left"; +// if (cell.endsWith(":")) return "right"; +// return null; +// }); +// const bodyRows = rows.slice(2).map((row) => +// row +// .split("|") +// .slice(1, -1) +// .map((cell) => cell.trim()), +// ); + +// return { header, alignments, bodyRows }; +// }; + +// const renderTable = ({ header, alignments, bodyRows }) => ( +// +// +// +// {header.map((cell, index) => ( +// +// ))} +// +// +// +// {bodyRows.map((row, rowIndex) => ( +// +// {row.map((cell, cellIndex) => ( +// +// ))} +// +// ))} +// +//
+// {cell} +//
+// {cell} +//
+// ); + +// export function remarkTables() { +// return (tree) => { +// visit(tree, "text", (node, index, parent) => { +// if (!node.value) return; + +// const matches = [...node.value.matchAll(markdownTableRegex)]; +// console.log("MATCHES", matches); +// if (matches.length === 0) return; + +// const fragments = []; +// let lastIndex = 0; + +// matches.forEach((match, idx) => { +// const [fullMatch] = match; +// const matchStart = match.index; +// const matchEnd = matchStart + fullMatch.length; + +// // Add text before the table +// if (matchStart > lastIndex) { +// fragments.push(node.value.slice(lastIndex, matchStart)); +// } + +// // Parse and render the markdown table +// const table = parseMarkdownTable(fullMatch); +// fragments.push(renderTable(table)); + +// lastIndex = matchEnd; +// }); + +// // Add remaining text after the last table +// if (lastIndex < node.value.length) { +// fragments.push(node.value.slice(lastIndex)); +// } + +// // Replace the parent node with a fragment containing the new nodes +// if (parent && parent.children) { +// parent.children.splice(index, 1, ...fragments); +// } +// }); +// }; +// } + +const visit = require("unist-util-visit"); + +export function remarkTables() { + return (tree) => { + visit(tree, "text", (node, index, parent) => { + const { value } = node; + // Inspired by + // https://stackoverflow.com/questions/9837935/regex-for-markdown-table-syntax + const tableRegex = + /^(\|[^\n]+\|(?:\r?\n| ))((?:\|:?[-]+:?)+\|)((?:\r?\n| )(?:\|[^\n]+\|(?:\r?\n| ))*)?$/gm; + let match; + let lastIndex = 0; + const newNodes = []; + + while ((match = tableRegex.exec(value)) !== null) { + console.log(match); + const tableText = match[0]; + + // Add any text before the table as a text node + if (match.index > lastIndex) { + newNodes.push({ + type: "text", + value: value.slice(lastIndex, match.index), + }); + } + + const tableNode = parseTable(tableText); + if (tableNode) { + newNodes.push(tableNode); + } else { + // If parsing failed, keep the original text + newNodes.push({ + type: "text", + value: tableText, + }); + } + + lastIndex = tableRegex.lastIndex; + } + + // Add any remaining text after the last table + if (lastIndex < value.length) { + newNodes.push({ + type: "text", + value: value.slice(lastIndex), + }); + } + + // Replace the original text node with the new nodes + if (newNodes.length > 0) { + parent.children.splice(index, 1, ...newNodes); + } + }); + }; +} + +// The parseTable function remains the same as before +function parseTable(tableText) { + const lines = tableText.trim().split(/\r?\n/); + + if (lines.length < 2) return null; // Not enough lines for a valid table + + const header = splitRow(lines[0]); + const separator = splitRow(lines[1]); + + // Validate the separator line + if (!separator.every((cell) => /^:?-+:?$/.test(cell.trim()))) { + return null; + } + + const alignments = separator.map((cell) => { + const trimmed = cell.trim(); + if (trimmed.startsWith(":") && trimmed.endsWith(":")) return "center"; + if (trimmed.endsWith(":")) return "right"; + if (trimmed.startsWith(":")) return "left"; + return null; + }); + + const tableNode = { + type: "element", + tagName: "table", + children: [ + { + type: "element", + tagName: "thead", + children: [ + { + type: "element", + tagName: "tr", + children: header.map((cell, i) => ({ + type: "element", + tagName: "th", + properties: { + align: alignments[i] || "left", + className: "text-" + alignments[i], + }, + children: [{ type: "text", value: cell.trim() }], + })), + }, + ], + }, + { + type: "element", + tagName: "tbody", + children: lines.slice(2).map((line) => { + const cells = splitRow(line); + return { + type: "element", + tagName: "tr", + children: cells.map((cell, i) => ({ + type: "element", + tagName: "td", + properties: { + align: alignments[i] || "left", + className: "text-" + alignments[i], + }, + children: [{ type: "text", value: cell.trim() }], + })), + }; + }), + }, + ], + }; + + return tableNode; +} + +function splitRow(row) { + return row + .trim() + .replace(/^\||\|$/g, "") // Remove leading and trailing pipes + .split("|"); +}