From 27b2c1954f3afbc1e571a3e61ebdbb9060efbc9b Mon Sep 17 00:00:00 2001 From: bincxz <16399091+binaricat@users.noreply.github.com> Date: Mon, 10 Aug 2026 18:00:47 +0800 Subject: [PATCH] fix(notes): keep image width/height on paste, scale via CSS Preserve HTML img dimensions in note source (including large screenshots). Side-panel display still uses max-width:100% and height:auto so content fits without cropping; small logos can honor their smaller width attrs. --- components/notes/noteClipboardPaste.test.ts | 69 +++++++++++---------- components/notes/noteClipboardPaste.ts | 53 ++++++---------- index.css | 10 ++- 3 files changed, 59 insertions(+), 73 deletions(-) diff --git a/components/notes/noteClipboardPaste.test.ts b/components/notes/noteClipboardPaste.test.ts index 1c6c7588b..4e67858b9 100644 --- a/components/notes/noteClipboardPaste.test.ts +++ b/components/notes/noteClipboardPaste.test.ts @@ -11,7 +11,6 @@ import { serializeSafeHtmlImage, shouldInterceptResolvedNotePaste, shouldInsertClipboardTextAsMarkdown, - NOTE_IMAGE_INTRINSIC_WIDTH_CAP, } from "./noteClipboardPaste.ts"; const CATTY_PASTE = `--- @@ -48,37 +47,44 @@ test("turndown converts pure html clipboard", () => { assert.match(md, /!\[shot\]\(https:\/\/example\.com\/a\.png\)/); }); -test("large screenshot images become markdown without baked pixel width", () => { +test("screenshot images keep width and height attributes", () => { const md = convertHtmlIslandsInMarkdown(CATTY_PASTE); assert.match(md, /^# 🔥 Catty Agent/m); assert.match( md, - /!\[Screenshot 2026-07-02 at 22 51 24\]\(https:\/\/github\.com\/user-attachments\/assets\/3116165d-623a-4d3a-a28a-914befb9b72d\)/, + /]*src="https:\/\/github\.com\/user-attachments\/assets\/3116165d-623a-4d3a-a28a-914befb9b72d"/, ); - assert.doesNotMatch(md, /width="3142"/); - assert.doesNotMatch(md, /height="1764"/); + assert.match(md, /width="3142"/); + assert.match(md, /height="1764"/); + assert.match(md, /alt="Screenshot 2026-07-02 at 22 51 24"/); assert.doesNotMatch(md, /\\#/); assert.doesNotMatch(md, / { - assert.ok(NOTE_IMAGE_INTRINSIC_WIDTH_CAP > 150); +test("serializeSafeHtmlImage preserves dimensions when present", () => { + assert.equal( + serializeSafeHtmlImage({ + src: "https://example.com/a.png", + alt: "shot", + }), + "![shot](https://example.com/a.png)", + ); + assert.match( + serializeSafeHtmlImage({ + src: "https://example.com/a.png", + alt: "shot", + width: 3142, + height: 1764, + }), + /]*width="3142"[^>]*height="1764"[^>]*\/>/, + ); assert.match( serializeSafeHtmlImage({ src: "https://cdn.ko-fi.com/cdn/kofi3.png?v=2", alt: "Support on Ko-fi", width: 150, }), - /]*width="150"/, - ); - assert.equal( - serializeSafeHtmlImage({ - src: "https://example.com/big.png", - alt: "shot", - width: 3142, - height: 1764, - }), - "![shot](https://example.com/big.png)", + /width="150"/, ); }); @@ -103,7 +109,7 @@ test("linked badge images collapse to clean text links", () => { assert.doesNotMatch(md, /^\s*\]\(/m); }); -test("normalize removes orphan link closers and oversized html img attrs", () => { +test("normalize removes orphan link closers but keeps image dimensions", () => { const messy = [ "Intro", "](https://example.com/orphan)", @@ -112,11 +118,12 @@ test("normalize removes orphan link closers and oversized html img attrs", () => ].join("\n"); const md = normalizePastedNoteMarkdown(messy); assert.doesNotMatch(md, /\]\(https:\/\/example\.com\/orphan\)/); - assert.match(md, /!\[wide\]\(https:\/\/example\.com\/w\.png\)/); - assert.doesNotMatch(md, /width="2000"/); + assert.match(md, /src="https:\/\/example\.com\/w\.png"/); + assert.match(md, /width="2000"/); + assert.match(md, /height="1000"/); }); -test("resolve pastes Catty-style mixed markdown+html cleanly", () => { +test("resolve pastes Catty-style mixed markdown+html with image sizes", () => { const payload = resolveNoteClipboardPaste({ plainText: CATTY_PASTE, htmlText: "", @@ -131,23 +138,18 @@ test("resolve pastes Catty-style mixed markdown+html cleanly", () => { true, ); assert.match(payload.text, /^# 🔥 Catty Agent/m); - assert.match(payload.text, /!\[Screenshot[^\]]*\]\(https:\/\/github\.com\/user-attachments/); - assert.doesNotMatch(payload.text, /width="3142"/); + assert.match(payload.text, /width="3142"/); + assert.match(payload.text, /height="1764"/); }); test("repo README paste collapses shields badges without debris", () => { const readmeHead = readFileSync(new URL("../../README.md", import.meta.url), "utf8").slice(0, 2200); const payload = resolveNoteClipboardPaste({ plainText: readmeHead, htmlText: "" }); assert.ok(payload.text.length > 50); - // No dangling markdown link closers from badge conversion. assert.doesNotMatch(payload.text, /^\s*\]\([^)\n]+\)\s*$/m); - // Badges become text links or clean images, not split wrappers. - if (payload.text.includes("shields.io")) { - assert.doesNotMatch( - payload.text, - /\[\s*\n+\s*!\[[^\]]+\]\(https:\/\/img\.shields\.io/, - ); - } + // Large screenshot keeps dimensions in source. + assert.match(payload.text, /width="3142"/); + assert.match(payload.text, /height="1764"/); }); test("resolve uses full turndown for browser StartFragment html", () => { @@ -166,8 +168,9 @@ test("resolve uses full turndown for browser StartFragment html", () => { assert.equal(payload.kind, "html-converted"); assert.match(payload.text, /^# From browser/m); assert.match(payload.text, /\*\*world\*\*/); - assert.match(payload.text, /!\[x\]\(https:\/\/cdn\.example\.com\/x\.png\)/); - assert.doesNotMatch(payload.text, /width="2000"/); + assert.match(payload.text, /src="https:\/\/cdn\.example\.com\/x\.png"/); + assert.match(payload.text, /width="2000"/); + assert.match(payload.text, /height="1000"/); }); test("resolve uses structured plain markdown when html is absent", () => { diff --git a/components/notes/noteClipboardPaste.ts b/components/notes/noteClipboardPaste.ts index 9cb1ae528..1da20db6e 100644 --- a/components/notes/noteClipboardPaste.ts +++ b/components/notes/noteClipboardPaste.ts @@ -4,8 +4,8 @@ * Product policy (lossy but clean — not GitHub README clone): * - HTML → Markdown via Turndown (+ island conversion for mixed sources) * - Linked badge images become plain text links (no broken ](url) debris) - * - Image dimensions are not forced as display pixels (side panel is narrow; - * CSS max-width:100% scales screenshots without cropping) + * - Image width/height attrs are preserved in source; CSS max-width:100% + * scales large screenshots in the side panel without cropping * - Center/align HTML from READMEs is intentionally dropped */ @@ -23,13 +23,6 @@ export type NoteClipboardPastePayload = { kind: NoteClipboardPasteKind; }; -/** - * Pixel widths at or above this are treated as "screenshot intrinsic size" - * (GitHub README often uses 1500–3000). Never bake them into HTML width attrs - * for the note editor — they cause overflow/crop in the side panel. - */ -export const NOTE_IMAGE_INTRINSIC_WIDTH_CAP = 640; - const PASTED_MARKDOWN_PATTERNS = [ /^ {0,3}#{1,6}\s+\S/m, /^ {0,3}(?:[-+*]|\d+[.)])\s+\S/m, @@ -116,7 +109,7 @@ const getTurndown = (): TurndownService => { ), replacement: () => "", }); - // Prefer markdown images; only keep modest HTML dimensions for small assets. + // Keep width/height when present (HTML ); CSS scales them in the panel. service.addRule("imagesForNotes", { filter: "img", replacement: (_content, node) => { @@ -156,16 +149,13 @@ const escapeHtmlAttr = (value: string): string => ( .replace(/>/g, ">") ); -const parsePositivePx = (raw: string): number | null => { - if (!/^\d+(?:\.\d+)?$/.test(raw)) return null; - const n = Number(raw); - return Number.isFinite(n) && n > 0 ? n : null; -}; - /** * Build image markdown for notes. - * - Large / screenshot widths → plain ![alt](src) (CSS scales; no crop) - * - Small assets with modest width → optional HTML width for badges/icons + * - With width/height → sanitized HTML (MDX keeps dimensions; CSS scales) + * - Without → standard ![alt](src) + * + * Display safety: side-panel CSS uses max-width:100% + height:auto so large + * intrinsic sizes (e.g. 3142×1764 screenshots) shrink instead of cropping. */ export const serializeSafeHtmlImage = (input: { src: string; @@ -180,27 +170,22 @@ export const serializeSafeHtmlImage = (input: { const title = input.title?.trim() || ""; const widthRaw = input.width != null ? String(input.width).trim() : ""; const heightRaw = input.height != null ? String(input.height).trim() : ""; - const widthPx = parsePositivePx(widthRaw); - const heightPx = parsePositivePx(heightRaw); + // Accept plain px numbers or percent strings. + const safeWidth = /^(?:\d+(?:\.\d+)?%?)$/.test(widthRaw) ? widthRaw : ""; + const safeHeight = /^(?:\d+(?:\.\d+)?%?)$/.test(heightRaw) ? heightRaw : ""; - const titlePart = title ? ` "${title.replace(/"/g, '\\"')}"` : ""; - const mdImage = `![${alt}](${src}${titlePart})`; - - // Oversized or missing usable width → markdown only (side-panel safe). - if (widthPx == null || widthPx >= NOTE_IMAGE_INTRINSIC_WIDTH_CAP) { - return mdImage; + if (!safeWidth && !safeHeight) { + const titlePart = title ? ` "${title.replace(/"/g, '\\"')}"` : ""; + return `![${alt}](${src}${titlePart})`; } - // Modest width: keep HTML so small badges/icons can stay roughly sized. const parts = [ `src="${escapeHtmlAttr(src)}"`, `alt="${escapeHtmlAttr(alt)}"`, ]; if (title) parts.push(`title="${escapeHtmlAttr(title)}"`); - parts.push(`width="${escapeHtmlAttr(String(Math.round(widthPx)))}"`); - if (heightPx != null && heightPx < NOTE_IMAGE_INTRINSIC_WIDTH_CAP) { - parts.push(`height="${escapeHtmlAttr(String(Math.round(heightPx)))}"`); - } + if (safeWidth) parts.push(`width="${escapeHtmlAttr(safeWidth)}"`); + if (safeHeight) parts.push(`height="${escapeHtmlAttr(safeHeight)}"`); return ``; }; @@ -281,13 +266,13 @@ export const collapseLinkedImagesToTextLinks = (markdown: string): string => { }; /** - * Final cleanup for note paste: badges → text links, drop huge img dimensions, - * tidy blank lines. Called after Turndown / island conversion. + * Final cleanup for note paste: badges → text links, re-sanitize img tags + * (keeps width/height), tidy blank lines. Called after Turndown / islands. */ export const normalizePastedNoteMarkdown = (markdown: string): string => { let body = collapseLinkedImagesToTextLinks(markdown); - // Re-serialize any remaining HTML images through the size policy. + // Re-serialize remaining HTML images (sanitize attrs; keep dimensions). body = body.replace(/]*>/gi, (tag) => { const converted = convertHtmlImgTagToMarkdownOrHtml(tag.trim()); return converted || ""; diff --git a/index.css b/index.css index 5bd03bee1..cd5f90e4c 100644 --- a/index.css +++ b/index.css @@ -1559,16 +1559,14 @@ html[data-theme-transition] *::after { } /* - * Note images (lossy-but-clean paste policy): - * - never overflow the side-panel content width - * - ignore baked-in screenshot pixel widths (object-fit + max-width) - * - no crop: height auto, overflow visible on wrappers + * Note images: source keeps width/height (HTML attrs for MDX / aspect ratio). + * Display: max-width caps screenshots to the pane; height:auto keeps ratio. + * Do NOT force width:auto — that would ignore a 128px logo preference. */ .netcatty-mdx-content img, .netcatty-mdx-editor [class*="_imageWrapper_"] img, .netcatty-mdx-editor img { max-width: 100% !important; - width: auto !important; height: auto !important; object-fit: contain; display: block; @@ -1577,11 +1575,11 @@ html[data-theme-transition] *::after { border-radius: 0.375rem; } +/* Cap MDX image chrome when ImageNode bakes large pixel widths into styles. */ .netcatty-mdx-editor [class*="_imageWrapper_"], .netcatty-mdx-editor [class*="_imageContainer_"], .netcatty-mdx-editor [class*="_imageControlWrapper_"] { max-width: 100% !important; - width: 100% !important; height: auto !important; overflow: visible !important; box-sizing: border-box;