From 473fa103cf879fd4b86656a2d488b68568098cb5 Mon Sep 17 00:00:00 2001 From: Jayly <65847850+JaylyDev@users.noreply.github.com> Date: Sat, 5 Dec 2026 01:40:46 +0000 Subject: [PATCH] feat(markdown-alert): localize alert titles for Traditional Chinese MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Why Markdown alerts show English titles such as `Note` on Traditional Chinese pages like `/zh/hk-tunnels-tolls/`. The site already sets the page language on `` during static export in `src/pages/_document.tsx` and during client navigation in `src/pages/_app.tsx`. Reading `` in CSS keeps every page in sync without passing a locale argument to `parseMarkdown`. ## What changed The change spans three files. - `src/remark-github-alerts/index.ts` adds `markdown-alert-title-default` to default alert title paragraphs and wraps the English text in ``. Custom inline titles and `options.titles` overrides still render as plain text without those classes. CSS localization then leaves author-written titles untouched. - `src/styles/markdown-alert/github-i18n.css` defines Traditional Chinese titles (`備註`, `提示`, `重要`, `警告`, and `注意`) and sets `--markdown-alert-title-text-display` to `none` on matching `` elements. The selector matches `[lang="zh" i]`, `:lang(zh-Hant)`, `:lang(zh-HK)`, `:lang(zh-TW)`, and `:lang(zh-MO)`. That selector covers every Traditional Chinese page on the site without matching `zh-CN` or `zh-Hans`. Using CSS avoids a flash of English text on static exports and updates immediately when `document.documentElement.lang` changes. Keeping all i18n rules in `src/styles/markdown-alert/github-i18n.css` leaves `src/styles/markdown-alert/github-base.css` untouched. - `src/pages/_app.tsx` imports `src/styles/markdown-alert/github-i18n.css` next to the other `markdown-alert` stylesheets. Every route loads the localized alert rules globally. ## Screenshots | Traditional Chinese (``) | English (``) | | --- | --- | | Traditional Chinese markdown alert showing 備註 on /zh/hk-tunnels-tolls/ | English markdown alert showing Note on /hk-tunnels-tolls/ | --- src/pages/_app.tsx | 1 + src/remark-github-alerts/index.ts | 26 ++++++++++---- src/styles/markdown-alert/github-i18n.css | 44 +++++++++++++++++++++++ 3 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 src/styles/markdown-alert/github-i18n.css diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index 576b144..65f90e1 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -9,6 +9,7 @@ import "@/styles/markdown-alert/github-base.css"; import "@/styles/markdown-alert/github-colors-dark-class.css"; import "@/styles/markdown-alert/github-colors-dark-media.css"; import "@/styles/markdown-alert/github-colors-light.css"; +import "@/styles/markdown-alert/github-i18n.css"; import type { AppProps } from "next/app"; import Head from "next/head"; import { useLanguageSync } from "@/components/LanguageSwitcher"; diff --git a/src/remark-github-alerts/index.ts b/src/remark-github-alerts/index.ts index ddcec27..81e9289 100644 --- a/src/remark-github-alerts/index.ts +++ b/src/remark-github-alerts/index.ts @@ -119,7 +119,8 @@ const remarkGithubAlerts: Plugin = ( if (!match) return; const type = match[1]?.toLowerCase() as keyof typeof icons; - const title = match[2]?.trim() || (titles[type] ?? capitalize(type)); + const customTitle = match[2]?.trim() || titles[type]; + const title = customTitle ?? capitalize(type); const icon = icons[type]; const iconDataUri = `data:image/svg+xml;utf8,${encodeSvg(icon)}`; @@ -141,7 +142,9 @@ const remarkGithubAlerts: Plugin = ( data: { hName: "p", hProperties: { - class: `${classPrefix}-title`, + class: customTitle + ? `${classPrefix}-title` + : `${classPrefix}-title ${classPrefix}-title-default`, }, }, children: [ @@ -156,10 +159,21 @@ const remarkGithubAlerts: Plugin = ( }, }, }, - { - type: "text", - value: title, - }, + customTitle + ? { + type: "text", + value: title, + } + : { + type: "text", + value: title, + data: { + hName: "span", + hProperties: { + class: `${classPrefix}-title-text`, + }, + }, + }, ], }, ...node.children, diff --git a/src/styles/markdown-alert/github-i18n.css b/src/styles/markdown-alert/github-i18n.css new file mode 100644 index 0000000..0edc0c1 --- /dev/null +++ b/src/styles/markdown-alert/github-i18n.css @@ -0,0 +1,44 @@ +html[lang="zh" i], +html:lang(zh-Hant), +html:lang(zh-HK), +html:lang(zh-TW), +html:lang(zh-MO) { + --markdown-alert-title-note: "備註"; + --markdown-alert-title-tip: "提示"; + --markdown-alert-title-important: "重要"; + --markdown-alert-title-warning: "警告"; + --markdown-alert-title-caution: "注意"; + --markdown-alert-title-text-display: none; +} + +.markdown-alert:is( + .markdown-alert-note, + .markdown-alert-tip, + .markdown-alert-important, + .markdown-alert-warning, + .markdown-alert-caution + ) + .markdown-alert-title-default + .markdown-alert-title-text { + display: var(--markdown-alert-title-text-display, inline); +} + +.markdown-alert.markdown-alert-note .markdown-alert-title-default::after { + content: var(--markdown-alert-title-note, none); +} + +.markdown-alert.markdown-alert-tip .markdown-alert-title-default::after { + content: var(--markdown-alert-title-tip, none); +} + +.markdown-alert.markdown-alert-important .markdown-alert-title-default::after { + content: var(--markdown-alert-title-important, none); +} + +.markdown-alert.markdown-alert-warning .markdown-alert-title-default::after { + content: var(--markdown-alert-title-warning, none); +} + +.markdown-alert.markdown-alert-caution .markdown-alert-title-default::after { + content: var(--markdown-alert-title-caution, none); +}