From b02e6ba660b493fcd62742dfb2010c90b88a2656 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 27 Feb 2025 11:47:20 -0800 Subject: [PATCH 1/2] fix markdown patch --- .../markdown/utils/patchNestedMarkdown.ts | 68 ++++--------------- 1 file changed, 13 insertions(+), 55 deletions(-) diff --git a/gui/src/components/markdown/utils/patchNestedMarkdown.ts b/gui/src/components/markdown/utils/patchNestedMarkdown.ts index 29b305b26..b8be69ae7 100644 --- a/gui/src/components/markdown/utils/patchNestedMarkdown.ts +++ b/gui/src/components/markdown/utils/patchNestedMarkdown.ts @@ -8,9 +8,8 @@ // TODO support github-specific markdown as well, edge case */ - export const patchNestedMarkdown = (source: string): string => { - if (!source.match(/```(md|markdown)/)) return source; // For performance + if (!source.match(/```(\w+\.(md|markdown))/)) return source; // For performance // const start = Date.now(); let nestCount = 0; const lines = source.split("\n"); @@ -28,9 +27,18 @@ export const patchNestedMarkdown = (source: string): string => { } } else { // Enter the markdown block, start tracking nesting - if (line.startsWith("```md") || line.startsWith("```markdown")) { - nestCount = 1; - lines[i] = lines[i].replace(/```(md|markdown)/, "~~~"); // Replace backticks with tildes + if (line.startsWith("```")) { + const header = line.replaceAll("`", ""); + const file = header.split(" ")[0]; + + if (file) { + const ext = file.split(".").at(-1); + debugger; + if (ext === "md" || ext === "markdown") { + nestCount = 1; + lines[i] = lines[i].replaceAll("`", "~"); // Replace backticks with tildes + } + } } } } @@ -38,53 +46,3 @@ export const patchNestedMarkdown = (source: string): string => { // console.log(`patched in ${Date.now() - start}ms`); return out; }; - -// This version tries to detect if a codeblock without a language specified is a starter codeblock -// It tries again if didn't come back to root nesting, without checking for that ^ -// I didn't use for performance and also because the root nest check doesn't make sense mid-generation - -// export const patchNestedMarkdown = (source: string): string => { -// const start = Date.now(); - -// let attempts = 0; - -// while (attempts <= 2) { -// let nestCount = 0; -// const lines = source.split("\n"); -// const trimmedLines = lines.map((l) => l.trim()); -// for (let i = 0; i < trimmedLines.length; i++) { -// const line = trimmedLines[i]; -// if (nestCount) { -// if (line.match(/^`+$/)) { -// if (attempts === 0 && i !== lines.length && lines[i + 1] !== "") { -// nestCount++; -// continue; -// } -// if (nestCount === 1) lines[i] = "~~~"; -// nestCount--; -// } else if (line.startsWith("```")) { -// // Going into a nested codeblock -// nestCount++; -// } -// } else { -// // Enter the markdown block, start tracking nesting -// if (line.startsWith("```md") || line.startsWith("```markdown")) { -// nestCount = 1; -// lines[i] = lines[i].replace(/^```(md|markdown)/, "~~~"); // Replace backticks with tildes -// } -// } -// } -// if (nestCount === 0) { -// console.log( -// `patch successful in ${Date.now() - start} ms with ${attempts} attempts`, -// ); -// return lines.join("\n"); -// } -// attempts++; -// } - -// console.log( -// `patch failed in ${Date.now() - start} ms with ${attempts} attempts`, -// ); -// return source; -// }; From e2e8e3990a40fe4ff49d8ce6ac21f1a85e1f5b62 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 27 Feb 2025 11:49:01 -0800 Subject: [PATCH 2/2] cleanup --- gui/src/components/markdown/utils/patchNestedMarkdown.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/gui/src/components/markdown/utils/patchNestedMarkdown.ts b/gui/src/components/markdown/utils/patchNestedMarkdown.ts index b8be69ae7..10d5af1b2 100644 --- a/gui/src/components/markdown/utils/patchNestedMarkdown.ts +++ b/gui/src/components/markdown/utils/patchNestedMarkdown.ts @@ -33,7 +33,6 @@ export const patchNestedMarkdown = (source: string): string => { if (file) { const ext = file.split(".").at(-1); - debugger; if (ext === "md" || ext === "markdown") { nestCount = 1; lines[i] = lines[i].replaceAll("`", "~"); // Replace backticks with tildes