Skip to content

Fully autonomous AI Agents system capable of performing complex penetration testing tasks

fix(editor): keep indented fenced code blocks on load

Sergey Kozyrenkode6859d93575
diff --git a/frontend/src/components/shared/markdown-editor/editor-indented-fence.test.ts b/frontend/src/components/shared/markdown-editor/editor-indented-fence.test.ts
new file mode 100644
index 00000000..cdf9b50e
--- /dev/null
+++ b/frontend/src/components/shared/markdown-editor/editor-indented-fence.test.ts
@@ -0,0 +1,49 @@
+import { Editor } from '@tiptap/core';
+import { beforeAll, describe, expect, it } from 'vitest';
+
+import { createMarkdownExtensions } from './markdown-editor-extensions';
+
+beforeAll(() => {
+ document.elementFromPoint = () => null;
+ const r = { bottom: 0, height: 0, left: 0, right: 0, toJSON: () => ({}), top: 0, width: 0, x: 0, y: 0 };
+ Range.prototype.getBoundingClientRect = () => r as DOMRect;
+ Range.prototype.getClientRects = () =>
+ ({ item: () => null, length: 0, [Symbol.iterator]: [][Symbol.iterator] }) as unknown as DOMRectList;
+});
+
+const load = (md: string) => {
+ const editor = new Editor({ content: md, contentType: 'markdown', extensions: createMarkdownExtensions() });
+ const json = JSON.stringify(editor.getJSON());
+ const out = editor.getMarkdown();
+ editor.destroy();
+
+ return { json, out };
+};
+
+// CommonMark allows a fenced code block's opening fence to be indented up to 3 spaces. Upstream
+// @tiptap/extension-code-block gated its parseMarkdown on `token.raw.startsWith('```')`, so an indented
+// fence (whose raw begins with that whitespace) was dropped on load — and when a document mixed fence
+// indents the mis-detection cascaded, silently deleting everything after the first dropped fence.
+describe('indented fenced code blocks survive load', () => {
+ it('keeps a 3-space-indented fence and its content', () => {
+ const { json, out } = load('intro paragraph\n\n ```\ntail content KEEP\n```');
+
+ expect(out).toContain('tail content KEEP');
+ expect(json).toContain('codeBlock');
+ expect(out).toContain('intro paragraph');
+ });
+
+ it('keeps a 1-space-indented fence', () => {
+ const { out } = load(' ```\none space BODY\n```');
+
+ expect(out).toContain('one space BODY');
+ });
+
+ it('does not drop content following an indented fence (cascade guard)', () => {
+ const { out } = load('para one\n\n ```\ncode ALPHA\n```\n\n## Heading BETA\n\ntrailing GAMMA');
+
+ expect(out).toContain('code ALPHA');
+ expect(out).toContain('Heading BETA');
+ expect(out).toContain('trailing GAMMA');
+ });
+});
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.test.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.test.ts
index 6f7f01bf..a143c680 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.test.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.test.ts
@@ -208,17 +208,15 @@ describe('nesting & sequencing — content preserved and converges (≤2 saves)'
});
});
-// KNOWN @tiptap/markdown BUG (not marked, not our customizations). A code block nested in a bullet sublist
-// inside an ordered list is SILENTLY DROPPED. Root cause verified: marked's lexer KEEPS the code token
-// (marked.lexer retains it, marked.parse emits <pre><code>); the loss is downstream in @tiptap/markdown's
-// list-item token→ProseMirror reconstruction, so a marked-side hook can't recover it and there is no clean
-// fix short of patching @tiptap. 0 corpus docs hit it. This test pins the CURRENT (buggy) behavior so we
-// notice if an upstream @tiptap/markdown release fixes it (then flip it to a passing round-trip + drop this).
-describe('KNOWN @tiptap/markdown BUG — ordered > bullet > code drops the code block', () => {
- it('still loses the deeply-nested code (remove this test once @tiptap/markdown fixes it)', () => {
+// A code block nested in a list (ordered > bullet > code) has an indented opening fence, so
+// @tiptap/extension-code-block's `startsWith('```')` gate used to drop it — the same root cause as a
+// top-level indented fence, fixed by parseFaithfulCodeBlock (see markdown-editor-extensions.ts). This was
+// once a pinned KNOWN-BUG test asserting the loss; the fix now keeps the code.
+describe('ordered > bullet > code — indented nested code survives', () => {
+ it('keeps the deeply-nested code block', () => {
const out = roundTrip('1. lvl1\n - lvl2\n\n ```\n deepcode\n ```');
- expect(out).not.toContain('deepcode');
+ expect(out).toContain('deepcode');
});
});
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts
index bcadcbc8..72b4eddc 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts
@@ -9,29 +9,64 @@ import { MarkdownPaste } from './editor-paste';
import { TagHighlight } from './editor-tag-highlight';
import { VariableHighlight } from './editor-variable-highlight';
+const dropUnderscore = (rules: { find: unknown }[]) =>
+ rules.filter((rule) => !(rule.find instanceof RegExp && rule.find.source.includes('_')));
+
+interface MarkdownCodeToken {
+ codeBlockStyle?: string;
+ lang?: string;
+ raw?: string;
+ text?: string;
+}
+
+interface MarkdownParseHelpers {
+ createNode: (type: string, attrs: Record<string, unknown>, content: unknown[]) => unknown;
+ createTextNode: (text: string) => unknown;
+}
+
+// @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
+// a document mixes fences at different indents the mis-detection cascades and everything after the first
+// dropped fence vanishes too. Trim the leading indent before the gate; otherwise identical to upstream.
+const parseFaithfulCodeBlock = (token: MarkdownCodeToken, helpers: MarkdownParseHelpers): unknown => {
+ const fence = token.raw?.trimStart() ?? '';
+
+ if (!fence.startsWith('```') && !fence.startsWith('~~~') && token.codeBlockStyle !== 'indented') {
+ return [];
+ }
+
+ return helpers.createNode(
+ 'codeBlock',
+ { language: token.lang || null },
+ token.text ? [helpers.createTextNode(token.text)] : [],
+ );
+};
+
// StarterKit's Bold/Italic register BOTH `**`/`*` and `__`/`_` input+paste rules. The marked layer keeps
// `_`-emphasis literal on load/paste, so leaving the underscore TYPING rules on would diverge — typed
// `__init__`/`_word_` would emphasize (→ `**init**`/`*word*`) while the same text loaded stays literal,
-// breaking identifiers. Drop only the underscore rules (their `find` regex mentions `_`; the `*` rules
-// stay) so typing matches load. (Link autolink + Underline are turned off in configure() below.)
+// breaking identifiers. Drop only the underscore rules (their `find` regex mentions `_`; the `*` rules stay)
+// so typing matches load. codeBlock gets the indented-fence fix above. (Link autolink + Underline off below.)
const StarterKitFaithful = StarterKit.extend({
addExtensions() {
return (this.parent?.() ?? []).map((extension) => {
- if (extension.name !== 'bold' && extension.name !== 'italic') {
- return extension;
+ if (extension.name === 'bold' || extension.name === 'italic') {
+ return extension.extend({
+ addInputRules() {
+ return dropUnderscore(this.parent?.() ?? []);
+ },
+ addPasteRules() {
+ return dropUnderscore(this.parent?.() ?? []);
+ },
+ });
}
- const dropUnderscore = (rules: { find: unknown }[]) =>
- rules.filter((rule) => !(rule.find instanceof RegExp && rule.find.source.includes('_')));
+ if (extension.name === 'codeBlock') {
+ return extension.extend({ parseMarkdown: parseFaithfulCodeBlock } as Parameters<typeof extension.extend>[0]);
+ }
- return extension.extend({
- addInputRules() {
- return dropUnderscore(this.parent?.() ?? []);
- },
- addPasteRules() {
- return dropUnderscore(this.parent?.() ?? []);
- },
- });
+ return extension;
});
},
});