diff --git a/extension/common.js b/extension/common.js index 93c3355d..1bfe41bf 100644 --- a/extension/common.js +++ b/extension/common.js @@ -85,5 +85,11 @@ }; } - globalThis.MdgCommon = Object.freeze({ localDate, fileName, parseTimestamp, parseCount, sleep, latest, isStale, PAGES, SUBTITLE_LANGS, READER_PARAM, CHAT_PORT }); + // The language a block of text is shown in: "en" when it has Latin letters and no Chinese, Japanese or Korean, "" + // otherwise (the page's zh-CN stands). The quote marks and ellipsis are the same characters in Chinese and English but + // take a Chinese font only in Chinese text (tokens.css, #487), so English blocks are marked lang="en". + const textLang = (text) => + /[A-Za-z]/.test(text) && !/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u.test(text) ? "en" : ""; + + globalThis.MdgCommon = Object.freeze({ localDate, textLang, fileName, parseTimestamp, parseCount, sleep, latest, isStale, PAGES, SUBTITLE_LANGS, READER_PARAM, CHAT_PORT }); })(); diff --git a/extension/common.selftest.js b/extension/common.selftest.js index e69cd1b4..11b70cbe 100644 --- a/extension/common.selftest.js +++ b/extension/common.selftest.js @@ -36,6 +36,11 @@ assert.deepStrictEqual(["", "12:3x", "1::2", "-1:00", "1.5:00", null].map(C.pars // C383: one count parser for YouTube's and B站's texts; thousands separators are dropped, not read as the end. assert.deepStrictEqual(["322K", "1.2M", "1,234", "12.5万", "1.2亿", "3萬", 2505, "2505"].map(C.parseCount), [322000, 1200000, 1234, 125000, 120000000, 30000, 2505, 2505]); assert.deepStrictEqual(["-", "", null, undefined].map(C.parseCount), [0, 0, 0, 0]); +assert.deepStrictEqual( + ["He said “fine”", "他说“好”", "他说 fine", "ラーメン “ok”", "한국어 “ok”", "“…”", "", "123"].map(C.textLang), + ["en", "", "", "", "", "", "", ""], + "English only when there are Latin letters and no CJK" +); assert.deepStrictEqual(Object.keys(C.SUBTITLE_LANGS), ["zh-Hans", "zh-Hant", "en", "ja"]); assert.ok(Object.values(C.PAGES).every((p) => fs.existsSync(path.join(__dirname, p))), "every page path exists"); diff --git a/extension/content.css b/extension/content.css index 44b63fbb..33280e30 100644 --- a/extension/content.css +++ b/extension/content.css @@ -5,6 +5,28 @@ * The reading view ignores the host: its readerTheme is the user's explicit choice (light base, dark set, paper override). * Reader mode also sets the tokens on html because the transcript moves into #mdg-reading-inline-host in the host DOM. */ +/* #487: the quote marks and ellipsis are the same characters in Chinese and English. Segoe UI and SF have them, so Chinese + text got narrow Latin ones. This face gives just those characters a Chinese font; it comes first in --mdg-font-sans, and + English blocks (lang="en", MdgCommon.textLang) use --mdg-font-sans-en without it. */ +@font-face { + font-family: "MdgCjkPunct"; + src: local("PingFangSC-Regular"), local("PingFang SC Regular"), local("MicrosoftYaHei"), local("Microsoft YaHei"); + unicode-range: U+2018-2019, U+201C-201D, U+2026; +} + +@font-face { + font-family: "MdgCjkPunct"; + font-weight: 600 900; + src: local("PingFangSC-Semibold"), local("PingFang SC Semibold"), local("MicrosoftYaHei-Bold"), local("Microsoft YaHei Bold"); + unicode-range: U+2018-2019, U+201C-201D, U+2026; +} + +/* Only our own nodes: a global [lang="en"] rule would restyle the host page (YouTube's html is lang="en"). */ +#mdg-reading-view [lang="en"], +#mdg-reading-inline-host [lang="en"] { + font-family: var(--mdg-font-sans-en); +} + :where(#mdg-root, .mdg-confirm-overlay, #mdg-reading-view, html[data-mdg-reader-mode="1"]) { color-scheme: light; --mdg-bg: #f6f7f8; @@ -54,7 +76,8 @@ --mdg-fs-md: 14px; --mdg-fs-lg: 15px; --mdg-fs-xl: 19px; - --mdg-font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Helvetica Neue", Arial, sans-serif; + --mdg-font-sans-en: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Helvetica Neue", Arial, sans-serif; + --mdg-font-sans: "MdgCjkPunct", var(--mdg-font-sans-en); --mdg-font-mono: ui-monospace, SFMono-Regular, Menlo, monospace; --mdg-on-image: #fff; --mdg-cover-chip: rgba(0, 0, 0, 0.55); diff --git a/extension/content.js b/extension/content.js index ca5bcfd3..0e87046c 100644 --- a/extension/content.js +++ b/extension/content.js @@ -1872,6 +1872,8 @@ function renderReadingView() { if (titleNode) { titleNode.textContent = state.title || READER_NAME; + if (MdgCommon.textLang(titleNode.textContent)) titleNode.lang = "en"; + else titleNode.removeAttribute("lang"); } renderReadingPageTitle(); if (metaNode) { @@ -1893,7 +1895,7 @@ function renderReadingView() { ${escapeHtml( formatCompactTimestamp(item.from, withHours) )} - ${escapeHtml(item.title)} + ${escapeHtml(item.title)} ` ) @@ -1917,7 +1919,7 @@ function renderReadingView() { ${escapeHtml( formatCompactTimestamp(item.from, withHours) )} - ${escapeHtml(item.content)} + ${escapeHtml(item.content)} ` ) diff --git a/extension/history/history.js b/extension/history/history.js index dd89e13d..ad8e3117 100644 --- a/extension/history/history.js +++ b/extension/history/history.js @@ -558,7 +558,7 @@ async function closeNote() { function renderConversation(conv, index, total) { const turns = MdgNote.buildConversationTurns(conv.messages, 0) - .map((t) => `

