Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix nested markdown patch for codeblocks with filename #4396

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 12 additions & 55 deletions gui/src/components/markdown/utils/patchNestedMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -28,63 +27,21 @@ 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);
if (ext === "md" || ext === "markdown") {
nestCount = 1;
lines[i] = lines[i].replaceAll("`", "~"); // Replace backticks with tildes
}
}
}
}
}
const out = lines.join("\n");
// 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;
// };
Loading