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
feat(markdown-editor): promote a block that starts with # to a heading on any edit
Sergey Kozyrenko9d0e1924a1db
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.tsindex ec222f41..7215c582 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@@ -1,3 +1,5 @@+import type { Transaction } from '@tiptap/pm/state';+import { Editor } from '@tiptap/core';import { beforeAll, describe, expect, it } from 'vitest';@@ -406,6 +408,64 @@ describe('line-leading # / > in a paragraph stay body text on round-trip (ENCODE});});+describe('a block that comes to start with # becomes a heading (content-driven, HeadingAutoformat)', () => {+ const paragraph = (...content: unknown[]) => ({ content: [{ content, type: 'paragraph' }], type: 'doc' });++ const afterEdit = (doc: unknown, edit: (tr: Transaction) => void) => {+ const editor = new Editor({ content: doc as string, extensions: createMarkdownExtensions() });++ editor.commands.command(({ dispatch, tr }) => {+ edit(tr);+ dispatch?.(tr);++ return true;+ });++ const md = editor.getMarkdown();++ editor.destroy();++ return md;+ };++ const text = (value: string) => ({ text: value, type: 'text' });+ const hardBreak = { type: 'hardBreak' };++ it('deleting the text before a mid-line # promotes the paragraph to a heading', () => {+ const md = afterEdit(paragraph(text('x# Big')), (tr) => tr.delete(1, 2));++ // trimEnd: StarterKit's TrailingNode appends an empty paragraph after a doc-final heading (so the caret+ // has somewhere to go), same as typing `# ` on the last line — it collapses back to `# Big` on reload.+ expect(md.trimEnd()).toBe('# Big');+ expect(structuralCounts(md).heading).toBe(1);+ });++ it('the heading level matches the number of hashes', () => {+ const md = afterEdit(paragraph(text('x### Big')), (tr) => tr.delete(1, 2));++ expect(md.trimEnd()).toBe('### Big');+ });++ it('pressing Enter in front of a mid-line # promotes the new block to a heading', () => {+ const md = afterEdit(paragraph(text('123 # Big')), (tr) => tr.split(5));++ expect(structuralCounts(md).heading).toBe(1);+ expect(md).toContain('# Big');+ });++ it('a # after a hardBreak stays body text even after an edit (Shift+Enter is not a block start)', () => {+ const md = afterEdit(paragraph(text('foo'), hardBreak, text('# bar')), (tr) => tr.insertText('!', 1));++ expect(structuralCounts(md).heading ?? 0).toBe(0);+ });++ it('deleting the pre-break text, leaving a hardBreak in front of the #, stays body text', () => {+ const md = afterEdit(paragraph(text('foo'), hardBreak, text('# bar')), (tr) => tr.delete(1, 4));++ expect(structuralCounts(md).heading ?? 0).toBe(0);+ });+});+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.tsindex 50833ad3..6209deb4 100644--- a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts@@ -12,6 +12,7 @@ import { TableCell, TableHeader, TableRow } from '@tiptap/extension-table';import { Placeholder } from '@tiptap/extensions';import StarterKit from '@tiptap/starter-kit';+import { HeadingAutoformat } from './markdown-editor-heading-autoformat';import { createMarkdownLayer, TunedTable } from './markdown-editor-marked';import { MarkdownPaste } from './markdown-editor-paste';import { TagHighlight } from './markdown-editor-tag-highlight';@@ -125,6 +126,7 @@ export const createMarkdownExtensions = (placeholder?: string) => [link: { autolink: true, linkOnPaste: true, openOnClick: false },underline: false,}),+ HeadingAutoformat,TunedTable.configure({ resizable: true }),TableRow,TableHeader,diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-heading-autoformat.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-heading-autoformat.tsnew file mode 100644index 00000000..011fd6d0--- /dev/null+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-heading-autoformat.ts@@ -0,0 +1,71 @@+import type { Node as PMNode } from '@tiptap/pm/model';+import type { EditorState, Transaction } from '@tiptap/pm/state';++import { Extension } from '@tiptap/core';+import { Plugin, PluginKey } from '@tiptap/pm/state';++const HEADING_MARKER = /^(#{1,6}) /;++// The heading input-rule fires only on the keystroke that types `# ` at a block start; a block that comes to+// start with `# ` any other way — deleting the text before an existing `#`, Enter in front of it, paste — stays+// a paragraph, unlike CommonMark (a leading ATX marker IS a heading) and unlike what that same text becomes on+// the next load. This plugin promotes such a paragraph to the matching heading on every doc-changing transaction.+// It keys off the FIRST child, not textContent: a `# ` after a hardBreak (Shift+Enter) must stay body text, and+// there the first child is the pre-break text, so the block correctly does not read as starting with `#`.+export const HeadingAutoformat = Extension.create({+ addProseMirrorPlugins() {+ return [+ new Plugin({+ appendTransaction(transactions: readonly Transaction[], _oldState: EditorState, newState: EditorState) {+ if (!transactions.some((transaction) => transaction.docChanged)) {+ return null;+ }++ const heading = newState.schema.nodes.heading;+ const paragraph = newState.schema.nodes.paragraph;++ if (!heading || !paragraph) {+ return null;+ }++ const targets: { level: number; markerEnd: number; start: number }[] = [];++ newState.doc.descendants((node: PMNode, pos: number) => {+ if (node.type !== paragraph) {+ return true;+ }++ const first = node.firstChild;+ const match = first?.isText && first.text ? HEADING_MARKER.exec(first.text) : null;++ if (match) {+ const start = pos + 1;+ const $start = newState.doc.resolve(start);++ if ($start.node(-1).canReplaceWith($start.index(-1), $start.indexAfter(-1), heading)) {+ targets.push({ level: match[1]!.length, markerEnd: start + match[0].length, start });+ }+ }++ return false;+ });++ if (targets.length === 0) {+ return null;+ }++ const tr = newState.tr;++ // High position → low so each edit leaves the earlier, not-yet-applied positions valid.+ for (const { level, markerEnd, start } of targets.reverse()) {+ tr.delete(start, markerEnd).setBlockType(start, start, heading, { level });+ }++ return tr;+ },+ key: new PluginKey('headingAutoformat'),+ }),+ ];+ },+ name: 'headingAutoformat',+});