问:${esc(t.prompt)}

${MdgNote.renderMarkdown(t.answer)}
`) + .map((t) => `

问:${esc(t.prompt)}

${MdgNote.renderMarkdown(t.answer)}
`) .join(""); return `${total > 1 ? `

对话 ${index + 1} · ${esc(formatTime(conv.updatedAt))}

` : ""}${turns}`; } diff --git a/extension/note.js b/extension/note.js index fb85ac5c..176c074e 100644 --- a/extension/note.js +++ b/extension/note.js @@ -890,12 +890,15 @@ const lines = escaped.split("\n"); const out = []; + // Each block says its own language, so an English one keeps English quote marks (MdgCommon.textLang). + const lang = (text) => (MdgCommon.textLang(text) ? ' lang="en"' : ""); let listType = ""; let paraBuf = []; const flushPara = () => { if (paraBuf.length) { - out.push(`

${renderInline(paraBuf.join(" "))}

`); + const para = paraBuf.join(" "); + out.push(`${renderInline(para)}

`); paraBuf = []; } }; @@ -943,7 +946,7 @@ .replace(/^\|/, "") .replace(/\|$/, "") .split("|") - .map((cell) => renderInline(cell.trim())); + .map((cell) => ({ lang: lang(cell), html: renderInline(cell.trim()) })); for (let index = 0; index < lines.length; index += 1) { const rawLine = lines[index]; @@ -962,7 +965,7 @@ flushPara(); closeList(); const level = Math.min(6, heading[1].length + 2); - out.push(`${renderInline(heading[2])}`); + out.push(`${renderInline(heading[2])}`); continue; } @@ -986,8 +989,8 @@ index += 1; } out.push( - `${headers.map((cell) => ``).join("")}${ - bodyRows.map((row) => `${row.map((cell) => ``).join("")}`).join("") + `
${cell}
${cell}
${headers.map((cell) => `${cell.html}`).join("")}${ + bodyRows.map((row) => `${row.map((cell) => `${cell.html}`).join("")}`).join("") }
` ); continue; @@ -997,7 +1000,7 @@ if (ul) { flushPara(); openList("ul"); - out.push(`
  • ${renderInline(ul[1])}
  • `); + out.push(`${renderInline(ul[1])}`); continue; } @@ -1006,7 +1009,7 @@ flushPara(); const orderNumber = Number(ol[1]) || 1; openList("ol", orderNumber); - out.push(`
  • ${renderInline(ol[2])}
  • `); + out.push(`${renderInline(ol[2])}`); continue; } diff --git a/extension/note.selftest.js b/extension/note.selftest.js index 7d224168..128fe32a 100644 --- a/extension/note.selftest.js +++ b/extension/note.selftest.js @@ -485,13 +485,19 @@ assert.strictEqual(N.buildTriageSummary({ status: "done", verdict: "must", reaso assert.strictEqual(N.buildTriageSummary({ status: "done", oneLiner: "一句话", points: ["a", "", ""] }), "> 一句话\n\n- a\n\nAI 判断:拿不准", "padded empty points are dropped"); assert.strictEqual( N.renderMarkdown("x## 标题\n\n**粗** \n\n- a\n- b"), - "

    标题

    粗 <b>

    • a
    • b
    ", + "

    标题

    粗 <b>

    • a
    • b
    ", "markdown renders, raw HTML is escaped, think is stripped" ); +// #487: a block with no Chinese is marked English, so its quote marks keep the English font; a Chinese one keeps the page's zh-CN. +assert.strictEqual( + N.renderMarkdown("He said “fine”\n\n他说“好”,then left\n\n## “Intro”\n\n| 列 | col |\n|---|---|\n| 值 | “v” |"), + '

    He said “fine”

    他说“好”,then left

    “Intro”

    ' + + '
    列col
    值“v”
    ' +); // A24, A25: a fence's language line is not code; numbered items are one list. assert.strictEqual(N.renderMarkdown("```python\nprint(1)\n```"), "
    print(1)\n
    "); -assert.strictEqual(N.renderMarkdown("1. a\n2. b\n\n3. c"), "
    1. a
    2. b
    3. c
    "); -assert.strictEqual(N.renderMarkdown("1. a\n1. b"), "
    1. a
    2. b
    "); +assert.strictEqual(N.renderMarkdown("1. a\n2. b\n\n3. c"), '
    1. a
    2. b
    3. c
    '); +assert.strictEqual(N.renderMarkdown("1. a\n1. b"), '
    1. a
    2. b
    '); { const p2 = { title: "合集", site: "bilibili", videoId: "BV1GJ411x7h7", url: "https://www.bilibili.com/video/BV1GJ411x7h7/?p=2&t=5", pageIndex: 2, pageCount: 3 }; assert.strictEqual(N.buildAiConversationFilename(p2), "【AI笔记】合集 P2.md"); @@ -527,7 +533,7 @@ assert.strictEqual(N.renderMarkdown("\\(x\\) 和 $y$"), "

    \\(x\\) 和 $y$

    " assert.ok(!md("```python\nx = '$a$'\ny = '\\(b\\)'\n").includes("code \\] here"), "display math stops at a blank line"); assert.ok(md("[x](https://a.com/$b$)").includes('0 plain

    ", "a placeholder character in the answer is dropped"); + assert.strictEqual(md("\u00030\u0003 plain"), '

    0 plain

    ', "a placeholder character in the answer is dropped"); } // 详情 › 笔记标签 shows the tags the note gets: none when the setting is cleared, plus the 分拣台 tags, from one function. assert.deepStrictEqual([...N.noteTags({ userTags: ["学 习"] }, { tags: "" })], ["学_习"]); @@ -589,7 +595,7 @@ assert.ok(!/payload\.tags \|\| "MoonDigest"/.test(PS.text(PS.own("popup.html"))) // C213-C215: inline code blocks keep their text, #### is a heading, and code spans are left unformatted. assert.strictEqual(N.renderMarkdown("看 ```x = 1``` 这里"), "

    看 x = 1 这里

    "); assert.strictEqual(N.renderMarkdown("#### 四级\n##### 五级"), "
    四级
    五级
    "); - assert.strictEqual(N.renderMarkdown("`**a** [b](https://x)` **c**"), "

    **a** [b](https://x) c

    "); + assert.strictEqual(N.renderMarkdown("`**a** [b](https://x)` **c**"), '

    **a** [b](https://x) c

    '); } // A web page's note: its own frontmatter, the body Defuddle read, the AI 问答 between the markers; under {{site}} = 网页. { diff --git a/extension/sidepanel.css b/extension/sidepanel.css index 64250e7a..9dbefed6 100644 --- a/extension/sidepanel.css +++ b/extension/sidepanel.css @@ -1419,6 +1419,11 @@ body:has(#spInput:not(:placeholder-shown)) .sp-summary-btn { outline: none; } +/* font: inherit above outranks tokens.css's [lang="en"] rule. */ +#spInput[lang="en"] { + font-family: var(--font-sans-en); +} + .sp-non-video-context #spInput { min-height: 72px; } diff --git a/extension/sidepanel.js b/extension/sidepanel.js index a1b57c06..0c35391d 100644 --- a/extension/sidepanel.js +++ b/extension/sidepanel.js @@ -147,7 +147,19 @@ async function init() { await consumePendingPlayerAiQuickAction(); } +// The box says the language of what is in it, as each message does (MdgCommon.textLang): typed or put there by code. +function setLang(node, text) { + if (MdgCommon.textLang(text)) node.lang = "en"; + else node.removeAttribute("lang"); +} + +function setInput(text) { + els.input.value = text; + setLang(els.input, text); +} + function bindEvents() { + els.input.addEventListener("input", () => setLang(els.input, els.input.value)); els.input.addEventListener("keydown", (e) => { if (MdgTyping.composing(e)) return; if (e.key === "Enter" && !e.shiftKey) { @@ -527,7 +539,7 @@ async function refreshProvidersAndPrefsAfterExternalChange() { // AI 总结 filled in and not edited since follows the setting, so the box shows what the button sends. const summary = summaryPrompt(state.view().context); if (previousSummary && summary && els.input.value.trim() === previousSummary.trim()) { - els.input.value = summary; + setInput(summary); } if (state.view().streaming) { return; @@ -610,7 +622,7 @@ async function handlePlayerAiQuickActionRequest(value, { fromStorageChange = tru // user. The panel's own AI 总结 button sends (sendSummary). function fillPrompt(prompt) { if (!els.input.value.trim()) { - els.input.value = prompt; + setInput(prompt); } els.input.focus(); const end = els.input.value.length; @@ -675,7 +687,7 @@ async function runEffect(effect) { const typed = els.input.value.trim() ? els.input.value : ""; drafts.delete(effect.from); if (typed && effect.from !== effect.to) drafts.set(effect.from, typed); - els.input.value = drafts.get(effect.to) || ""; + setInput(drafts.get(effect.to) || ""); drafts.delete(effect.to); return; } @@ -1444,7 +1456,7 @@ function renderPreviousVideoBar() { ? conversation.messages .map((message) => message.role === "user" - ? `
    ${escapeHtml(message.content)}
    ` + ? `
    ${escapeHtml(message.content)}
    ` : `
    ${renderMarkdown(message.content)}
    ` ) .join("") @@ -1863,11 +1875,11 @@ async function sendMessage(prompt, { onStart = null, replaces = [] } = {}) { if (id && !state.view().saved.some((c) => c.id === id)) return; if (!drafts.get(draftKey)) drafts.set(draftKey, text); } else if (typed && !els.input.value.trim()) { - els.input.value = text; + setInput(text); } }; if (typed) { - els.input.value = ""; + setInput(""); } const hidden = [suggestionsNode, ...els.messages.querySelectorAll(".sp-center-error"), ...replaces].filter((node) => node && !node.hidden); for (const node of hidden) node.hidden = true; @@ -1967,6 +1979,7 @@ function appendUserMessage(text) { const node = document.createElement("div"); node.className = "sp-msg sp-msg-user"; node.textContent = text; + setLang(node, text); els.messages.appendChild(node); return node; } diff --git a/extension/tokens.css b/extension/tokens.css index 206bd35a..a133595b 100644 --- a/extension/tokens.css +++ b/extension/tokens.css @@ -3,6 +3,26 @@ box-sizing: border-box; } +/* #487: the quote marks and ellipsis are the same characters in Chinese and English. Segoe UI and SF have them, so Chinese + text got narrow Latin ones. This face gives just those characters a Chinese font; it comes first in --font-sans, and + English blocks (lang="en", MdgCommon.textLang) use --font-sans-en without it. */ +@font-face { + font-family: "MdgCjkPunct"; + src: local("PingFangSC-Regular"), local("PingFang SC Regular"), local("MicrosoftYaHei"), local("Microsoft YaHei"); + unicode-range: U+2018-2019, U+201C-201D, U+2026; +} + +@font-face { + font-family: "MdgCjkPunct"; + font-weight: 600 900; + src: local("PingFangSC-Semibold"), local("PingFang SC Semibold"), local("MicrosoftYaHei-Bold"), local("Microsoft YaHei Bold"); + unicode-range: U+2018-2019, U+201C-201D, U+2026; +} + +[lang="en"] { + font-family: var(--font-sans-en); +} + :root { color-scheme: light dark; @@ -61,8 +81,9 @@ --fs-lg: 15px; --fs-xl: 19px; - --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Helvetica Neue", + --font-sans-en: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Helvetica Neue", Arial, sans-serif; + --font-sans: "MdgCjkPunct", var(--font-sans-en); --font-mono: ui-monospace, SFMono-Regular, Menlo, monospace; /* Over a cover image, the same in both themes: text and chips on the picture, the 看完了 veil and its faint mark, the