fix: 🐛 Project structure creation with bare codeblocks is breaking markdown display and creation

This commit is contained in:
chezsmithy 2025-06-27 16:00:29 -07:00
parent 9f1448983b
commit 00fc90badc
4 changed files with 242 additions and 4 deletions

View file

@ -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
});

View file

@ -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;
}

View file

@ -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~~~";

View file

@ -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