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) => `| ${cell} | `).join("")}
${
- bodyRows.map((row) => `${row.map((cell) => `| ${cell} | `).join("")}
`).join("")
+ `${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>
",
+ "标题
粗 <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”
' +
+ ''
+);
// 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"), "- a
- b
- c
");
-assert.strictEqual(N.renderMarkdown("1. a\n1. b"), "- a
- b
");
+assert.strictEqual(N.renderMarkdown("1. a\n2. b\n\n3. c"), '- a
- b
- c
');
+assert.strictEqual(N.renderMarkdown("1. a\n1. b"), '- a
- 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("