Fully autonomous AI Agents system capable of performing complex penetration testing tasks
Go ai-agentsai-security-toolanthropicautonomous-agentsgolanggptgraphqlmulti-agent-systemoffensive-securityopen-sourceopenaipenetration-testingpenetration-testing-toolsreactsecurity-automationsecurity-testingsecurity-toolsself-hosted
fix(markdown-editor): widen code-block fence past inner backtick runs (M1)
Sergey Kozyrenko15fab1b40523
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-code-fence.test.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-code-fence.test.tsnew file mode 100644index 00000000..90cb4dd6--- /dev/null+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-code-fence.test.ts@@ -0,0 +1,45 @@+import { Editor } from '@tiptap/core';+import { beforeAll, describe, expect, it } from 'vitest';++import { createMarkdownExtensions } from './markdown-editor-extensions';+import { roundTrip, setupEditorJsdom } from './markdown-editor-test-setup';++beforeAll(setupEditorJsdom);++const nodeCount = (md: string, type: string): number => {+ const editor = new Editor({ content: md, contentType: 'markdown', extensions: createMarkdownExtensions() });+ const count = (editor.getJSON().content ?? []).filter((node) => node.type === type).length;+ editor.destroy();++ return count;+};++describe('code-block fence lengthening — a block documenting a ``` fence stays one block (M1)', () => {+ it('widens the outer fence past an inner ``` run so the block does not split on reload', () => {+ const src = '````\ninner\n```\nnested fence\n```\nafter\n````';+ const out = roundTrip(src);++ expect(nodeCount(out, 'codeBlock')).toBe(1);+ expect(roundTrip(out)).toBe(out);+ expect(out).toContain('nested fence');+ });++ it('leaves an ordinary code block on a 3-backtick fence', () => {+ expect(roundTrip('```js\nconst x = 1;\n```')).toContain('```js');+ });+});++// KNOWN UPSTREAM LIMITATION — not fixable in this layer. @tiptap/markdown derives a MARK's opening/closing+// delimiter by rendering it around a fixed placeholder (getMarkOpening/getMarkClosing), so the `code` mark's+// real content is never seen at delimiter time and it always emits a SINGLE backtick. A code span whose+// content contains a backtick therefore collapses on save and never converges. A NODE (codeBlock, above) DOES+// receive its real content and is content-aware — hence M1 is fixable but this is not. Pinned so the gap stays+// visible and this test flips green if a future @tiptap/markdown makes mark delimiters content-aware.+describe('inline code containing a backtick — known lossy on save (upstream mark-delimiter limitation)', () => {+ it('collapses a backtick-containing code span (does not round-trip)', () => {+ const out = roundTrip('x ``a `b` c`` y');++ expect(out).not.toContain('``a `b` c``');+ expect(roundTrip(out)).not.toBe(out);+ });+});diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.tsindex d40bf388..a51318bd 100644--- a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts@@ -24,6 +24,32 @@ interface MarkdownParseHelpers {createTextNode: (text: string) => unknown;}+interface MarkdownRenderHelpers {+ renderChildren: (content: unknown) => string;+}++type MarkdownRenderNode = { attrs?: { language?: null | string }; content?: unknown };++const longestBacktickRun = (text: string): number =>+ (text.match(/`+/g) ?? []).reduce((max, run) => Math.max(max, run.length), 0);++// @tiptap/extension-code-block's renderMarkdown always emits a 3-backtick fence, so a code block whose content+// contains a ``` line (a doc demonstrating fenced markdown — common in knowledge/prompt examples) re-parses as+// TWO blocks on the next load: the inner fence closes the outer one. CommonMark requires the fence to be longer+// than any backtick run inside — widen it. Otherwise identical to upstream.+const renderFaithfulCodeBlock = (node: MarkdownRenderNode, h: MarkdownRenderHelpers): string => {+ const language = node.attrs?.language || '';++ if (!node.content) {+ return `\`\`\`${language}\n\n\`\`\``;+ }++ const content = h.renderChildren(node.content);+ const fence = '`'.repeat(Math.max(3, longestBacktickRun(content) + 1));++ return [`${fence}${language}`, content, fence].join('\n');+};+// @tiptap/extension-code-block's own parseMarkdown gates on `token.raw.startsWith('```')`, but CommonMark// lets a fenced code block's opening fence be indented up to 3 spaces — marked then emits a valid `code`// token whose `raw` starts with that whitespace, the gate rejects it, and the block is dropped on load. When@@ -63,9 +89,10 @@ const StarterKitFaithful = StarterKit.extend({}if (extension.name === 'codeBlock') {- return extension.extend({ parseMarkdown: parseFaithfulCodeBlock } as Parameters<- typeof extension.extend- >[0]);+ return extension.extend({+ parseMarkdown: parseFaithfulCodeBlock,+ renderMarkdown: renderFaithfulCodeBlock,+ } as Parameters<typeof extension.extend>[0]);}return extension;@@ -75,7 +102,7 @@ const StarterKitFaithful = StarterKit.extend({// Single source of truth for the editor's extension stack — shared by markdown-editor.tsx AND the// round-trip tests so they can never drift. createMarkdownLayer is the official @tiptap/markdown layer-// tuned for our content (see editor-markdown.ts); VariableHighlight/TagHighlight are view-only decorations+// tuned for our content (see markdown-editor-marked.ts); VariableHighlight/TagHighlight are view-only decorations// ({{vars}} / <tags>) that don't affect serialization.// • underline: false — its `++text++` markdown corrupts `C++ … C++` prose on load and Ctrl+U emits `++`.// • link autolink/linkOnPaste: false — a typed/pasted bare URL stays literal (matches load); explicit