mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-09-24 15:49:09 +00:00
f9803f3642
* fix(notes): domain paste policy + Codex review follow-ups Move clipboard paste resolution to domain/notes (pure, no React) and address post-#2868 review items: structured text/plain wins over wrapper HTML, caret-safe insert for long pastes (no EOF append), https/protocol-relative image src, entity decode, quote-aware img attrs, code-region protection, nested HTML islands, hard-break preservation, height-only image CSS, and complete small-icon widths. * fix: address Codex review on PR #2869 * perf(notes): cut hover re-renders and heavy image :has() CSS Reduce perceived lag in large notes by (1) coalescing link-hover setState with equality checks and rAF, (2) replacing the combinatorial :has(img[width]) matrix with data-note-img-size markers plus lazy images, and (3) merging DOM decoration into one observer that debounces in edit mode so Lexical keystrokes do not re-walk anchors every frame. * perf(notes): render preview with Streamdown instead of MDXEditor Vault note preview no longer mounts Lexical/MDXEditor/CodeMirror for read-only viewing. Preview uses static Streamdown (same stack as chat) with HTML islands for centered heroes and sized images; edit mode keeps MDXEditor. This cuts large-note scroll and mode-switch cost while preserving host-link click handling and compact badge image layout. * fix(notes): render markdown inside center HTML for Streamdown preview Streamdown/remark does not parse Markdown nested in HTML blocks, so README-style <div align="center"> heroes showed raw # ** and badge syntax. Preprocess preview markdown: expand centered islands via marked GFM to HTML, and rewrite relative images so Streamdown no longer shows "Image blocked" chips under app CSP. * fix(notes): GitHub-style preview with react-markdown Replace Streamdown note preview (chat-oriented Image blocked chrome and poor README layout) with react-markdown + remark-gfm + rehype-raw/sanitize and github-markdown-css. Center HTML islands still expand via marked so pasted heroes render; relative images and empty links are dropped quietly instead of alt-as-title or blocked chips. * fix(notes): keep relative images; map Vite public/ to site root Stop dropping non-https note images. Preserve absolute and relative src as-is, except rewrite repo public/ assets to site-root paths (public/icon.png → /icon.png) so Vite/Electron serve them without /public/* warnings. Unwrap oversized center blocks so Features-style body stays left-aligned in preview like edit mode. * fix(notes): left-align preview body; center only README hero Force markdown-body text-align left and direct block headings/lists left. After the first hero thematic break, strip align=center / text-align:center so Catty/Features sections no longer inherit full-width centering. Tighten oversized-center heuristics (Catty Agent, lists, blockquotes) without treating the Netcatty logo title as a body section. * fix(notes): force preview body left-align (nuclear CSS + unwrap) Catty/Features still rendered centered because small align=center shells and inheritance beat partial CSS. Force every node left via * selector, re-enable center only under [align=center], unwrap all center shells after the hero --- band, and set inline textAlign left on the preview root. * fix(notes): stop /public asset requests; fix preview center inheritance Root cause of body still centering: CSS re-applied text-align:center to all descendants of [align=center], so one large shell centered Catty/lists. Keep * left; only the shell itself may center (inline badges). Root cause of Vite /public/* warnings: stored notes still had /public/... paths and MDX loaded them raw. normalizeNotePublicAssetPaths rewrites /public/ and public/ to site-root paths for both edit and preview display. * revert(notes): restore MDXEditor for preview (drop GitHub markdown path) GitHub-style react-markdown preview caused layout and asset-path issues that diverged from edit mode. Preview again uses MDXEditor with readOnly + mode key remount, matching the pre-Streamdown/GitHub path. Keep display-time public/ → / rewrite so Vite does not request /public/*. * perf(notes): reuse MDX editor when switching notes Remove key={noteId} remount that tore down Lexical/CodeMirror on every note change. Pass noteId and call setMarkdown on switch instead, after the existing draft flush, so selection stays snappy and only document content is replaced. * perf(notes): defer setMarkdown on switch so selection paints first Yield a double rAF before Lexical import on note change, show a blocking overlay for large notes, and ignore external sync while a swap is in flight. Hardens flaky Suspense loading assertions in SSR tests. * fix(notes): align task-list checkboxes and make them clickable in preview Center GFM checkboxes on the first text line, give them a solid hit target, and toggle markdown from preview clicks since Lexical readOnly blocks CheckListPlugin. Pure toggle helper lives in domain/notes/taskList. * fix(notes): close multi-agent review findings on switch/paste/tasks Gate stale onChange during deferred note-switch import, keep draft refs in display-markdown space, baseline paste settle on live getMarkdown to avoid false success after muted setMarkdown, mask code regions in task toggles, and drop abandoned GitHub/Streamdown preview module and deps. * fix(notes): address remaining Codex review findings Clear Lexical undo history on note switch, always inert-block during deferred setMarkdown, re-annotate host links and image sizes on attribute mutations, harden entity/code-region paste paths, and angle-bracket image destinations that contain spaces. * fix(notes): tighten Codex follow-ups on src, code masks, tasks Prefer real img src over data-src, normalize public/ paths only outside code, mask indented/blockquote fences, and accept 1) ordered task markers. * fix(notes): keep swap payloads fresh; keep nested list tasks Refresh deferred setMarkdown when the same note's value changes mid-yield, and stop treating nested list lines as indented code so task toggles stay aligned with the preview DOM. * test(notes): update swap guard source assertions * fix(notes): collision-free code masks, real href, cancel hover rAF Use a document-unique mask sentinel, match anchor href with attribute boundaries (not data-href), and cancel the pending link-hover frame on mouse leave so chips cannot reappear after exit. * fix(notes): accept longer closing fences in code masks CommonMark allows closing fences with more backticks/tildes than the opener; line-scan masking now matches length >= open so public/ paths and task samples inside those blocks stay protected. * fix(notes): harden CommonMark fence, span, and indented-code masks Use homogeneous fence openers, multi-backtick inline spans, and list-context aware indented-code detection so samples stay masked without breaking nested task indexing. * fix(notes): task spacing, angled image dests, deep blockquote fences Require whitespace after task brackets, parse <angled> image destinations with spaces, and mask fenced code at any blockquote depth. * fix(notes): raw-text HTML elements and HTML comment task masks Locate script/style closers without nested-tag walks so raw bodies with '<' are removed, and mask HTML comments before task indexing so preview toggles hit visible checkboxes only. * perf(notes): drop fade animation on note switch and editor load Use an instant solid cover instead of opacity/transform fade-in so Lexical swaps do not pay extra composite cost that can feel more janky than a hard cut. --------- Co-authored-by: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com>
108 lines
3.5 KiB
TypeScript
108 lines
3.5 KiB
TypeScript
/**
|
|
* GFM task-list helpers for note markdown.
|
|
* Lexical CheckListPlugin only toggles when the editor is editable; preview mode
|
|
* reuses these pure transforms so checkboxes stay clickable.
|
|
*/
|
|
|
|
import { maskCodeRegions, unmaskCodeRegions } from "./clipboardPaste";
|
|
|
|
/**
|
|
* Matches "- [ ]", "* [x]", "1. [X]", "1) [ ]", and optional blockquote
|
|
* prefixes (`> - [ ]`) at line start. Code regions and HTML comments are
|
|
* masked before matching so they never steal a DOM checkbox index.
|
|
*/
|
|
// Require whitespace (or EOL) after `]` so `- [ ]foo` is not treated as a task.
|
|
const TASK_LIST_ITEM_PATTERN =
|
|
"^([ \\t]*(?:>[ \\t]*)*(?:[-*+]|\\d+[.)])[ \\t]+)\\[([ xX])\\](?=\\s|$)";
|
|
|
|
const createTaskListItemRe = (): RegExp => new RegExp(TASK_LIST_ITEM_PATTERN, "gm");
|
|
|
|
const escapeRegExp = (value: string): string => (
|
|
value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
|
);
|
|
|
|
/** Mask HTML comments with a unique sentinel so task indices skip them. */
|
|
const maskHtmlComments = (markdown: string): {
|
|
text: string;
|
|
slots: string[];
|
|
sentinel: string;
|
|
} => {
|
|
let n = 0;
|
|
let sentinel = "@@NETCATTY_MD_COMMENT_";
|
|
while (markdown.includes(sentinel)) {
|
|
n += 1;
|
|
sentinel = `@@NETCATTY_MD_COMMENT_S${n}_`;
|
|
}
|
|
const slots: string[] = [];
|
|
const text = markdown.replace(/<!--[\s\S]*?-->/g, (chunk) => {
|
|
const token = `${sentinel}${slots.length}@@`;
|
|
slots.push(chunk);
|
|
return token;
|
|
});
|
|
return { text, slots, sentinel };
|
|
};
|
|
|
|
const unmaskHtmlComments = (
|
|
text: string,
|
|
slots: string[],
|
|
sentinel: string,
|
|
): string => {
|
|
const re = new RegExp(`${escapeRegExp(sentinel)}(\\d+)@@`, "g");
|
|
return text.replace(re, (_, idx: string) => slots[Number(idx)] ?? "");
|
|
};
|
|
|
|
/** Prepare markdown for task scanning: code regions then HTML comments. */
|
|
const prepareTaskScanText = (markdown: string): {
|
|
text: string;
|
|
restore: (body: string) => string;
|
|
} => {
|
|
const codeMask = maskCodeRegions(markdown);
|
|
const commentMask = maskHtmlComments(codeMask.text);
|
|
return {
|
|
text: commentMask.text,
|
|
restore: (body: string) => {
|
|
const withComments = unmaskHtmlComments(
|
|
body,
|
|
commentMask.slots,
|
|
commentMask.sentinel,
|
|
);
|
|
return unmaskCodeRegions(withComments, codeMask.slots, codeMask.sentinel);
|
|
},
|
|
};
|
|
};
|
|
|
|
export const countTaskListItems = (markdown: string): number => {
|
|
const { text } = prepareTaskScanText(markdown);
|
|
return text.match(createTaskListItemRe())?.length ?? 0;
|
|
};
|
|
|
|
/**
|
|
* Toggle the Nth GFM task checkbox (0-based order among rendered tasks:
|
|
* outside fenced/indented/inline code and HTML comments). Returns the original
|
|
* string when the index is out of range.
|
|
*/
|
|
export const toggleTaskListItemAtIndex = (markdown: string, index: number): string => {
|
|
if (index < 0 || !Number.isFinite(index)) return markdown;
|
|
|
|
const { text, restore } = prepareTaskScanText(markdown);
|
|
let seen = 0;
|
|
let changed = false;
|
|
const next = text.replace(createTaskListItemRe(), (full, prefix: string, mark: string) => {
|
|
if (seen++ !== index) return full;
|
|
changed = true;
|
|
const nextMark = mark === " " ? "x" : " ";
|
|
return `${prefix}[${nextMark}]`;
|
|
});
|
|
|
|
return changed ? restore(next) : markdown;
|
|
};
|
|
|
|
/** Left-edge hit box for checklist toggles (checkbox + padding), in CSS px. */
|
|
export const NOTE_TASK_CHECKBOX_HIT_PX = 28;
|
|
|
|
export const isPointerOnTaskCheckbox = (
|
|
listItemRect: Pick<DOMRect, "left" | "right">,
|
|
clientX: number,
|
|
hitPx: number = NOTE_TASK_CHECKBOX_HIT_PX,
|
|
): boolean => clientX >= listItemRect.left && clientX <= listItemRect.left + hitPx;
|