Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
fix(open-knowledge): lift 256-char cap on self-closing media tags (#1598)
Andrew Mikofalvy7851f757b120
diff --git a/.changeset/img-tag-length-cap.md b/.changeset/img-tag-length-cap.mdnew file mode 100644index 000000000..ccd95ac27--- /dev/null+++ b/.changeset/img-tag-length-cap.md@@ -0,0 +1,18 @@+---+"@inkeep/open-knowledge-core": patch+---++fix(open-knowledge): render self-closing `<img>` / `<video>` / `<audio>` tags of any length (PRD-6910)++The markdown autolink/HTML guard recognized self-closing canonical media tags+(`<img />`, `<video />`, `<audio />`) only when the entire tag fit inside a fixed+256-character lookahead window. A valid tag with a data-URI `src` or a long+descriptive `alt` exceeded that window and silently fell through to literal text+instead of rendering as a media node — the symptom reported in PRD-6910.++The guard now recognizes the self-closing `/>` form via a precomputed `>`-offset+index (bounded by the tag's own first `>` and the next blank line) rather than a+fixed window, so canonical media tags render regardless of total length. Bare+HTML-void forms (`<img src="x">`, no slash) still parse as text, unchanged. The+check is O(log n) per tag, reintroducing none of the O(n²)/ReDoS risk the+original window guarded against.diff --git a/packages/core/src/markdown/autolink-void-html-guard.precision.test.ts b/packages/core/src/markdown/autolink-void-html-guard.precision.test.tsindex d2b619710..6431b3ba8 100644--- a/packages/core/src/markdown/autolink-void-html-guard.precision.test.ts+++ b/packages/core/src/markdown/autolink-void-html-guard.precision.test.ts@@ -98,6 +98,52 @@ describe('Guard precision: valid MDX survives protectFromMdx() unchanged', () =>});});+describe('Guard precision: self-closing canonical media tags are length-independent', () => {+ const GUARD_OPEN = '';+ const CANONICAL = ['img', 'video', 'audio'] as const;++ test('long alt does not guard self-closing canonical tags', () => {+ for (const tag of CANONICAL) {+ const desc = 'a lone sailboat on calm rippled water under high cirrus '.repeat(16);+ const mdx = `<${tag} src="m.ext" alt="${desc}" />`;+ expect(mdx.length).toBeGreaterThan(256);+ expect(protectFromMdx(mdx)[0]).toBe('<');+ }+ });++ test('data-URI src does not guard self-closing <img/>', () => {+ const mdx = `<img src="data:image/png;base64,${'A'.repeat(2000)}" alt="x" />`;+ expect(protectFromMdx(mdx)[0]).toBe('<');+ });++ test('bare void form (no slash) stays guarded regardless of length', () => {+ const mdx = `<img src="x.png" alt="${'a'.repeat(900)}">`;+ expect(protectFromMdx(mdx)[0]).toBe(GUARD_OPEN);+ });++ test('adjacent long self-closing tags each pass through', () => {+ const alt = 'descriptive caption text '.repeat(20); // ~500 chars each+ const mdx = `<img src="a.png" alt="${alt}" />\n\n<img src="b.png" alt="${alt}" />`;+ const out = protectFromMdx(mdx);+ expect(out[0]).toBe('<'); // first opener not guarded+ expect(out).not.toContain(GUARD_OPEN); // neither opener guarded+ });++ test(+ 'PBT: self-closing canonical media tags are never guarded at arbitrary attribute length',+ () => {+ fc.assert(+ fc.property(fc.constantFrom(...CANONICAL), fc.integer({ min: 0, max: 4000 }), (tag, n) => {+ const mdx = `<${tag} src="x.ext" alt="${'a'.repeat(n)}" />`;+ expect(protectFromMdx(mdx)[0]).toBe('<');+ }),+ { numRuns: NUM_RUNS, seed: 42 },+ );+ },+ TIMEOUT,+ );+});+describe('Guard: `<mark>` paired-JSX exemption paths', () => {test('paired `<mark>text</mark>` opener is not PUA-protected', () => {const protected_ = protectFromMdx('<mark>text</mark>');diff --git a/packages/core/src/markdown/autolink-void-html-guard.test.ts b/packages/core/src/markdown/autolink-void-html-guard.test.tsindex b62fec49c..08848b87e 100644--- a/packages/core/src/markdown/autolink-void-html-guard.test.ts+++ b/packages/core/src/markdown/autolink-void-html-guard.test.ts@@ -1,4 +1,5 @@import { describe, expect, test } from 'bun:test';+import type { JSONContent } from '@tiptap/core';import { sharedExtensions } from '../extensions/shared.ts';import { MarkdownManager } from './index.ts';@@ -8,6 +9,17 @@ function roundTrip(md: string): string {return mdManager.serialize(mdManager.parse(md));}+function collectText(node: JSONContent): string {+ let out = node.type === 'text' && typeof node.text === 'string' ? node.text : '';+ for (const child of node.content ?? []) out += collectText(child);+ return out;+}++function renderedAsLiteralText(md: string): boolean {+ const text = collectText(mdManager.parse(md));+ return text.includes('<img') || text.includes('<video') || text.includes('<audio');+}+describe('R23: autolink regression fix', () => {test('autolink <https://example.com> parses without error', () => {expect(() => mdManager.parse('Visit <https://example.com>.\n')).not.toThrow();@@ -44,6 +56,47 @@ describe('R23: void HTML regression fix', () => {});});+describe('R23: self-closing media tags render at any length', () => {+ const longAlt = 'a lone sailboat on calm rippled water under high cirrus clouds '.repeat(6);++ test('long-alt <img/> is not literal text', () => {+ const md = `<img src="./photo.jpg" alt="${longAlt}" width="320" />\n`;+ expect(md.length).toBeGreaterThan(256);+ expect(renderedAsLiteralText(md)).toBe(false);+ });++ test('data-URI <img/> is not literal text', () => {+ expect(+ renderedAsLiteralText(`<img src="data:image/png;base64,${'A'.repeat(800)}" alt="x" />\n`),+ ).toBe(false);+ });++ test('long <video/> and <audio/> are not literal text', () => {+ expect(renderedAsLiteralText(`<video src="v.mp4" controls aria-label="${longAlt}" />\n`)).toBe(+ false,+ );+ expect(renderedAsLiteralText(`<audio src="a.mp3" controls aria-label="${longAlt}" />\n`)).toBe(+ false,+ );+ });++ test('bare void <img> (no slash) stays literal text regardless of length', () => {+ expect(renderedAsLiteralText(`<img src="x.png" alt="${longAlt}">\n`)).toBe(true);+ });++ test('two long self-closing <img/> separated by a caption both render', () => {+ const md = `## Photos\n\n<img src="./a.jpg" alt="${longAlt}" width="320" />\n\n*caption*\n\n<img src="./b.jpg" alt="${longAlt}" width="480" />\n`;+ expect(renderedAsLiteralText(md)).toBe(false);+ });++ test('long self-closing <img/> round-trips with src and alt intact', () => {+ const md = `<img src="./photo.jpg" alt="${longAlt}" width="320" />\n`;+ const out = roundTrip(md);+ expect(out).toContain('./photo.jpg');+ expect(out).toContain('high cirrus');+ });+});+describe('R23: invalid JSX opener recovery', () => {test('literal <50ms in prose parses without error', () => {expect(() => mdManager.parse('Warm replay when nothing changed is <50ms.\n')).not.toThrow();diff --git a/packages/core/src/markdown/autolink-void-html-guard.ts b/packages/core/src/markdown/autolink-void-html-guard.tsindex 057796617..4f863ab10 100644--- a/packages/core/src/markdown/autolink-void-html-guard.ts+++ b/packages/core/src/markdown/autolink-void-html-guard.ts@@ -66,6 +66,31 @@ function indexSelfClose(source: string): number[] {return positions;}+function indexGreaterThan(source: string): number[] {+ const positions: number[] = [];+ let i = source.indexOf('>');+ while (i !== -1) {+ positions.push(i);+ i = source.indexOf('>', i + 1);+ }+ return positions;+}++function isSelfClosingTagAt(+ offset: number,+ result: string,+ greaterThanOffsets: number[],+ paragraphBreaks: number[],+): boolean {+ const gtIdx = lowerBound(greaterThanOffsets, offset);+ if (gtIdx >= greaterThanOffsets.length) return false;+ const tagClose = greaterThanOffsets[gtIdx];+ const pbIdx = lowerBound(paragraphBreaks, offset);+ const nextBlankLine = pbIdx < paragraphBreaks.length ? paragraphBreaks[pbIdx] : result.length;+ if (tagClose >= nextBlankLine) return false; // tag never closes before a blank line+ return result[tagClose - 1] === '/';+}+export function protectFromMdx(source: string): string {let result = source;@@ -101,6 +126,7 @@ export function protectFromMdx(source: string): string {const closeTagOffsets = indexUppercaseCloseTagsByName(result);const paragraphBreaks = indexParagraphBreaks(result);const selfCloseOffsets = indexSelfClose(result);+ const greaterThanOffsets = indexGreaterThan(result);result = result.replace(/</g, (match, offset) => {const lookahead = result.slice(offset, offset + 256);@@ -110,8 +136,12 @@ export function protectFromMdx(source: string): string {return GUARD_OPEN; // Incomplete close tag — protect}- const lowercaseCanonicalMatch = /^<([a-z][a-z0-9]*)([^>]*)\/>/.exec(lookahead);- if (lowercaseCanonicalMatch && LOWERCASE_JSX_CANONICAL_TAGS.has(lowercaseCanonicalMatch[1])) {+ const lowercaseNameMatch = /^<([a-z][a-z0-9]*)/.exec(lookahead);+ if (+ lowercaseNameMatch &&+ LOWERCASE_JSX_CANONICAL_TAGS.has(lowercaseNameMatch[1]) &&+ isSelfClosingTagAt(offset, result, greaterThanOffsets, paragraphBreaks)+ ) {return match;}