Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion extension/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
})();
5 changes: 5 additions & 0 deletions extension/common.selftest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
25 changes: 24 additions & 1 deletion extension/content.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions extension/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -1893,7 +1895,7 @@ function renderReadingView() {
<span class="mdg-reading-chapter-time">${escapeHtml(
formatCompactTimestamp(item.from, withHours)
)}</span>
<span class="mdg-reading-chapter-title">${escapeHtml(item.title)}</span>
<span class="mdg-reading-chapter-title"${MdgCommon.textLang(item.title) ? ' lang="en"' : ""}>${escapeHtml(item.title)}</span>
</button>
`
)
Expand All @@ -1917,7 +1919,7 @@ function renderReadingView() {
<span class="mdg-reading-time">${escapeHtml(
formatCompactTimestamp(item.from, withHours)
)}</span>
<span class="mdg-reading-text">${escapeHtml(item.content)}</span>
<span class="mdg-reading-text"${MdgCommon.textLang(item.content) ? ' lang="en"' : ""}>${escapeHtml(item.content)}</span>
</button>
`
)
Expand Down
2 changes: 1 addition & 1 deletion extension/history/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ async function closeNote() {

function renderConversation(conv, index, total) {
const turns = MdgNote.buildConversationTurns(conv.messages, 0)
.map((t) => `<p class="turn-q">问:${esc(t.prompt)}</p><div class="turn-a">${MdgNote.renderMarkdown(t.answer)}</div>`)
.map((t) => `<p class="turn-q">问:<span${MdgCommon.textLang(t.prompt) ? ' lang="en"' : ""}>${esc(t.prompt)}</span></p><div class="turn-a">${MdgNote.renderMarkdown(t.answer)}</div>`)
.join("");
return `${total > 1 ? `<p class="conv-sep">对话 ${index + 1} · ${esc(formatTime(conv.updatedAt))}</p>` : ""}${turns}`;
}
Expand Down
17 changes: 10 additions & 7 deletions extension/note.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`<p>${renderInline(paraBuf.join(" "))}</p>`);
const para = paraBuf.join(" ");
out.push(`<p${lang(para)}>${renderInline(para)}</p>`);
paraBuf = [];
}
};
Expand Down Expand Up @@ -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];
Expand All @@ -962,7 +965,7 @@
flushPara();
closeList();
const level = Math.min(6, heading[1].length + 2);
out.push(`<h${level}>${renderInline(heading[2])}</h${level}>`);
out.push(`<h${level}${lang(heading[2])}>${renderInline(heading[2])}</h${level}>`);
continue;
}

