Skip to content

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

fix(markdown-editor): keep line-leading # / > in a paragraph as body text

Sergey Kozyrenkocb9012ce11ba
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 7f504f47..ec222f41 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
@@ -2,7 +2,7 @@ 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';
+import { roundTrip, setupEditorJsdom, structuralCounts } from './markdown-editor-test-setup';
import { findVariableOccurrences } from './markdown-editor-variable-highlight';
beforeAll(setupEditorJsdom);
@@ -359,6 +359,53 @@ describe('typing matches load — underscore emphasis literal, bare URL autolink
});
});
+describe('line-leading # / > in a paragraph stay body text on round-trip (ENCODER-LEADING)', () => {
+ const paraDoc = (...content: unknown[]) => ({ content: [{ content, type: 'paragraph' }], type: 'doc' });
+
+ const save = (doc: unknown) => {
+ const editor = new Editor({ content: doc as string, extensions: createMarkdownExtensions() });
+ const md = editor.getMarkdown();
+
+ editor.destroy();
+
+ return md;
+ };
+
+ it('a Shift+Enter line starting with "# " is escaped and does not become a heading', () => {
+ const md = save(paraDoc({ text: 'foo', type: 'text' }, { type: 'hardBreak' }, { text: '# bar', type: 'text' }));
+
+ expect(md).toContain('\\# bar');
+ expect(structuralCounts(md).heading ?? 0).toBe(0);
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it('a Shift+Enter line starting with "> " does not become a blockquote', () => {
+ const md = save(
+ paraDoc({ text: 'foo', type: 'text' }, { type: 'hardBreak' }, { text: '> quote', type: 'text' }),
+ );
+
+ expect(md).toContain('\\> quote');
+ expect(structuralCounts(md).blockquote ?? 0).toBe(0);
+ expect(roundTrip(md)).toBe(md);
+ });
+
+ it('an escaped \\# at line start loads as clean body text and converges', () => {
+ expect(structuralCounts(roundTrip('\\# not a heading')).heading ?? 0).toBe(0);
+ expect(roundTrip(roundTrip('\\# x'))).toBe(roundTrip('\\# x'));
+ });
+
+ it('a REAL heading / blockquote is untouched', () => {
+ expect(structuralCounts(roundTrip('# foo')).heading).toBe(1);
+ expect(structuralCounts(roundTrip('> foo')).blockquote).toBe(1);
+ });
+
+ it('only # and > are escaped — regex/glob literals and a mid-line # survive byte-identical', () => {
+ for (const s of ['glob \\* and \\? here', 'regex \\d+ and \\.php', 'see #123 issue', 'a # b mid line']) {
+ expect(roundTrip(s)).toBe(s);
+ }
+ });
+});
+
describe('multi-paragraph table cell does not persist a raw control byte on save', () => {
const header = (text: string) => ({
content: [{ content: [{ text, type: 'text' }], type: 'paragraph' }],
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 b0676311..50833ad3 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts
@@ -59,6 +59,17 @@ const parseTunedCodeBlock = (token: MarkdownToken, helpers: MarkdownParseHelpers
);
};
+// A paragraph line that literally starts with `# ` or `> ` re-parses as a heading / blockquote on the next load
+// (an ATX heading interrupts a paragraph; `>` opens a quote), silently changing the block TYPE of body text —
+// reachable by Shift+Enter then a `# ` line. Escape those markers at line start; createTunedMarked's escape
+// tokenizer unescapes `\#`/`\>` on load, so the round-trip stays faithful. Only `#`/`>` are handled: `-`/`*`/`+`/
+// `1.`/fences overlap with literal regex/glob/backref escapes (`\*`, `\1`, `\|`) the editor must preserve.
+const escapeLineLeadingBlockMarkers = (markdown: string): string =>
+ markdown.replace(
+ /(^|\n)( {0,3})(#{1,6} |> )/g,
+ (_match, lineStart: string, indent: string, marker: string) => `${lineStart}${indent}\\${marker}`,
+ );
+
// 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,
@@ -85,6 +96,14 @@ const TunedStarterKit = StarterKit.extend({
});
}
+ if (extension.name === 'paragraph') {
+ return extension.extend({
+ renderMarkdown(node: JSONContent, helpers: MarkdownRendererHelpers) {
+ return escapeLineLeadingBlockMarkers(helpers.renderChildren(node.content ?? []));
+ },
+ });
+ }
+
return extension;
});
},
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-marked.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-marked.ts
index 3d918726..0dc09219 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-marked.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-marked.ts
@@ -23,7 +23,9 @@ const createTunedMarked = () => {
// • del — keep GFM `~~strike~~`, drop a lone `~…~` (else `~5~` / ranges become <del>)
// • emStrong — keep `*`/`**`, drop `_`-delimited emphasis (else `__init__`/`_word_` become em/strong)
// • escape — keep `\`+punct literal (`\.` `\*` `\|` `\\`); marked's default DROPS the backslash
- // (CommonMark unescape), silently corrupting regex/paths on the first load
+ // (CommonMark unescape), silently corrupting regex/paths on the first load. EXCEPT
+ // `\#`/`\>`: unescape those, symmetric with the paragraph serializer escaping a
+ // line-leading `# `/`> ` so body text doesn't re-parse as a heading/quote
// • html/tag — keep `<xml-like>` tags literal (marked swallows real-HTML-element names)
// NB: autolink/url are intentionally NOT neutralised — a bare `https://…`, `<url>` or email is
// meant to become a link (see markdown-editor-extensions.ts link config, kept symmetric with typing).
@@ -34,7 +36,11 @@ const createTunedMarked = () => {
// `&lt;` — that re-freezes the artifacts.
del: (src: string) => (/^~~(?!~)/.test(src) ? false : undefined),
emStrong: (src: string) => (/^_/.test(src) ? undefined : false),
- escape: () => undefined,
+ escape: (src: string) => {
+ const marker = /^\\([#>])/.exec(src);
+
+ return marker ? { raw: marker[0], text: marker[1]!, type: 'escape' as const } : undefined;
+ },
html: () => undefined,
tag: () => undefined,
},