AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
fix(editor): exclude adjacent CJK prose from pasted links
tianma-if7493f3cf8f4f
diff --git a/packages/shared/src/editor-link.test.mjs b/packages/shared/src/editor-link.test.mjsindex 10505af3..701bdc72 100644--- a/packages/shared/src/editor-link.test.mjs+++ b/packages/shared/src/editor-link.test.mjs@@ -1,5 +1,5 @@import { describe, expect, test } from "bun:test";-import { getSchema } from "@tiptap/core";+import { getExtensionField, getSchema } from "@tiptap/core";import { EditorState, TextSelection } from "@tiptap/pm/state";import StarterKit from "@tiptap/starter-kit";import { EdgeEverLink } from "./editor-link.ts";@@ -10,6 +10,70 @@ const schema = getSchema([]);describe("EdgeEverLink", () => {+ function pastedLinks(text, extension = EdgeEverLink) {+ const rules = getExtensionField(extension, "addPasteRules", {+ name: "link",+ options: extension.options,+ type: schema.marks.link,+ })();+ return rules.flatMap(rule => rule.find(text)).map(match => ({+ text: match.text,+ index: match.index,+ href: match.data.href,+ }));+ }++ test("pasting the reported sentence links only the domain", () => {+ const text = "我让他先去refero.design挑选参考样子,然后apple-design skill落地,品质线是获奖级——Awwwards / Webby / FWA 那个水准,交付前自我检查,不达标就反复打磨,不许一版交差。";+ expect(pastedLinks(text)).toEqual([+ { text: "refero.design", index: 5, href: "http://refero.design" },+ ]);+ });++ test.each([+ ["先看参考refero.design", "refero.design"],+ ["参考はrefero.design", "refero.design"],+ ["참고refero.design", "refero.design"],+ ["查看www.example.com/path?q=中文", "www.example.com/path?q=中文"],+ ["看看foo中文sub.example.com", "sub.example.com"],+ ])("keeps adjacent prose outside a pasted bare URL: %s", (text, url) => {+ expect(pastedLinks(text)).toEqual([+ { text: url, index: text.indexOf(url), href: `http://${url}` },+ ]);+ });++ test.each([+ "https://example.com/中文",+ "https://中文example.com/path",+ "例子.中国",+ "[email protected]",+ "中文[email protected]",+ "example.com/path?q=中文",+ ])("preserves URLs and email addresses: %s", text => {+ expect(pastedLinks(text)[0]).toMatchObject({ text, index: 0 });+ });++ test("keeps positions correct for multiple links and non-BMP text", () => {+ const text = "😀先看refero.design,再看example.com。";+ expect(pastedLinks(text)).toEqual([+ { text: "refero.design", index: text.indexOf("refero"), href: "http://refero.design" },+ { text: "example.com", index: text.indexOf("example"), href: "http://example.com" },+ ]);+ });++ test("retains configured URL validation after removing prose", () => {+ expect(pastedLinks("先看refero.design", EdgeEverLink.configure({+ shouldAutoLink: url => !url.startsWith("refero"),+ }))).toEqual([]);+ });++ test("preserves explicit Markdown link labels", () => {+ const text = "[我让他先去refero.design](https://example.com)";+ expect(pastedLinks(text, EdgeEverLink.configure({ markdownLinks: true }))).toEqual([+ { text, index: 0, href: "https://example.com" },+ ]);+ });+test("does not extend a terminal link into subsequently entered text", () => {const link = schema.marks.link.create({ href: "https://example.com" });const paragraph = schema.nodes.paragraph.create(null, schema.text("linked", [link]));diff --git a/packages/shared/src/editor-link.ts b/packages/shared/src/editor-link.tsindex 496af51b..4becdb78 100644--- a/packages/shared/src/editor-link.ts+++ b/packages/shared/src/editor-link.ts@@ -1,12 +1,40 @@import Link from "@tiptap/extension-link";import { EMPTY_EXTERNAL_LINK_NODE_TYPE } from "./empty-external-link";+// Linkify accepts Unicode hostnames, so adjacent prose can become part of a+// bare ASCII domain. Keep internationalized URLs available via explicit schemes+// and leave fully non-ASCII domain labels and email addresses alone.+function bareDomainProseLength(text: string): number {+ if (/^[a-z][a-z\d+.-]*:/i.test(text) || text.includes("@")) return 0;+ const label = text.split(".", 1)[0];+ const prefix = label.match(/^.*[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}](?=[a-z\d-]+$)/u);+ return prefix?.[0].length ?? 0;+}+/*** Keep text entered at the end of a link outside that link while preserving* TipTap's automatic URL detection and link-on-paste behavior.*/export const EdgeEverLink = Link.extend({inclusive: false,+ addPasteRules() {+ const rules = this.parent?.() ?? [];+ for (const rule of rules) {+ const find = rule.find;+ if (typeof find !== "function") continue;+ rule.find = (text, event) => (find(text, event) ?? []).flatMap(match => {+ // Explicit Markdown links must retain their authored label and URL.+ if (match.replaceWith !== undefined) return [match];+ const offset = bareDomainProseLength(match.text);+ if (!offset) return [match];+ return (find(match.text.slice(offset), event) ?? []).map(link => ({+ ...link,+ index: match.index + offset + link.index,+ }));+ });+ }+ return rules;+ },parseMarkdown(token, helpers) {if (!token.tokens?.length && token.text === "" && typeof token.href === "string" && token.href) {return {