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(editor): syntax-highlight code blocks and caption their language
Sergey Kozyrenkob260b1d73574
diff --git a/frontend/package.json b/frontend/package.jsonindex 75c8b306..e4c24c81 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -48,6 +48,7 @@"@tanstack/react-table": "^8.21.3","@tanstack/react-virtual": "^3.14.3","@tiptap/core": "^3.27.1",+ "@tiptap/extension-code-block-lowlight": "^3.27.1","@tiptap/extension-image": "^3.27.1","@tiptap/extension-list": "^3.27.1","@tiptap/extension-table": "^3.27.1",diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yamlindex 2072a534..f9291cfe 100644--- a/frontend/pnpm-lock.yaml+++ b/frontend/pnpm-lock.yaml@@ -89,6 +89,9 @@ importers:'@tiptap/core':specifier: ^3.27.1version: 3.27.1(@tiptap/[email protected])+ '@tiptap/extension-code-block-lowlight':+ specifier: ^3.27.1+ version: 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))(@tiptap/[email protected])([email protected])([email protected])'@tiptap/extension-image':specifier: ^3.27.1version: 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))@@ -2219,6 +2222,15 @@ packages:peerDependencies:'@tiptap/extension-list': 3.27.1+ '@tiptap/[email protected]':+ resolution: {integrity: sha512-rDZYDwKpV3Uw8xjI5pzSQTLJ7pEyJkDrVhnzdh9mBY3sHVwP1maYh+0kRUrHi5N3EhiNp/GXG1kPjysPIQ4nqQ==}+ peerDependencies:+ '@tiptap/core': 3.27.1+ '@tiptap/extension-code-block': 3.27.1+ '@tiptap/pm': 3.27.1+ highlight.js: ^11+ lowlight: ^2 || ^3+'@tiptap/[email protected]':resolution: {integrity: sha512-pHlzmZx2OlHfyQ0yRlT5UL4mGokz947DthZuYefN1OleVqOkHpWBG+2JQwqoNq6bmzMne92zbH32rhcJUEYSjA==}peerDependencies:@@ -7599,6 +7611,14 @@ snapshots:dependencies:'@tiptap/extension-list': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])+ '@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))(@tiptap/[email protected])([email protected])([email protected])':+ dependencies:+ '@tiptap/core': 3.27.1(@tiptap/[email protected])+ '@tiptap/extension-code-block': 3.27.1(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])+ '@tiptap/pm': 3.27.1+ highlight.js: 11.11.1+ lowlight: 3.3.0+'@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])':dependencies:'@tiptap/core': 3.27.1(@tiptap/[email protected])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.tsindex d1067e83..01bdaef4 100644--- 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.ts@@ -29,6 +29,29 @@ describe('code-block fence lengthening — a block documenting a ``` fence stays});});+// The fence language must reach node.attrs.language (what CodeBlockLowlight highlights and the+// data-language caption names) and survive save unchanged — highlighting is a view-only decoration.+describe('code-block language attribute', () => {+ const language = (md: string): null | string => {+ const editor = new Editor({ content: md, contentType: 'markdown', extensions: createMarkdownExtensions() });+ const node = (editor.getJSON().content ?? []).find((candidate) => candidate.type === 'codeBlock');+ editor.destroy();++ return (node?.attrs?.language as null | string) ?? null;+ };++ it('captures the fence info string into attrs.language', () => {+ expect(language('```ts\nconst x = 1;\n```')).toBe('ts');+ expect(language('```\nplain\n```')).toBeNull();+ });++ it('round-trips the language on the fence for several languages', () => {+ for (const lang of ['ts', 'python', 'bash', 'json', 'go']) {+ expect(roundTrip(`\`\`\`${lang}\ncode\n\`\`\``)).toContain(`\`\`\`${lang}`);+ }+ });+});+// 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 whosediff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.tsindex 6209deb4..739c1f2d 100644--- a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.ts@@ -6,11 +6,14 @@ import type {MarkdownToken,} from '@tiptap/core';+import { mergeAttributes } from '@tiptap/core';+import { CodeBlockLowlight } from '@tiptap/extension-code-block-lowlight';import { Image } from '@tiptap/extension-image';import { TaskItem, TaskList } from '@tiptap/extension-list';import { TableCell, TableHeader, TableRow } from '@tiptap/extension-table';import { Placeholder } from '@tiptap/extensions';import StarterKit from '@tiptap/starter-kit';+import { common, createLowlight } from 'lowlight';import { HeadingAutoformat } from './markdown-editor-heading-autoformat';import { createMarkdownLayer, TunedTable } from './markdown-editor-marked';@@ -60,6 +63,27 @@ const parseTunedCodeBlock = (token: MarkdownToken, helpers: MarkdownParseHelpers);};+const lowlight = createLowlight(common);++// CodeBlockLowlight extends the default codeBlock, so the same byte-fidelity+// parse/render tuning applies verbatim; the highlighting it adds is a view-only+// ProseMirror decoration and never touches the serialized markdown. renderHTML+// stamps the fence language onto the `<pre>` as data-language so a CSS caption+// (index.css) can name the block — the label lives in the DOM, not the document.+const TunedCodeBlock = CodeBlockLowlight.extend({+ parseMarkdown: parseTunedCodeBlock,+ renderHTML({ HTMLAttributes, node }) {+ const language = (node.attrs.language as null | string) || null;++ return [+ 'pre',+ mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, language ? { 'data-language': language } : {}),+ ['code', { class: language ? `${this.options.languageClassPrefix}${language}` : null }, 0],+ ];+ },+ renderMarkdown: renderTunedCodeBlock,+}).configure({ HTMLAttributes: { class: 'hljs' }, lowlight });+// 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@@ -75,7 +99,7 @@ const escapeLineLeadingBlockMarkers = (markdown: string): string =>// `_`-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. codeBlock gets the indented-fence fix above. (Underline off below.)+// so typing matches load. (codeBlock is replaced by TunedCodeBlock below; underline off below.)const TunedStarterKit = StarterKit.extend({addExtensions() {return (this.parent?.() ?? []).map((extension) => {@@ -90,13 +114,6 @@ const TunedStarterKit = StarterKit.extend({});}- if (extension.name === 'codeBlock') {- return extension.extend({- parseMarkdown: parseTunedCodeBlock,- renderMarkdown: renderTunedCodeBlock,- });- }-if (extension.name === 'paragraph') {return extension.extend({renderMarkdown(node: JSONContent, helpers: MarkdownRendererHelpers) {@@ -122,10 +139,11 @@ const TunedStarterKit = StarterKit.extend({// (markdown-editor-link-handle.tsx) can show the edit popover; opening still works via that popover's button.export const createMarkdownExtensions = (placeholder?: string) => [TunedStarterKit.configure({- codeBlock: { HTMLAttributes: { class: 'hljs' } },+ codeBlock: false,link: { autolink: true, linkOnPaste: true, openOnClick: false },underline: false,}),+ TunedCodeBlock,HeadingAutoformat,TunedTable.configure({ resizable: true }),TableRow,diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor.tsx b/frontend/src/components/shared/markdown-editor/markdown-editor.tsxindex 2ff3aba6..2b407690 100644--- a/frontend/src/components/shared/markdown-editor/markdown-editor.tsx+++ b/frontend/src/components/shared/markdown-editor/markdown-editor.tsx@@ -5,6 +5,8 @@ import { history } from '@tiptap/pm/history';import { EditorState, TextSelection } from '@tiptap/pm/state';import { EditorContent, useEditor } from '@tiptap/react';import { useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';+// Code-block syntax highlighting theme for TunedCodeBlock (lowlight), matching the viewer.+import 'highlight.js/styles/atom-one-dark.css';import { Spinner } from '@/components/ui/spinner';import { cn } from '@/lib/utils';diff --git a/frontend/src/styles/index.css b/frontend/src/styles/index.cssindex a304ab6e..97b13c26 100644--- a/frontend/src/styles/index.css+++ b/frontend/src/styles/index.css@@ -634,14 +634,32 @@font-size: 0.875em;}+/* Background + token colours come from the highlight.js theme on `.hljs`+ (imported by markdown-editor.tsx), matching the read-only viewer. */.tiptap-content .ProseMirror pre {- background-color: var(--editor-code-bg);- color: var(--foreground);+ position: relative;padding: 0.75rem 1rem;border-radius: calc(var(--radius) - 2px);overflow-x: auto;}+/* A caption bar naming the fence language, above the code (data-language is+ stamped by TunedCodeBlock's renderHTML and never enters the document). */+.tiptap-content .ProseMirror pre[data-language]::before {+ content: attr(data-language);+ display: block;+ margin: -0.75rem -1rem 0.75rem;+ padding: 0.25rem 1rem;+ border-bottom: 1px solid oklch(1 0 0 / 0.1);+ border-radius: calc(var(--radius) - 2px) calc(var(--radius) - 2px) 0 0;+ background-color: oklch(1 0 0 / 0.05);+ color: oklch(1 0 0 / 0.55);+ font-family: var(--font-mono);+ font-size: 0.7rem;+ letter-spacing: 0.05em;+ text-transform: uppercase;+}+.tiptap-content .ProseMirror pre code {background: transparent;padding: 0;