Expand All @@ -986,8 +989,8 @@
index += 1;
}
out.push(
`<table><thead><tr>${headers.map((cell) => `<th>${cell}</th>`).join("")}</tr></thead><tbody>${
bodyRows.map((row) => `<tr>${row.map((cell) => `<td>${cell}</td>`).join("")}</tr>`).join("")
`<table><thead><tr>${headers.map((cell) => `<th${cell.lang}>${cell.html}</th>`).join("")}</tr></thead><tbody>${
bodyRows.map((row) => `<tr>${row.map((cell) => `<td${cell.lang}>${cell.html}</td>`).join("")}</tr>`).join("")
}</tbody></table>`
);
continue;
Expand All @@ -997,7 +1000,7 @@
if (ul) {
flushPara();
openList("ul");
out.push(`<li>${renderInline(ul[1])}</li>`);
out.push(`<li${lang(ul[1])}>${renderInline(ul[1])}</li>`);
continue;
}

Expand All @@ -1006,7 +1009,7 @@
flushPara();
const orderNumber = Number(ol[1]) || 1;
openList("ol", orderNumber);
out.push(`<li>${renderInline(ol[2])}</li>`);
out.push(`<li${lang(ol[2])}>${renderInline(ol[2])}</li>`);
continue;
}

Expand Down
16 changes: 11 additions & 5 deletions extension/note.selftest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("<think>x</think>## 标题\n\n**粗** <b>\n\n- a\n- b"),
"<h4>标题</h4><p><strong>粗</strong> &lt;b&gt;</p><ul><li>a</li><li>b</li></ul>",
"<h4>标题</h4><p><strong>粗</strong> &lt;b&gt;</p><ul><li lang=\"en\">a</li><li lang=\"en\">b</li></ul>",
"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” |"),
'<p lang="en">He said “fine”</p><p>他说“好”,then left</p><h4 lang="en">“Intro”</h4>' +
'<table><thead><tr><th>列</th><th lang="en">col</th></tr></thead><tbody><tr><td>值</td><td lang="en">“v”</td></tr></tbody></table>'
);
// A24, A25: a fence's language line is not code; numbered items are one list.
assert.strictEqual(N.renderMarkdown("```python\nprint(1)\n```"), "<pre><code>print(1)\n</code></pre>");
assert.strictEqual(N.renderMarkdown("1. a\n2. b\n\n3. c"), "<ol><li>a</li><li>b</li><li>c</li></ol>");
assert.strictEqual(N.renderMarkdown("1. a\n1. b"), "<ol><li>a</li><li>b</li></ol>");
assert.strictEqual(N.renderMarkdown("1. a\n2. b\n\n3. c"), '<ol><li lang="en">a</li><li lang="en">b</li><li lang="en">c</li></ol>');
assert.strictEqual(N.renderMarkdown("1. a\n1. b"), '<ol><li lang="en">a</li><li lang="en">b</li></ol>');
{
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");
Expand Down Expand Up @@ -527,7 +533,7 @@ assert.strictEqual(N.renderMarkdown("\\(x\\) 和 $y$"), "<p>\\(x\\) 和 $y$</p>"
assert.ok(!md("```python\nx = '$a$'\ny = '\\(b\\)'\n").includes("<math"), "a fence still streaming keeps its LaTeX");
assert.ok(md("\\[ a\n\n```\ncode \\] here\n```").includes("<pre><code>code \\] here"), "display math stops at a blank line");
assert.ok(md("[x](https://a.com/$b$)").includes('<a href="#"'), "no formula inside an href");
assert.strictEqual(md("\u00030\u0003 plain"), "<p>0 plain</p>", "a placeholder character in the answer is dropped");
assert.strictEqual(md("\u00030\u0003 plain"), '<p lang="en">0 plain</p>', "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: "" })], ["学_习"]);
Expand Down Expand Up @@ -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``` 这里"), "<p>看 <code>x = 1</code> 这里</p>");
assert.strictEqual(N.renderMarkdown("#### 四级\n##### 五级"), "<h6>四级</h6><h6>五级</h6>");
assert.strictEqual(N.renderMarkdown("`**a** [b](https://x)` **c**"), "<p><code>**a** [b](https://x)</code> <strong>c</strong></p>");
assert.strictEqual(N.renderMarkdown("`**a** [b](https://x)` **c**"), '<p lang="en"><code>**a** [b](https://x)</code> <strong>c</strong></p>');
}
// A web page's note: its own frontmatter, the body Defuddle read, the AI 问答 between the markers; under {{site}} = 网页.
{
Expand Down
5 changes: 5 additions & 0 deletions extension/sidepanel.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
25 changes: 19 additions & 6 deletions extension/sidepanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -1444,7 +1456,7 @@ function renderPreviousVideoBar() {
? conversation.messages
.map((message) =>
message.role === "user"
? `<div class="sp-msg sp-msg-user">${escapeHtml(message.content)}</div>`
? `<div class="sp-msg sp-msg-user"${MdgCommon.textLang(message.content) ? ' lang="en"' : ""}>${escapeHtml(message.content)}</div>`
: `<div class="sp-msg sp-msg-assistant"><div class="sp-msg-assistant-body">${renderMarkdown(message.content)}</div></div>`
)
.join("")
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
23 changes: 22 additions & 1 deletion extension/tokens.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Loading