mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 20:11:08 +00:00
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.
This commit is contained in:
1 parent
b36642994c
commit
27b2c1954f
3 files changed
+59
-73
No files matched your search
@@ -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\)/,
|
||||
/<img\b[^>]*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, /<a\s+name=/i);
|
||||
});
|
||||
|
||||
test("modest badge-sized images may keep a small html width", () => {
|
||||
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",
|
||||
}),
|
||||
"",
|
||||
);
|
||||
assert.match(
|
||||
serializeSafeHtmlImage({
|
||||
src: "https://example.com/a.png",
|
||||
alt: "shot",
|
||||
width: 3142,
|
||||
height: 1764,
|
||||
}),
|
||||
/<img\b[^>]*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,
|
||||
}),
|
||||
/<img\b[^>]*width="150"/,
|
||||
);
|
||||
assert.equal(
|
||||
serializeSafeHtmlImage({
|
||||
src: "https://example.com/big.png",
|
||||
alt: "shot",
|
||||
width: 3142,
|
||||
height: 1764,
|
||||
}),
|
||||
"",
|
||||
/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", () => {
|
||||
|
||||
@@ -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 <img>); 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  (CSS scales; no crop)
|
||||
* - Small assets with modest width → optional HTML width for badges/icons
|
||||
* - With width/height → sanitized HTML <img> (MDX keeps dimensions; CSS scales)
|
||||
* - Without → standard 
|
||||
*
|
||||
* 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 = ``;
|
||||
|
||||
// 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 ``;
|
||||
}
|
||||
|
||||
// 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 `<img ${parts.join(" ")} />`;
|
||||
};
|
||||
|
||||
@@ -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(/<img\b[^>]*>/gi, (tag) => {
|
||||
const converted = convertHtmlImgTagToMarkdownOrHtml(tag.trim());
|
||||
return converted || "";
|
||||
|
||||
Reference in new issue
Block a user