Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Render Markdown Codeblocks that contain code #2880

Merged
merged 9 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"binary/build/**": true,
"binary/out/**": true,
"binary/tmp/**": true,
"core/edit/lazy/test-examples/**": true,
"core/llm/llamaTokenizer.js": true,
"core/llm/llamaTokenizer.mjs": true,
"core/vendor/**": true,
Expand Down
4 changes: 2 additions & 2 deletions gui/src/components/gui/TimelineItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ interface TimelineItemProps {
item: ChatHistoryItem;
open: boolean;
onToggle: () => void;
children: any;
iconElement?: any;
children: JSX.Element;
iconElement?: JSX.Element;
}

function TimelineItem(props: TimelineItemProps) {
Expand Down
3 changes: 2 additions & 1 deletion gui/src/components/markdown/StyledMarkdownPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import FilenameLink from "./FilenameLink";
import StepContainerPreToolbar from "./StepContainerPreToolbar";
import { SyntaxHighlightedPre } from "./SyntaxHighlightedPre";
import StepContainerPreActionButtons from "./StepContainerPreActionButtons";
import { patchNestedMarkdown } from "./utils/patchNestedMarkdown";
import { ContextItemWithId } from "core";

const StyledMarkdown = styled.div<{
Expand Down Expand Up @@ -252,7 +253,7 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
});

useEffect(() => {
setMarkdownSource(props.source || "");
setMarkdownSource(patchNestedMarkdown(props.source ?? ""));
}, [props.source]);

return (
Expand Down
90 changes: 90 additions & 0 deletions gui/src/components/markdown/utils/patchNestedMarkdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
This is a patch for outputing markdown code that contains codeblocks

It notices markdown blocks, keeps track of when that specific block is closed,
and uses ~~~ instead of ``` for that block

Note, this was benchmarked at sub-millisecond

// TODO support github-specific markdown as well, edge case
*/

export const patchNestedMarkdown = (source: string): string => {
if (!source.match(/```(md|markdown)/)) return source; // For performance
// const start = Date.now();
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(/^`+$/)) {
// Ending a block
if (nestCount === 1) lines[i] = "~~~"; // End of markdown block
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
}
}
}
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