From 00fc90badca92b6d03c40eb2e2e3462ba3213aaf Mon Sep 17 00:00:00 2001 From: chezsmithy Date: Fri, 27 Jun 2025 16:00:29 -0700 Subject: [PATCH] fix: :bug: Project structure creation with bare codeblocks is breaking markdown display and creation --- .../utils/isOpeningNestedBlock.test.ts | 157 ++++++++++++++++++ .../utils/isOpeningNestedBlock.ts | 66 ++++++++ .../utils/patchNestedMarkdown.test.ts | 8 + .../utils/patchNestedMarkdown.ts | 15 +- 4 files changed, 242 insertions(+), 4 deletions(-) create mode 100644 gui/src/components/StyledMarkdownPreview/utils/isOpeningNestedBlock.test.ts create mode 100644 gui/src/components/StyledMarkdownPreview/utils/isOpeningNestedBlock.ts diff --git a/gui/src/components/StyledMarkdownPreview/utils/isOpeningNestedBlock.test.ts b/gui/src/components/StyledMarkdownPreview/utils/isOpeningNestedBlock.test.ts new file mode 100644 index 000000000..e5eb02f0c --- /dev/null +++ b/gui/src/components/StyledMarkdownPreview/utils/isOpeningNestedBlock.test.ts @@ -0,0 +1,157 @@ +import { expect, test } from "vitest"; +import { isOpeningNestedBlock } from "./isOpeningNestedBlock"; + +test("isOpeningNestedBlock should return true when odd number of bare backticks ahead", () => { + const trimmedLines = [ + "```markdown", + "# Header", + + "```", // <- index 2: should be opening (1 ``` ahead = odd) + "content", + + "```", // <- index 4: closing (0 ``` ahead = even) + ]; + + expect(isOpeningNestedBlock(trimmedLines, 2, 1)).toBe(true); + expect(isOpeningNestedBlock(trimmedLines, 4, 2)).toBe(false); +}); + +test("isOpeningNestedBlock should return false when even number (zero) of bare backticks ahead", () => { + const trimmedLines = [ + "```markdown", + "# Header", + + "```", // <- index 2: should be closing (0 ``` ahead = even) + ]; + + expect(isOpeningNestedBlock(trimmedLines, 2, 1)).toBe(false); +}); + +test("isOpeningNestedBlock should handle multiple nested pairs", () => { + const trimmedLines = [ + "```markdown", + "# Header", + + "```", // <- index 2: opening (3 ``` ahead = odd) + "```", // <- index 3: opening (2 ``` ahead = even) -> should be false + "content", + + "```", // <- index 5: closing (1 ``` ahead = odd) -> should be true + "```", // <- index 6: closing (0 ``` ahead = even) -> should be false + ]; + + expect(isOpeningNestedBlock(trimmedLines, 2, 1)).toBe(true); // 3 ahead = odd + expect(isOpeningNestedBlock(trimmedLines, 3, 2)).toBe(false); // 2 ahead = even + expect(isOpeningNestedBlock(trimmedLines, 5, 2)).toBe(true); // 1 ahead = odd + expect(isOpeningNestedBlock(trimmedLines, 6, 1)).toBe(false); // 0 ahead = even +}); + +test("isOpeningNestedBlock should return false when reaching end of markdown block via ~~~", () => { + const trimmedLines = [ + "```markdown", + "# Header", + + "```", // <- index 2: should be closing since ~~~ terminates the block + "content", + + "~~~", // <- terminator stops counting + "```", // <- this one is after the markdown block ends + ]; + + expect(isOpeningNestedBlock(trimmedLines, 2, 1)).toBe(false); +}); + +test("isOpeningNestedBlock should return false when reaching new markdown block", () => { + const trimmedLines = [ + "```markdown", + "# Header", + + "```", // <- index 2: should be closing since new markdown block starts + "content", + + "```md", // <- new markdown block terminates counting + "```", // <- this one belongs to the new block + ]; + + expect(isOpeningNestedBlock(trimmedLines, 2, 1)).toBe(false); +}); + +test("isOpeningNestedBlock should handle empty content between backticks", () => { + const trimmedLines = [ + "```markdown", + + "```", // <- index 1: opening (1 ``` ahead = odd) + "", + + "```", // <- index 3: closing (0 ``` ahead = even) + ]; + + expect(isOpeningNestedBlock(trimmedLines, 1, 1)).toBe(true); + expect(isOpeningNestedBlock(trimmedLines, 3, 2)).toBe(false); +}); + +test("isOpeningNestedBlock should handle case where currentIndex is last line", () => { + const trimmedLines = [ + "```markdown", + "# Header", + + "```", // <- index 2: last line, should be closing (0 ahead = even) + ]; + + expect(isOpeningNestedBlock(trimmedLines, 2, 1)).toBe(false); +}); + +test("isOpeningNestedBlock should not be confused by backticks in content", () => { + const trimmedLines = [ + "```markdown", + "Some `code` with backticks", + + "```", // <- index 2: should be closing (0 bare ``` ahead = even) + ]; + + expect(isOpeningNestedBlock(trimmedLines, 2, 1)).toBe(false); +}); + +test("isOpeningNestedBlock should handle GitHub markdown variants as terminators", () => { + const trimmedLines = [ + "```markdown", + "# Header", + + "```", // <- index 2: should be closing (terminator stops counting) + "content", + + "```gfm", // <- GitHub markdown variant starts new block + "```", + ]; + + expect(isOpeningNestedBlock(trimmedLines, 2, 1)).toBe(false); +}); + +test("isOpeningNestedBlock should only check terminators at nest level 1", () => { + const trimmedLines = [ + "```markdown", + + "```", // <- index 1: at nest level 1, ~~~ should terminate counting + "~~~", // <- terminator found, stops counting + "```", // <- this doesn't count towards the logic + ]; + + expect(isOpeningNestedBlock(trimmedLines, 1, 1)).toBe(false); +}); + +test("isOpeningNestedBlock should handle complex realistic markdown structure", () => { + const trimmedLines = [ + "```markdown README.md", + "# Project Structure", + "", + "```", // <- index 3: has 2 ``` ahead (even), so should be closing + ".", + "│ └── file.ts", + "```", // <- index 7: has 1 ``` ahead (odd), so should be opening + "```", // <- index 8: has 0 ``` ahead (even), so should be closing + ]; + + expect(isOpeningNestedBlock(trimmedLines, 3, 1)).toBe(false); // 2 ahead = even + expect(isOpeningNestedBlock(trimmedLines, 7, 2)).toBe(true); // 1 ahead = odd + expect(isOpeningNestedBlock(trimmedLines, 8, 1)).toBe(false); // 0 ahead = even +}); diff --git a/gui/src/components/StyledMarkdownPreview/utils/isOpeningNestedBlock.ts b/gui/src/components/StyledMarkdownPreview/utils/isOpeningNestedBlock.ts new file mode 100644 index 000000000..8327ed219 --- /dev/null +++ b/gui/src/components/StyledMarkdownPreview/utils/isOpeningNestedBlock.ts @@ -0,0 +1,66 @@ +import { headerIsMarkdown } from "./headerIsMarkdown"; + +/** + * Determines if a line with bare backticks (```) should be treated as opening + * a nested code block rather than closing the current block. + * + * Uses look-ahead to check if there are more bare backtick lines ahead, + * which would indicate this is opening a nested block. + * + * @param trimmedLines - Array of trimmed lines from the source + * @param currentIndex - Index of the current line with bare backticks + * @param nestCount - Current nesting level + * @returns true if this should open a nested block, false if it should close + */ +export function isOpeningNestedBlock( + trimmedLines: string[], + currentIndex: number, + nestCount: number, +): boolean { + // Simulate what would happen if we continue processing from this point + // Count how many blocks would need to be closed vs opened + + let simulatedNestCount = nestCount; + let bareBackticksAhead = 0; + + for (let j = currentIndex + 1; j < trimmedLines.length; j++) { + const line = trimmedLines[j]; + + // Count bare backticks lines + if (line.match(/^`+$/)) { + bareBackticksAhead++; + } else if (line.startsWith("```")) { + // Non-bare backticks (with language/type) always open + simulatedNestCount++; + } + + // Stop looking if we hit what appears to be the end of current markdown block + if (nestCount === 1) { + // At top level of markdown block - check for block terminators + if ( + line.startsWith("~~~") || + (line.startsWith("```") && headerIsMarkdown(line.replaceAll("`", ""))) + ) { + break; + } + } + } + + // The key insight: if we need the current backticks to close to get back to nestCount=1, + // and there are more bare backticks ahead than what we'd need to close everything, + // then this should open + + if (nestCount > 1) { + // We're deeply nested, usually should close + return bareBackticksAhead > nestCount - 1; + } + + // At top level (nestCount === 1), if there are exactly 2 ahead and we're at top level, + // this suggests: current=open, first_ahead=close, second_ahead=close_markdown + if (nestCount === 1 && bareBackticksAhead === 2) { + return true; + } + + // Default to the original logic + return bareBackticksAhead % 2 === 1; +} diff --git a/gui/src/components/StyledMarkdownPreview/utils/patchNestedMarkdown.test.ts b/gui/src/components/StyledMarkdownPreview/utils/patchNestedMarkdown.test.ts index af94f118d..4e347a9e2 100644 --- a/gui/src/components/StyledMarkdownPreview/utils/patchNestedMarkdown.test.ts +++ b/gui/src/components/StyledMarkdownPreview/utils/patchNestedMarkdown.test.ts @@ -6,6 +6,14 @@ describe("patchNestedMarkdown", () => { expect(patchNestedMarkdown(source)).toBe(source); }); + it("should handle nested blocks without a type", () => { + const source = + "```markdown README.md\n# Project Structure\n\n```\n.\n├── AdvancedPage.tsx\n├── Calculator.java\n├── Dockerfile\n├── calculator_test/\n│ ├── Calculator.java\n│ └── Main.java\n├── data.json\n├── example.ipynb\n├── logs/\n├── nested-folder/\n│ ├── helloNested.py\n│ ├── package.json\n│ └── rules.md\n├── program.cs\n├── query.sql\n├── react-calculator/\n├── readme.md\n├── requirements.txt\n├── test.css\n├── test.html\n├── test.js\n├── test.kt\n├── test.php\n├── test.py\n├── test.rb\n├── test.rs\n├── test.sh\n└── test.ts\n```\n```"; + const expected = + "~~~markdown README.md\n# Project Structure\n\n```\n.\n├── AdvancedPage.tsx\n├── Calculator.java\n├── Dockerfile\n├── calculator_test/\n│ ├── Calculator.java\n│ └── Main.java\n├── data.json\n├── example.ipynb\n├── logs/\n├── nested-folder/\n│ ├── helloNested.py\n│ ├── package.json\n│ └── rules.md\n├── program.cs\n├── query.sql\n├── react-calculator/\n├── readme.md\n├── requirements.txt\n├── test.css\n├── test.html\n├── test.js\n├── test.kt\n├── test.php\n├── test.py\n├── test.rb\n├── test.rs\n├── test.sh\n└── test.ts\n```\n~~~"; + expect(patchNestedMarkdown(source)).toBe(expected); + }); + it("should replace backticks with tildes for markdown codeblocks", () => { const source = "```markdown\n# Header\nSome text\n```"; const expected = "~~~markdown\n# Header\nSome text\n~~~"; diff --git a/gui/src/components/StyledMarkdownPreview/utils/patchNestedMarkdown.ts b/gui/src/components/StyledMarkdownPreview/utils/patchNestedMarkdown.ts index 9be5bfbb2..30bd8fa1f 100644 --- a/gui/src/components/StyledMarkdownPreview/utils/patchNestedMarkdown.ts +++ b/gui/src/components/StyledMarkdownPreview/utils/patchNestedMarkdown.ts @@ -8,6 +8,7 @@ Note, this was benchmarked at sub-millisecond */ import { headerIsMarkdown } from "./headerIsMarkdown"; +import { isOpeningNestedBlock } from "./isOpeningNestedBlock"; export const patchNestedMarkdown = (source: string): string => { // Early return if no markdown codeblock pattern is found (including GitHub variants) @@ -24,10 +25,16 @@ export const patchNestedMarkdown = (source: string): string => { if (nestCount > 0) { // Inside a markdown block if (line.match(/^`+$/)) { - // Ending a block with just backticks (```) - nestCount--; - if (nestCount === 0) { - lines[i] = "~~~"; // End of markdown block + // Found bare backticks - determine if opening or closing + if (isOpeningNestedBlock(trimmedLines, i, nestCount)) { + // Opening a nested codeblock + nestCount++; + } else { + // Closing a block + nestCount--; + if (nestCount === 0) { + lines[i] = "~~~"; // End of markdown block + } } } else if (line.startsWith("```")) { // Going into a nested codeblock