From 2113d5ef07bb845321d7a85c0f1c32235bdc331b Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 2 Dec 2024 01:06:25 -0500 Subject: [PATCH 1/6] remark tables p1 --- extensions/vscode/package-lock.json | 5 +- gui/package-lock.json | 276 ++++++++++++++++++ gui/package.json | 1 + .../markdown/StyledMarkdownPreview.tsx | 31 +- 4 files changed, 299 insertions(+), 14 deletions(-) diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index e0bc72e92..49cc1fb6c 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -1,12 +1,12 @@ { "name": "continue", - "version": "0.9.235", + "version": "0.9.237", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "continue", - "version": "0.9.235", + "version": "0.9.237", "license": "Apache-2.0", "dependencies": { "@electron/rebuild": "^3.2.10", @@ -109,6 +109,7 @@ "cheerio": "^1.0.0-rc.12", "commander": "^12.0.0", "comment-json": "^4.2.3", + "dbinfoz": "^0.14.0", "diff": "^7.0.0", "dotenv": "^16.4.5", "fastest-levenshtein": "^1.0.16", diff --git a/gui/package-lock.json b/gui/package-lock.json index 3a69aaaea..52a14384e 100644 --- a/gui/package-lock.json +++ b/gui/package-lock.json @@ -50,6 +50,7 @@ "rehype-highlight": "^7.0.0", "rehype-katex": "^7.0.0", "rehype-wrap-all": "^1.1.0", + "remark-gfm": "^4.0.0", "remark-math": "^6.0.0", "reselect": "^5.1.1", "seti-file-icons": "^0.0.8", @@ -8545,6 +8546,15 @@ "markdown-it": "bin/markdown-it.mjs" } }, + "node_modules/markdown-table": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/markdown-table/-/markdown-table-3.0.4.tgz", + "integrity": "sha512-wiYz4+JrLyb/DqW2hkFJxP7Vd7JuTDm77fvbM8VfEQdmSMqcImWeeRbHwZjBjIFki/VaMK2BhFi7oUUZeM5bqw==", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/wooorm" + } + }, "node_modules/mdast-util-definitions": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/mdast-util-definitions/-/mdast-util-definitions-4.0.0.tgz", @@ -8598,6 +8608,32 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/mdast-util-find-and-replace": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/mdast-util-find-and-replace/-/mdast-util-find-and-replace-3.0.1.tgz", + "integrity": "sha512-SG21kZHGC3XRTSUhtofZkBzZTJNM5ecCi0SK2IMKmSXR8vO3peL+kb1O0z7Zl83jKtutG4k5Wv/W7V3/YHvzPA==", + "dependencies": { + "@types/mdast": "^4.0.0", + "escape-string-regexp": "^5.0.0", + "unist-util-is": "^6.0.0", + "unist-util-visit-parents": "^6.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/mdast-util-find-and-replace/node_modules/escape-string-regexp": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-5.0.0.tgz", + "integrity": "sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/mdast-util-from-markdown": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/mdast-util-from-markdown/-/mdast-util-from-markdown-2.0.1.tgz", @@ -8621,6 +8657,101 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/mdast-util-gfm": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/mdast-util-gfm/-/mdast-util-gfm-3.0.0.tgz", + "integrity": "sha512-dgQEX5Amaq+DuUqf26jJqSK9qgixgd6rYDHAv4aTBuA92cTknZlKpPfa86Z/s8Dj8xsAQpFfBmPUHWJBWqS4Bw==", + "dependencies": { + "mdast-util-from-markdown": "^2.0.0", + "mdast-util-gfm-autolink-literal": "^2.0.0", + "mdast-util-gfm-footnote": "^2.0.0", + "mdast-util-gfm-strikethrough": "^2.0.0", + "mdast-util-gfm-table": "^2.0.0", + "mdast-util-gfm-task-list-item": "^2.0.0", + "mdast-util-to-markdown": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/mdast-util-gfm-autolink-literal": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/mdast-util-gfm-autolink-literal/-/mdast-util-gfm-autolink-literal-2.0.1.tgz", + "integrity": "sha512-5HVP2MKaP6L+G6YaxPNjuL0BPrq9orG3TsrZ9YXbA3vDw/ACI4MEsnoDpn6ZNm7GnZgtAcONJyPhOP8tNJQavQ==", + "dependencies": { + "@types/mdast": "^4.0.0", + "ccount": "^2.0.0", + "devlop": "^1.0.0", + "mdast-util-find-and-replace": "^3.0.0", + "micromark-util-character": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/mdast-util-gfm-footnote": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/mdast-util-gfm-footnote/-/mdast-util-gfm-footnote-2.0.0.tgz", + "integrity": "sha512-5jOT2boTSVkMnQ7LTrd6n/18kqwjmuYqo7JUPe+tRCY6O7dAuTFMtTPauYYrMPpox9hlN0uOx/FL8XvEfG9/mQ==", + "dependencies": { + "@types/mdast": "^4.0.0", + "devlop": "^1.1.0", + "mdast-util-from-markdown": "^2.0.0", + "mdast-util-to-markdown": "^2.0.0", + "micromark-util-normalize-identifier": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/mdast-util-gfm-strikethrough": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/mdast-util-gfm-strikethrough/-/mdast-util-gfm-strikethrough-2.0.0.tgz", + "integrity": "sha512-mKKb915TF+OC5ptj5bJ7WFRPdYtuHv0yTRxK2tJvi+BDqbkiG7h7u/9SI89nRAYcmap2xHQL9D+QG/6wSrTtXg==", + "dependencies": { + "@types/mdast": "^4.0.0", + "mdast-util-from-markdown": "^2.0.0", + "mdast-util-to-markdown": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/mdast-util-gfm-table": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/mdast-util-gfm-table/-/mdast-util-gfm-table-2.0.0.tgz", + "integrity": "sha512-78UEvebzz/rJIxLvE7ZtDd/vIQ0RHv+3Mh5DR96p7cS7HsBhYIICDBCu8csTNWNO6tBWfqXPWekRuj2FNOGOZg==", + "dependencies": { + "@types/mdast": "^4.0.0", + "devlop": "^1.0.0", + "markdown-table": "^3.0.0", + "mdast-util-from-markdown": "^2.0.0", + "mdast-util-to-markdown": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/mdast-util-gfm-task-list-item": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/mdast-util-gfm-task-list-item/-/mdast-util-gfm-task-list-item-2.0.0.tgz", + "integrity": "sha512-IrtvNvjxC1o06taBAVJznEnkiHxLFTzgonUdy8hzFVeDun0uTjxxrRGVaNFqkU1wJR3RBPEfsxmU6jDWPofrTQ==", + "dependencies": { + "@types/mdast": "^4.0.0", + "devlop": "^1.0.0", + "mdast-util-from-markdown": "^2.0.0", + "mdast-util-to-markdown": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "node_modules/mdast-util-math": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/mdast-util-math/-/mdast-util-math-3.0.0.tgz", @@ -8847,6 +8978,120 @@ "micromark-util-types": "^2.0.0" } }, + "node_modules/micromark-extension-gfm": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/micromark-extension-gfm/-/micromark-extension-gfm-3.0.0.tgz", + "integrity": "sha512-vsKArQsicm7t0z2GugkCKtZehqUm31oeGBV/KVSorWSy8ZlNAv7ytjFhvaryUiCUJYqs+NoE6AFhpQvBTM6Q4w==", + "dependencies": { + "micromark-extension-gfm-autolink-literal": "^2.0.0", + "micromark-extension-gfm-footnote": "^2.0.0", + "micromark-extension-gfm-strikethrough": "^2.0.0", + "micromark-extension-gfm-table": "^2.0.0", + "micromark-extension-gfm-tagfilter": "^2.0.0", + "micromark-extension-gfm-task-list-item": "^2.0.0", + "micromark-util-combine-extensions": "^2.0.0", + "micromark-util-types": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/micromark-extension-gfm-autolink-literal": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/micromark-extension-gfm-autolink-literal/-/micromark-extension-gfm-autolink-literal-2.1.0.tgz", + "integrity": "sha512-oOg7knzhicgQ3t4QCjCWgTmfNhvQbDDnJeVu9v81r7NltNCVmhPy1fJRX27pISafdjL+SVc4d3l48Gb6pbRypw==", + "dependencies": { + "micromark-util-character": "^2.0.0", + "micromark-util-sanitize-uri": "^2.0.0", + "micromark-util-symbol": "^2.0.0", + "micromark-util-types": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/micromark-extension-gfm-footnote": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/micromark-extension-gfm-footnote/-/micromark-extension-gfm-footnote-2.1.0.tgz", + "integrity": "sha512-/yPhxI1ntnDNsiHtzLKYnE3vf9JZ6cAisqVDauhp4CEHxlb4uoOTxOCJ+9s51bIB8U1N1FJ1RXOKTIlD5B/gqw==", + "dependencies": { + "devlop": "^1.0.0", + "micromark-core-commonmark": "^2.0.0", + "micromark-factory-space": "^2.0.0", + "micromark-util-character": "^2.0.0", + "micromark-util-normalize-identifier": "^2.0.0", + "micromark-util-sanitize-uri": "^2.0.0", + "micromark-util-symbol": "^2.0.0", + "micromark-util-types": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/micromark-extension-gfm-strikethrough": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/micromark-extension-gfm-strikethrough/-/micromark-extension-gfm-strikethrough-2.1.0.tgz", + "integrity": "sha512-ADVjpOOkjz1hhkZLlBiYA9cR2Anf8F4HqZUO6e5eDcPQd0Txw5fxLzzxnEkSkfnD0wziSGiv7sYhk/ktvbf1uw==", + "dependencies": { + "devlop": "^1.0.0", + "micromark-util-chunked": "^2.0.0", + "micromark-util-classify-character": "^2.0.0", + "micromark-util-resolve-all": "^2.0.0", + "micromark-util-symbol": "^2.0.0", + "micromark-util-types": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/micromark-extension-gfm-table": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/micromark-extension-gfm-table/-/micromark-extension-gfm-table-2.1.0.tgz", + "integrity": "sha512-Ub2ncQv+fwD70/l4ou27b4YzfNaCJOvyX4HxXU15m7mpYY+rjuWzsLIPZHJL253Z643RpbcP1oeIJlQ/SKW67g==", + "dependencies": { + "devlop": "^1.0.0", + "micromark-factory-space": "^2.0.0", + "micromark-util-character": "^2.0.0", + "micromark-util-symbol": "^2.0.0", + "micromark-util-types": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/micromark-extension-gfm-tagfilter": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/micromark-extension-gfm-tagfilter/-/micromark-extension-gfm-tagfilter-2.0.0.tgz", + "integrity": "sha512-xHlTOmuCSotIA8TW1mDIM6X2O1SiX5P9IuDtqGonFhEK0qgRI4yeC6vMxEV2dgyr2TiD+2PQ10o+cOhdVAcwfg==", + "dependencies": { + "micromark-util-types": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/micromark-extension-gfm-task-list-item": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/micromark-extension-gfm-task-list-item/-/micromark-extension-gfm-task-list-item-2.1.0.tgz", + "integrity": "sha512-qIBZhqxqI6fjLDYFTBIa4eivDMnP+OZqsNwmQ3xNLE4Cxwc+zfQEfbs6tzAo2Hjq+bh6q5F+Z8/cksrLFYWQQw==", + "dependencies": { + "devlop": "^1.0.0", + "micromark-factory-space": "^2.0.0", + "micromark-util-character": "^2.0.0", + "micromark-util-symbol": "^2.0.0", + "micromark-util-types": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "node_modules/micromark-extension-math": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/micromark-extension-math/-/micromark-extension-math-3.1.0.tgz", @@ -11282,6 +11527,23 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/remark-gfm": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/remark-gfm/-/remark-gfm-4.0.0.tgz", + "integrity": "sha512-U92vJgBPkbw4Zfu/IiW2oTZLSL3Zpv+uI7My2eq8JxKgqraFdU8YUGicEJCEgSbeaG+QDFqIcwwfMTOEelPxuA==", + "dependencies": { + "@types/mdast": "^4.0.0", + "mdast-util-gfm": "^3.0.0", + "micromark-extension-gfm": "^3.0.0", + "remark-parse": "^11.0.0", + "remark-stringify": "^11.0.0", + "unified": "^11.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "node_modules/remark-math": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/remark-math/-/remark-math-6.0.0.tgz", @@ -11328,6 +11590,20 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/remark-stringify": { + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/remark-stringify/-/remark-stringify-11.0.0.tgz", + "integrity": "sha512-1OSmLd3awB/t8qdoEOMazZkNsfVTeY4fTsgzcQFdXNq8ToTN4ZGwrMnlda4K6smTFKD+GRV6O48i6Z4iKgPPpw==", + "dependencies": { + "@types/mdast": "^4.0.0", + "mdast-util-to-markdown": "^2.0.0", + "unified": "^11.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "node_modules/require-from-string": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", diff --git a/gui/package.json b/gui/package.json index 4365e2ba2..e13e00be6 100644 --- a/gui/package.json +++ b/gui/package.json @@ -58,6 +58,7 @@ "rehype-highlight": "^7.0.0", "rehype-katex": "^7.0.0", "rehype-wrap-all": "^1.1.0", + "remark-gfm": "^4.0.0", "remark-math": "^6.0.0", "reselect": "^5.1.1", "seti-file-icons": "^0.0.8", diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index caad6428c..383891d5c 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -3,6 +3,7 @@ import { useRemark } from "react-remark"; import rehypeHighlight, { Options } from "rehype-highlight"; import rehypeKatex from "rehype-katex"; import remarkMath from "remark-math"; +import remarkGfm from "remark-gfm"; import styled from "styled-components"; import { visit } from "unist-util-visit"; import { @@ -175,7 +176,13 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( }, [symbols]); const [reactContent, setMarkdownSource] = useRemark({ - remarkPlugins: [remarkMath, () => processCodeBlocks], + remarkPlugins: [ + // remarkGfm, + remarkMath, + () => processCodeBlocks, + // c + //c + ], rehypePlugins: [ rehypeKatex as any, {}, @@ -187,17 +194,17 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( { // languages: {}, } as Options, - () => { - let codeBlockIndex = 0; - return (tree) => { - visit(tree, { tagName: "pre" }, (node: any) => { - // Add an index (0, 1, 2, etc...) to each code block. - node.properties = { codeBlockIndex }; - codeBlockIndex++; - }); - }; - }, - {}, + // () => { + // let codeBlockIndex = 0; + // return (tree) => { + // visit(tree, { tagName: "pre" }, (node: any) => { + // Add an index (0, 1, 2, etc...) to each code block. + // node.properties = { codeBlockIndex }; + // codeBlockIndex++; + // }); + // }; + // }, + // {}, ], rehypeReactOptions: { components: { From a2b327fb5863e593552350d07357b940dccdf2bb Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 2 Dec 2024 16:11:49 -0500 Subject: [PATCH 2/6] markdown tables custom remark plugin p1 --- .../markdown/StyledMarkdownPreview.tsx | 5 +- .../components/markdown/markdown-tables.css | 11 + .../markdown/utils/remarkTables.tsx | 233 ++++++++++++++++++ 3 files changed, 247 insertions(+), 2 deletions(-) create mode 100644 gui/src/components/markdown/markdown-tables.css create mode 100644 gui/src/components/markdown/utils/remarkTables.tsx 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("|"); +} From 34ca668d54364454821589719fc686b48aad8acb Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 3 Dec 2024 00:28:47 -0500 Subject: [PATCH 3/6] markdown tables: regex tinkering --- .../markdown/StyledMarkdownPreview.tsx | 2 +- .../markdown/utils/remarkTables.tsx | 20 ++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index eee5eb223..6df96df05 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -180,7 +180,7 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( const [reactContent, setMarkdownSource] = useRemark({ remarkPlugins: [ // remarkTables, - // remarkGfm, + remarkGfm, remarkMath, () => processCodeBlocks, ], diff --git a/gui/src/components/markdown/utils/remarkTables.tsx b/gui/src/components/markdown/utils/remarkTables.tsx index d8e5bc84e..797e9ed08 100644 --- a/gui/src/components/markdown/utils/remarkTables.tsx +++ b/gui/src/components/markdown/utils/remarkTables.tsx @@ -98,22 +98,35 @@ // }; // } -const visit = require("unist-util-visit"); +import { visit } from "unist-util-visit"; export function remarkTables() { return (tree) => { visit(tree, "text", (node, index, parent) => { const { value } = node; + // console.log("NODE", value); // 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; + /((?:\| *[^|\n]+ *)+\|)(?:\r?\n)((?:\|[ :]?-+[ :]?)+\|)((?:(?:\r?\n)(?:\| *[^|\r\n]+ *)+\|)+)/g; + // /((?:\| *[^|\r\n]+ *)+\|)(?:\r?\n)((?:\|:?-+:?)+\|)((?:(?:\r?\n)(?:\| *[^|\r\n]+ *)+\|)+)/g; + // /((?:\| +[A-Za-z0-9 -_*#@$%:;?!.,\/\\]+ +)+\|)(?:\r?\n| )?((?:\|:?-+:?)+\|)((?:(?:\r?\n| )?(?:\| +[A-Za-z0-9 -_*#@$%:;?!.,\/\\^~]+ +)+\|)+)/g; // accepts space or nothing as separator between + // should probably change the (?:\r?\n| )? groups to (?:\r?\n) + // consider limiting table cells to chars e.g. A-Za-z0-9 -_*#@$%:;?!.,\/\\^~`{} + // /^(\|[^\n]+\|(?:\r?\n| ))((?:\|:?[-]+:?)+\|)((?:\r?\n| )(?:\|[^\n]+\|(?:\r?\n| ))*)?$/gm; + + let match: RegExpExecArray | null; let lastIndex = 0; const newNodes = []; + const matches = tableRegex.exec(value); //value.match(tableRegex); + // console.log("MATCHES FOR\n\n" + value, matches); + while ((match = tableRegex.exec(value)) !== null) { + // if(match.groups === 3){ console.log(match); + + // } const tableText = match[0]; // Add any text before the table as a text node @@ -156,6 +169,7 @@ export function remarkTables() { // The parseTable function remains the same as before function parseTable(tableText) { + // const header = tableText; const lines = tableText.trim().split(/\r?\n/); if (lines.length < 2) return null; // Not enough lines for a valid table From 73f614e28e0f70656baf96dfe2bb4e73e8d42434 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 3 Dec 2024 15:39:10 -0800 Subject: [PATCH 4/6] cleaned up, working remarktables plugin --- .../markdown/StyledMarkdownPreview.tsx | 33 +- .../components/markdown/markdown-tables.css | 11 - .../markdown/utils/remarkTables.tsx | 296 ++++++------------ 3 files changed, 111 insertions(+), 229 deletions(-) delete mode 100644 gui/src/components/markdown/markdown-tables.css diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index 6df96df05..6afb7c09d 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -15,7 +15,6 @@ 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"; @@ -179,9 +178,13 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( const [reactContent, setMarkdownSource] = useRemark({ remarkPlugins: [ - // remarkTables, - remarkGfm, - remarkMath, + remarkTables, + [ + remarkMath, + { + singleDollarTextMath: false, + }, + ], () => processCodeBlocks, ], rehypePlugins: [ @@ -195,17 +198,17 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( { // languages: {}, } as Options, - // () => { - // let codeBlockIndex = 0; - // return (tree) => { - // visit(tree, { tagName: "pre" }, (node: any) => { - // Add an index (0, 1, 2, etc...) to each code block. - // node.properties = { codeBlockIndex }; - // codeBlockIndex++; - // }); - // }; - // }, - // {}, + () => { + let codeBlockIndex = 0; + return (tree) => { + visit(tree, { tagName: "pre" }, (node: any) => { + // Add an index (0, 1, 2, etc...) to each code block. + node.properties = { codeBlockIndex }; + codeBlockIndex++; + }); + }; + }, + {}, ], rehypeReactOptions: { components: { diff --git a/gui/src/components/markdown/markdown-tables.css b/gui/src/components/markdown/markdown-tables.css deleted file mode 100644 index e0f80b6db..000000000 --- a/gui/src/components/markdown/markdown-tables.css +++ /dev/null @@ -1,11 +0,0 @@ -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 index 797e9ed08..8a1730529 100644 --- a/gui/src/components/markdown/utils/remarkTables.tsx +++ b/gui/src/components/markdown/utils/remarkTables.tsx @@ -1,150 +1,111 @@ -// 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); -// } -// }); -// }; -// } - import { visit } from "unist-util-visit"; +/* + Remark plugin for github-flavor markdown tables + Given a table such as this exists in a text node: + + | Fruit | Color | Taste | + |----------|---------|-------------| + | Apple | Red | Sweet | + | Banana | Yellow | Sweet | + | Lemon | Yellow | Sour | + | Orange | Orange | Citrus | + | Grape | Purple | Sweet/Tart | + + 1. Find it and split it into groups using regex + - header + - alignment row + - body + 2. Parse the groups to get table cell values and text alignment + 3. Build an MDAST table node + 4. Do this for each table found, until no tables are found +*/ export function remarkTables() { - return (tree) => { + return (tree: any) => { visit(tree, "text", (node, index, parent) => { const { value } = node; - // console.log("NODE", value); - // Inspired by - // https://stackoverflow.com/questions/9837935/regex-for-markdown-table-syntax + const tableRegex = - /((?:\| *[^|\n]+ *)+\|)(?:\r?\n)((?:\|[ :]?-+[ :]?)+\|)((?:(?:\r?\n)(?:\| *[^|\r\n]+ *)+\|)+)/g; - // /((?:\| *[^|\r\n]+ *)+\|)(?:\r?\n)((?:\|:?-+:?)+\|)((?:(?:\r?\n)(?:\| *[^|\r\n]+ *)+\|)+)/g; - // /((?:\| +[A-Za-z0-9 -_*#@$%:;?!.,\/\\]+ +)+\|)(?:\r?\n| )?((?:\|:?-+:?)+\|)((?:(?:\r?\n| )?(?:\| +[A-Za-z0-9 -_*#@$%:;?!.,\/\\^~]+ +)+\|)+)/g; // accepts space or nothing as separator between - // should probably change the (?:\r?\n| )? groups to (?:\r?\n) - // consider limiting table cells to chars e.g. A-Za-z0-9 -_*#@$%:;?!.,\/\\^~`{} - // /^(\|[^\n]+\|(?:\r?\n| ))((?:\|:?[-]+:?)+\|)((?:\r?\n| )(?:\|[^\n]+\|(?:\r?\n| ))*)?$/gm; + /((?:\| *[^|\r\n]+ *)+\|)(?:\r?\n)((?:\|[ :]?-+[ :]?)+\|)((?:(?:\r?\n)(?:\| *[^|\r\n]+ *)+\|)+)/g; + //// header // newline // |:---|----:| // new line // table rows let match: RegExpExecArray | null; let lastIndex = 0; const newNodes = []; - const matches = tableRegex.exec(value); //value.match(tableRegex); - // console.log("MATCHES FOR\n\n" + value, matches); - while ((match = tableRegex.exec(value)) !== null) { - // if(match.groups === 3){ - console.log(match); + const fullTableString = match[0]; + const headerGroup = match[1]; + const separatorGroup = match[2]; + const bodyGroup = match[3]; - // } - 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), - }); + if (!fullTableString || !headerGroup || !separatorGroup || !bodyGroup) { + console.error("Markdown table regex failed to yield table groups"); + return; } - const tableNode = parseTable(tableText); - if (tableNode) { + const headerCells = splitRow(headerGroup); + const alignments = splitRow(separatorGroup).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 bodyRows = bodyGroup + .trim() + .split("\n") + .map((bodyRow) => splitRow(bodyRow.trim())); + + try { + const tableNode = { + type: "table", + align: alignments, + children: [ + { + type: "tableRow", + children: headerCells.map((cell, i) => ({ + type: "element", + tagName: "th", + align: alignments[i], + children: [{ type: "text", value: cell }], + })), + }, + ...bodyRows.map((row, i) => { + return { + type: "tableRow", + data: { + hProperties: { + class: "markdown-table", + key: i, + }, + }, + children: row.map((cell, i) => ({ + type: "tableCell", + align: alignments[i], + children: [{ type: "text", value: cell.trim() }], + })), + }; + }), + ], + }; + + // Add any text before the table as a text node + if (match.index > lastIndex) { + newNodes.push({ + type: "text", + value: value.slice(lastIndex, match.index), + }); + } + + // Add table node newNodes.push(tableNode); - } else { - // If parsing failed, keep the original text + } catch (e) { + console.error("Failed to parse markdown table after regex match", e); newNodes.push({ type: "text", - value: tableText, + value: fullTableString, }); } @@ -167,81 +128,10 @@ export function remarkTables() { }; } -// The parseTable function remains the same as before -function parseTable(tableText) { - // const header = 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) { +function splitRow(row: string) { return row .trim() .replace(/^\||\|$/g, "") // Remove leading and trailing pipes - .split("|"); + .split("|") + .map((cell) => cell.trim()); } From 60923cb9cad97e2f1395415de76084eee45926aa Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 3 Dec 2024 15:41:16 -0800 Subject: [PATCH 5/6] remove remarkgfm package --- gui/package-lock.json | 276 ------------------------------------------ gui/package.json | 1 - 2 files changed, 277 deletions(-) diff --git a/gui/package-lock.json b/gui/package-lock.json index 7546cc341..91410e6df 100644 --- a/gui/package-lock.json +++ b/gui/package-lock.json @@ -52,7 +52,6 @@ "rehype-highlight": "^7.0.0", "rehype-katex": "^7.0.0", "rehype-wrap-all": "^1.1.0", - "remark-gfm": "^4.0.0", "remark-math": "^6.0.0", "reselect": "^5.1.1", "seti-file-icons": "^0.0.8", @@ -8099,15 +8098,6 @@ "markdown-it": "bin/markdown-it.mjs" } }, - "node_modules/markdown-table": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/markdown-table/-/markdown-table-3.0.4.tgz", - "integrity": "sha512-wiYz4+JrLyb/DqW2hkFJxP7Vd7JuTDm77fvbM8VfEQdmSMqcImWeeRbHwZjBjIFki/VaMK2BhFi7oUUZeM5bqw==", - "funding": { - "type": "github", - "url": "https://github.com/sponsors/wooorm" - } - }, "node_modules/mdast-util-definitions": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/mdast-util-definitions/-/mdast-util-definitions-4.0.0.tgz", @@ -8161,32 +8151,6 @@ "url": "https://opencollective.com/unified" } }, - "node_modules/mdast-util-find-and-replace": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/mdast-util-find-and-replace/-/mdast-util-find-and-replace-3.0.1.tgz", - "integrity": "sha512-SG21kZHGC3XRTSUhtofZkBzZTJNM5ecCi0SK2IMKmSXR8vO3peL+kb1O0z7Zl83jKtutG4k5Wv/W7V3/YHvzPA==", - "dependencies": { - "@types/mdast": "^4.0.0", - "escape-string-regexp": "^5.0.0", - "unist-util-is": "^6.0.0", - "unist-util-visit-parents": "^6.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, - "node_modules/mdast-util-find-and-replace/node_modules/escape-string-regexp": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-5.0.0.tgz", - "integrity": "sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/mdast-util-from-markdown": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/mdast-util-from-markdown/-/mdast-util-from-markdown-2.0.1.tgz", @@ -8210,101 +8174,6 @@ "url": "https://opencollective.com/unified" } }, - "node_modules/mdast-util-gfm": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/mdast-util-gfm/-/mdast-util-gfm-3.0.0.tgz", - "integrity": "sha512-dgQEX5Amaq+DuUqf26jJqSK9qgixgd6rYDHAv4aTBuA92cTknZlKpPfa86Z/s8Dj8xsAQpFfBmPUHWJBWqS4Bw==", - "dependencies": { - "mdast-util-from-markdown": "^2.0.0", - "mdast-util-gfm-autolink-literal": "^2.0.0", - "mdast-util-gfm-footnote": "^2.0.0", - "mdast-util-gfm-strikethrough": "^2.0.0", - "mdast-util-gfm-table": "^2.0.0", - "mdast-util-gfm-task-list-item": "^2.0.0", - "mdast-util-to-markdown": "^2.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, - "node_modules/mdast-util-gfm-autolink-literal": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/mdast-util-gfm-autolink-literal/-/mdast-util-gfm-autolink-literal-2.0.1.tgz", - "integrity": "sha512-5HVP2MKaP6L+G6YaxPNjuL0BPrq9orG3TsrZ9YXbA3vDw/ACI4MEsnoDpn6ZNm7GnZgtAcONJyPhOP8tNJQavQ==", - "dependencies": { - "@types/mdast": "^4.0.0", - "ccount": "^2.0.0", - "devlop": "^1.0.0", - "mdast-util-find-and-replace": "^3.0.0", - "micromark-util-character": "^2.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, - "node_modules/mdast-util-gfm-footnote": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/mdast-util-gfm-footnote/-/mdast-util-gfm-footnote-2.0.0.tgz", - "integrity": "sha512-5jOT2boTSVkMnQ7LTrd6n/18kqwjmuYqo7JUPe+tRCY6O7dAuTFMtTPauYYrMPpox9hlN0uOx/FL8XvEfG9/mQ==", - "dependencies": { - "@types/mdast": "^4.0.0", - "devlop": "^1.1.0", - "mdast-util-from-markdown": "^2.0.0", - "mdast-util-to-markdown": "^2.0.0", - "micromark-util-normalize-identifier": "^2.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, - "node_modules/mdast-util-gfm-strikethrough": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/mdast-util-gfm-strikethrough/-/mdast-util-gfm-strikethrough-2.0.0.tgz", - "integrity": "sha512-mKKb915TF+OC5ptj5bJ7WFRPdYtuHv0yTRxK2tJvi+BDqbkiG7h7u/9SI89nRAYcmap2xHQL9D+QG/6wSrTtXg==", - "dependencies": { - "@types/mdast": "^4.0.0", - "mdast-util-from-markdown": "^2.0.0", - "mdast-util-to-markdown": "^2.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, - "node_modules/mdast-util-gfm-table": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/mdast-util-gfm-table/-/mdast-util-gfm-table-2.0.0.tgz", - "integrity": "sha512-78UEvebzz/rJIxLvE7ZtDd/vIQ0RHv+3Mh5DR96p7cS7HsBhYIICDBCu8csTNWNO6tBWfqXPWekRuj2FNOGOZg==", - "dependencies": { - "@types/mdast": "^4.0.0", - "devlop": "^1.0.0", - "markdown-table": "^3.0.0", - "mdast-util-from-markdown": "^2.0.0", - "mdast-util-to-markdown": "^2.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, - "node_modules/mdast-util-gfm-task-list-item": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/mdast-util-gfm-task-list-item/-/mdast-util-gfm-task-list-item-2.0.0.tgz", - "integrity": "sha512-IrtvNvjxC1o06taBAVJznEnkiHxLFTzgonUdy8hzFVeDun0uTjxxrRGVaNFqkU1wJR3RBPEfsxmU6jDWPofrTQ==", - "dependencies": { - "@types/mdast": "^4.0.0", - "devlop": "^1.0.0", - "mdast-util-from-markdown": "^2.0.0", - "mdast-util-to-markdown": "^2.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, "node_modules/mdast-util-math": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/mdast-util-math/-/mdast-util-math-3.0.0.tgz", @@ -8531,120 +8400,6 @@ "micromark-util-types": "^2.0.0" } }, - "node_modules/micromark-extension-gfm": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/micromark-extension-gfm/-/micromark-extension-gfm-3.0.0.tgz", - "integrity": "sha512-vsKArQsicm7t0z2GugkCKtZehqUm31oeGBV/KVSorWSy8ZlNAv7ytjFhvaryUiCUJYqs+NoE6AFhpQvBTM6Q4w==", - "dependencies": { - "micromark-extension-gfm-autolink-literal": "^2.0.0", - "micromark-extension-gfm-footnote": "^2.0.0", - "micromark-extension-gfm-strikethrough": "^2.0.0", - "micromark-extension-gfm-table": "^2.0.0", - "micromark-extension-gfm-tagfilter": "^2.0.0", - "micromark-extension-gfm-task-list-item": "^2.0.0", - "micromark-util-combine-extensions": "^2.0.0", - "micromark-util-types": "^2.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, - "node_modules/micromark-extension-gfm-autolink-literal": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/micromark-extension-gfm-autolink-literal/-/micromark-extension-gfm-autolink-literal-2.1.0.tgz", - "integrity": "sha512-oOg7knzhicgQ3t4QCjCWgTmfNhvQbDDnJeVu9v81r7NltNCVmhPy1fJRX27pISafdjL+SVc4d3l48Gb6pbRypw==", - "dependencies": { - "micromark-util-character": "^2.0.0", - "micromark-util-sanitize-uri": "^2.0.0", - "micromark-util-symbol": "^2.0.0", - "micromark-util-types": "^2.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, - "node_modules/micromark-extension-gfm-footnote": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/micromark-extension-gfm-footnote/-/micromark-extension-gfm-footnote-2.1.0.tgz", - "integrity": "sha512-/yPhxI1ntnDNsiHtzLKYnE3vf9JZ6cAisqVDauhp4CEHxlb4uoOTxOCJ+9s51bIB8U1N1FJ1RXOKTIlD5B/gqw==", - "dependencies": { - "devlop": "^1.0.0", - "micromark-core-commonmark": "^2.0.0", - "micromark-factory-space": "^2.0.0", - "micromark-util-character": "^2.0.0", - "micromark-util-normalize-identifier": "^2.0.0", - "micromark-util-sanitize-uri": "^2.0.0", - "micromark-util-symbol": "^2.0.0", - "micromark-util-types": "^2.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, - "node_modules/micromark-extension-gfm-strikethrough": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/micromark-extension-gfm-strikethrough/-/micromark-extension-gfm-strikethrough-2.1.0.tgz", - "integrity": "sha512-ADVjpOOkjz1hhkZLlBiYA9cR2Anf8F4HqZUO6e5eDcPQd0Txw5fxLzzxnEkSkfnD0wziSGiv7sYhk/ktvbf1uw==", - "dependencies": { - "devlop": "^1.0.0", - "micromark-util-chunked": "^2.0.0", - "micromark-util-classify-character": "^2.0.0", - "micromark-util-resolve-all": "^2.0.0", - "micromark-util-symbol": "^2.0.0", - "micromark-util-types": "^2.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, - "node_modules/micromark-extension-gfm-table": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/micromark-extension-gfm-table/-/micromark-extension-gfm-table-2.1.0.tgz", - "integrity": "sha512-Ub2ncQv+fwD70/l4ou27b4YzfNaCJOvyX4HxXU15m7mpYY+rjuWzsLIPZHJL253Z643RpbcP1oeIJlQ/SKW67g==", - "dependencies": { - "devlop": "^1.0.0", - "micromark-factory-space": "^2.0.0", - "micromark-util-character": "^2.0.0", - "micromark-util-symbol": "^2.0.0", - "micromark-util-types": "^2.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, - "node_modules/micromark-extension-gfm-tagfilter": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/micromark-extension-gfm-tagfilter/-/micromark-extension-gfm-tagfilter-2.0.0.tgz", - "integrity": "sha512-xHlTOmuCSotIA8TW1mDIM6X2O1SiX5P9IuDtqGonFhEK0qgRI4yeC6vMxEV2dgyr2TiD+2PQ10o+cOhdVAcwfg==", - "dependencies": { - "micromark-util-types": "^2.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, - "node_modules/micromark-extension-gfm-task-list-item": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/micromark-extension-gfm-task-list-item/-/micromark-extension-gfm-task-list-item-2.1.0.tgz", - "integrity": "sha512-qIBZhqxqI6fjLDYFTBIa4eivDMnP+OZqsNwmQ3xNLE4Cxwc+zfQEfbs6tzAo2Hjq+bh6q5F+Z8/cksrLFYWQQw==", - "dependencies": { - "devlop": "^1.0.0", - "micromark-factory-space": "^2.0.0", - "micromark-util-character": "^2.0.0", - "micromark-util-symbol": "^2.0.0", - "micromark-util-types": "^2.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, "node_modules/micromark-extension-math": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/micromark-extension-math/-/micromark-extension-math-3.1.0.tgz", @@ -11084,23 +10839,6 @@ "url": "https://opencollective.com/unified" } }, - "node_modules/remark-gfm": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/remark-gfm/-/remark-gfm-4.0.0.tgz", - "integrity": "sha512-U92vJgBPkbw4Zfu/IiW2oTZLSL3Zpv+uI7My2eq8JxKgqraFdU8YUGicEJCEgSbeaG+QDFqIcwwfMTOEelPxuA==", - "dependencies": { - "@types/mdast": "^4.0.0", - "mdast-util-gfm": "^3.0.0", - "micromark-extension-gfm": "^3.0.0", - "remark-parse": "^11.0.0", - "remark-stringify": "^11.0.0", - "unified": "^11.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, "node_modules/remark-math": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/remark-math/-/remark-math-6.0.0.tgz", @@ -11147,20 +10885,6 @@ "url": "https://opencollective.com/unified" } }, - "node_modules/remark-stringify": { - "version": "11.0.0", - "resolved": "https://registry.npmjs.org/remark-stringify/-/remark-stringify-11.0.0.tgz", - "integrity": "sha512-1OSmLd3awB/t8qdoEOMazZkNsfVTeY4fTsgzcQFdXNq8ToTN4ZGwrMnlda4K6smTFKD+GRV6O48i6Z4iKgPPpw==", - "dependencies": { - "@types/mdast": "^4.0.0", - "mdast-util-to-markdown": "^2.0.0", - "unified": "^11.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, "node_modules/require-from-string": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", diff --git a/gui/package.json b/gui/package.json index 3d8faabed..b5e16103d 100644 --- a/gui/package.json +++ b/gui/package.json @@ -60,7 +60,6 @@ "rehype-highlight": "^7.0.0", "rehype-katex": "^7.0.0", "rehype-wrap-all": "^1.1.0", - "remark-gfm": "^4.0.0", "remark-math": "^6.0.0", "reselect": "^5.1.1", "seti-file-icons": "^0.0.8", From df759312c85b9bac67c980d7814b5cf6b1988eea Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 3 Dec 2024 16:47:16 -0800 Subject: [PATCH 6/6] remark markdown table cleanup --- .../markdown/utils/remarkTables.tsx | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/gui/src/components/markdown/utils/remarkTables.tsx b/gui/src/components/markdown/utils/remarkTables.tsx index 8a1730529..f22542c2f 100644 --- a/gui/src/components/markdown/utils/remarkTables.tsx +++ b/gui/src/components/markdown/utils/remarkTables.tsx @@ -32,7 +32,7 @@ export function remarkTables() { let match: RegExpExecArray | null; let lastIndex = 0; const newNodes = []; - + let failed = false; while ((match = tableRegex.exec(value)) !== null) { const fullTableString = match[0]; const headerGroup = match[1]; @@ -41,22 +41,22 @@ export function remarkTables() { if (!fullTableString || !headerGroup || !separatorGroup || !bodyGroup) { console.error("Markdown table regex failed to yield table groups"); - return; + failed = true; + break; } const headerCells = splitRow(headerGroup); const alignments = splitRow(separatorGroup).map((cell) => { - const trimmed = cell.trim(); - if (trimmed.startsWith(":") && trimmed.endsWith(":")) return "center"; - if (trimmed.endsWith(":")) return "right"; - if (trimmed.startsWith(":")) return "left"; + if (cell.startsWith(":") && cell.endsWith(":")) return "center"; + if (cell.endsWith(":")) return "right"; + if (cell.startsWith(":")) return "left"; return null; }); - const bodyRows = bodyGroup + const bodyCells = bodyGroup .trim() .split("\n") - .map((bodyRow) => splitRow(bodyRow.trim())); + .map((bodyRow) => splitRow(bodyRow)); try { const tableNode = { @@ -72,7 +72,7 @@ export function remarkTables() { children: [{ type: "text", value: cell }], })), }, - ...bodyRows.map((row, i) => { + ...bodyCells.map((row, i) => { return { type: "tableRow", data: { @@ -112,6 +112,10 @@ export function remarkTables() { lastIndex = tableRegex.lastIndex; } + if (failed) { + return; + } + // Add any remaining text after the last table if (lastIndex < value.length) { newNodes.push({