GeezerVison is showing github.com — fix(web): recognize closed chat code fences with CR-only line endings… · Ryoku-dev/hyakki@82a82ee
File tree
Expand file treeCollapse file tree
Open diff view settings
apps/web/src/components/chat
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +import { describe, expect, it } from "vite-plus/test"; |
| 2 | + |
| 3 | +import { markdownTree } from "../../markdown/wordsHast"; |
| 4 | +import { isClosedCodeFence } from "./MarkdownCodeBlock.logic"; |
| 5 | + |
| 6 | +describe("isClosedCodeFence", () => { |
| 7 | + it.each(["\n", "\r\n", "\r"])("tells closed from unclosed fences with %j line endings", (eol) => { |
| 8 | + for (const closed of [false, true]) { |
| 9 | + const source = ["```sh", "echo hi", ...(closed ? ["```"] : [])].join(eol); |
| 10 | + const [pre] = markdownTree(source).children; |
| 11 | + const code = pre?.type === "element" ? pre.children[0] : undefined; |
| 12 | + const content = code?.type === "element" ? code.children[0] : undefined; |
| 13 | + if (pre?.type !== "element" || content?.type !== "text") { |
| 14 | + throw new Error("Expected a parsed code block"); |
| 15 | + } |
| 16 | + expect(isClosedCodeFence(pre, source, content.value)).toBe(closed); |
| 17 | + } |
| 18 | + }); |
| 19 | +}); |
Collapse file
Copy file name to clipboardExpand all lines: apps/web/src/components/chat/MarkdownCodeBlock.logic.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -52,7 +52,7 @@ export function isClosedCodeFence(node: Element | undefined, text: string, code: |
52 | 52 | const opening = /^(?:`{3,}|~{3,})/.exec(source)?.[0]; |
53 | 53 | // One class for the blockquote prefix: nested quantifiers here backtrack |
54 | 54 | // exponentially on code lines that start with many `> ` markers. |
55 | | - const closing = /(?:^|\n)[ \t>]*(`{3,}|~{3,})[ \t\r]*$/.exec(source)?.[1]; |
| 55 | + const closing = /(?:^|[\r\n])[ \t>]*(`{3,}|~{3,})[ \t\r]*$/.exec(source)?.[1]; |
56 | 56 | return ( |
57 | 57 | opening !== undefined && |
58 | 58 | closing !== undefined && |
|