feat(launcher): display rendered markdown release notes in a modal

Keep the About page launcher update notice compact by showing only the available version, package size, and action buttons, with a compact button to open a scrollable release notes dialog when notes are present. Render GFM Markdown safely into React elements via marked.lexer without injecting raw HTML, and raise the release notes cap from 1,600 chars to 64 KiB so full bilingual notes are preserved.
This commit is contained in:
N0VAN0M4LY committed 2026-12-03 07:18:31 +08:00
1 parent 214f9d7f4b
commit 10427912e2
10 files changed
+522 -20

No files matched your search

+1
View File
@@ -19,6 +19,7 @@
"@wto/server": "*",
"fflate": "^0.8.2",
"i18next": "^26.3.6",
"marked": "^18.4.0",
"react": "^19.2.8",
"react-dom": "^19.2.8",
"react-i18next": "^17.0.10",
@@ -62,6 +62,7 @@ export class ReleasePageLimitError extends Error {
}
const GITHUB_HEADERS = { Accept: 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28' };
const MAX_RELEASE_NOTES_CHARS = 64 * 1024;
const ReleaseAsset = z.object({ name: z.string(), browser_download_url: z.string().optional() });
const LauncherReleaseEntry = z.object({
draft: z.literal(false),
@@ -265,7 +266,7 @@ function launcherRelease(entry: Record<string, unknown>, version: string, source
return {
source,
version,
notes: body?.trim().slice(0, 1600) ?? '',
notes: body?.trim().slice(0, MAX_RELEASE_NOTES_CHARS) ?? '',
installer,
manifest,
signature,
@@ -0,0 +1,152 @@
import { createElement, Fragment, useMemo, type ReactNode } from 'react';
import { marked, type Token, type Tokens } from 'marked';
const SAFE_EXTERNAL_URL = /^https?:\/\//i;
const HTML_COMMENT = /^<!--[\s\S]*?-->$/;
function isSafeExternalUrl(href: string | undefined): href is string {
return typeof href === 'string' && SAFE_EXTERNAL_URL.test(href.trim());
}
function renderInline(tokens: readonly Token[], keyPrefix: string): ReactNode {
return tokens.map((token, index) => renderInlineToken(token, `${keyPrefix}-${index}`));
}
function renderInlineToken(token: Token, key: string): ReactNode {
switch (token.type) {
case 'text': {
const textToken = token as Tokens.Text;
if (textToken.tokens?.length) return <Fragment key={key}>{renderInline(textToken.tokens, key)}</Fragment>;
return <Fragment key={key}>{textToken.text}</Fragment>;
}
case 'escape':
return <Fragment key={key}>{(token as Tokens.Escape).text}</Fragment>;
case 'strong':
return <strong key={key}>{renderInline((token as Tokens.Strong).tokens, key)}</strong>;
case 'em':
return <em key={key}>{renderInline((token as Tokens.Em).tokens, key)}</em>;
case 'del':
return <del key={key}>{renderInline((token as Tokens.Del).tokens, key)}</del>;
case 'codespan':
return <code key={key}>{(token as Tokens.Codespan).text}</code>;
case 'br':
return <br key={key} />;
case 'link': {
const link = token as Tokens.Link;
const content = link.tokens.length ? renderInline(link.tokens, key) : link.text;
if (!isSafeExternalUrl(link.href)) return <Fragment key={key}>{content}</Fragment>;
return (
<a key={key} href={link.href} title={link.title ?? undefined} target="_blank" rel="noreferrer">
{content}
</a>
);
}
case 'image':
return <Fragment key={key}>{(token as Tokens.Image).text}</Fragment>;
case 'html': {
const raw = (token as Tokens.HTML).text.trim();
if (!raw || HTML_COMMENT.test(raw)) return null;
return <Fragment key={key}>{raw}</Fragment>;
}
default:
return 'raw' in token && typeof token.raw === 'string' ? <Fragment key={key}>{token.raw}</Fragment> : null;
}
}
function renderTable(table: Tokens.Table, key: string): ReactNode {
return (
<div key={key} className="markdown-table-wrap">
<table>
<thead>
<tr>
{table.header.map((cell, column) => (
<th key={`${key}-h-${column}`} style={cell.align ? { textAlign: cell.align } : undefined}>
{renderInline(cell.tokens, `${key}-h-${column}`)}
</th>
))}
</tr>
</thead>
<tbody>
{table.rows.map((row, rowIndex) => (
<tr key={`${key}-r-${rowIndex}`}>
{row.map((cell, column) => (
<td key={`${key}-r-${rowIndex}-${column}`} style={cell.align ? { textAlign: cell.align } : undefined}>
{renderInline(cell.tokens, `${key}-r-${rowIndex}-${column}`)}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
);
}
function renderListItem(item: Tokens.ListItem, key: string): ReactNode {
return (
<li key={key}>
{item.task && <input type="checkbox" checked={Boolean(item.checked)} disabled readOnly className="markdown-task" />}
{renderBlocks(item.tokens, key)}
</li>
);
}
function renderBlock(token: Token, key: string): ReactNode {
switch (token.type) {
case 'space':
case 'def':
case 'checkbox':
return null;
case 'heading': {
const heading = token as Tokens.Heading;
const level = Math.min(6, Math.max(1, heading.depth));
return createElement(`h${level}`, { key }, renderInline(heading.tokens, key));
}
case 'paragraph':
return <p key={key}>{renderInline((token as Tokens.Paragraph).tokens, key)}</p>;
case 'text': {
const textToken = token as Tokens.Text;
if (textToken.tokens?.length) return <Fragment key={key}>{renderInline(textToken.tokens, key)}</Fragment>;
return <Fragment key={key}>{textToken.text}</Fragment>;
}
case 'blockquote':
return <blockquote key={key}>{renderBlocks((token as Tokens.Blockquote).tokens, key)}</blockquote>;
case 'list': {
const list = token as Tokens.List;
const items = list.items.map((item, index) => renderListItem(item, `${key}-i-${index}`));
if (list.ordered) {
return <ol key={key} start={typeof list.start === 'number' && list.start !== 1 ? list.start : undefined}>{items}</ol>;
}
return <ul key={key}>{items}</ul>;
}
case 'code':
return (
<pre key={key}>
<code>{(token as Tokens.Code).text}</code>
</pre>
);
case 'hr':
return <hr key={key} />;
case 'table':
return renderTable(token as Tokens.Table, key);
case 'html': {
const raw = (token as Tokens.HTML).text.trim();
if (!raw || HTML_COMMENT.test(raw)) return null;
return <p key={key}>{raw}</p>;
}
default:
return 'raw' in token && typeof token.raw === 'string' && token.raw.trim()
? <p key={key}>{token.raw}</p>
: null;
}
}
function renderBlocks(tokens: readonly Token[], keyPrefix: string): ReactNode {
return tokens.map((token, index) => renderBlock(token, `${keyPrefix}-${index}`));
}
/** Safe React renderer for GFM Markdown without injecting raw HTML. */
export function MarkdownContent({ markdown }: { readonly markdown: string }) {
const tokens = useMemo(() => marked.lexer(markdown, { gfm: true, breaks: false }), [markdown]);
return <div className="markdown-body">{renderBlocks(tokens, 'md')}</div>;
}
@@ -634,6 +634,9 @@
"download": "Download update",
"install": "Quit and install",
"installing": "Opening the installer…",
"viewNotes": "Release notes",
"notesTitle": "Release notes — v{{version}}",
"closeNotes": "Close",
"errors": {
"network": "The update source could not be reached.",
"page_limit": "Could not confirm whether a newer version is available.",
@@ -634,6 +634,9 @@
"download": "下载更新",
"install": "退出并安装",
"installing": "正在打开安装包…",
"viewNotes": "查看更新说明",
"notesTitle": "版本 {{version}} 更新说明",
"closeNotes": "关闭",
"errors": {
"network": "暂时无法访问更新源。",
"page_limit": "无法确认是否有新版本。",
@@ -1,13 +1,19 @@
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Notice } from '../../components/Notice.tsx';
import { ProgressBar } from '../../components/ProgressBar.tsx';
import { formatBytes } from '../../format.ts';
import type { AppUpdateState } from '../../shell/useAppUpdate.ts';
import { ReleaseNotesModal } from './ReleaseNotesModal.tsx';
export function AppUpdateSection({ state }: { readonly state: AppUpdateState }) {
const { t } = useTranslation();
const [notesOpen, setNotesOpen] = useState(false);
if (!state.supported) return null;
const { check, busy, progress } = state;
const available = check?.status === 'available' && busy !== 'checking' ? check : null;
const notes = available?.release.notes.trim() ?? '';
return (
<section className="about-card">
<div className="about-card-head">
@@ -21,30 +27,46 @@ export function AppUpdateSection({ state }: { readonly state: AppUpdateState })
<p className="about-card-meta">{t('about.update.check')}</p>
{check?.status === 'current' && busy !== 'checking' && <Notice tone="info"><p>{t('about.update.current')}</p></Notice>}
{check?.status === 'failed' && busy !== 'checking' && <Notice tone="error"><p>{t(`about.update.errors.${check.problem}`)}</p></Notice>}
{check?.status === 'available' && busy !== 'checking' && (
{available && (
<Notice tone="info">
<p>{t('about.update.available', { version: check.release.version, size: formatBytes(check.release.size) })}</p>
{check.release.notes && <p className="small update-notes">{check.release.notes}</p>}
{busy === 'downloading' ? (
<p>{t('about.update.available', { version: available.release.version, size: formatBytes(available.release.size) })}</p>
{busy === 'downloading' && (
<>
<p>{t('about.update.downloading')}</p>
<ProgressBar value={progress?.received ?? 0} max={progress?.total ?? null} />
</>
) : (
<button
type="button"
className="button button-primary"
disabled={busy !== null}
onClick={() => {
if (check.ready) state.install(check.release.version);
else state.download(check.release.version);
}}
>
{t(busy === 'installing' ? 'about.update.installing' : check.ready ? 'about.update.install' : 'about.update.download')}
</button>
)}
{(busy !== 'downloading' || notes) && (
<div className="update-actions">
{busy !== 'downloading' && (
<button
type="button"
className="button button-primary"
disabled={busy !== null}
onClick={() => {
if (available.ready) state.install(available.release.version);
else state.download(available.release.version);
}}
>
{t(busy === 'installing' ? 'about.update.installing' : available.ready ? 'about.update.install' : 'about.update.download')}
</button>
)}
{notes && (
<button type="button" className="button button-compact" onClick={() => setNotesOpen(true)}>
{t('about.update.viewNotes')}
</button>
)}
</div>
)}
</Notice>
)}
{notesOpen && available && notes && (
<ReleaseNotesModal
version={available.release.version}
notes={notes}
onClose={() => setNotesOpen(false)}
/>
)}
</section>
);
}
@@ -0,0 +1,57 @@
import { useEffect, useId } from 'react';
import { createPortal } from 'react-dom';
import { useTranslation } from 'react-i18next';
import { MarkdownContent } from '../../components/MarkdownContent.tsx';
interface ReleaseNotesModalProps {
readonly version: string;
readonly notes: string;
readonly onClose: () => void;
}
/** Standalone dialog displaying rendered Markdown release notes for an available launcher update. */
export function ReleaseNotesModal({ version, notes, onClose }: ReleaseNotesModalProps) {
const { t } = useTranslation();
const titleId = useId();
useEffect(() => {
const onKeyDown = (event: KeyboardEvent): void => {
if (event.key === 'Escape') onClose();
};
window.addEventListener('keydown', onKeyDown);
return () => window.removeEventListener('keydown', onKeyDown);
}, [onClose]);
return createPortal(
<div
className="modal-backdrop"
role="presentation"
onClick={(event) => {
if (event.target === event.currentTarget) onClose();
}}
>
<div className="modal-dialog" role="dialog" aria-modal="true" aria-labelledby={titleId}>
<header className="modal-head">
<h3 id={titleId} className="modal-title">{t('about.update.notesTitle', { version })}</h3>
<button
type="button"
className="modal-close"
aria-label={t('about.update.closeNotes')}
onClick={onClose}
>
×
</button>
</header>
<div className="modal-body">
<MarkdownContent markdown={notes} />
</div>
<footer className="modal-foot">
<button type="button" className="button" onClick={onClose}>
{t('about.update.closeNotes')}
</button>
</footer>
</div>
</div>,
document.body,
);
}
@@ -252,6 +252,11 @@
background: var(--red);
}
.button-compact {
font-size: 13px;
padding: 5px 12px;
}
.link {
font: inherit;
color: var(--white);
@@ -481,3 +486,245 @@
left: 100%;
}
}
/* ===== Modal dialog & Markdown content ===== */
.modal-backdrop {
position: fixed;
inset: 0;
z-index: 40;
display: flex;
align-items: center;
justify-content: center;
padding: 28px;
background: rgba(7, 10, 12, 0.72);
}
.modal-dialog {
width: min(760px, 100%);
max-height: min(600px, 100%);
display: flex;
flex-direction: column;
overflow: hidden;
background: var(--sheet);
border: 1px solid var(--line-strong);
border-radius: var(--radius);
box-shadow: 0 18px 48px rgba(0, 0, 0, 0.65);
}
.modal-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 12px 14px 12px 20px;
background: linear-gradient(180deg, #343a40, #2b3136);
border-bottom: 1px solid #000;
}
.modal-title {
margin: 0;
font: 500 18px var(--font-display);
color: var(--white);
letter-spacing: 0.02em;
}
.modal-close {
flex: none;
width: 28px;
height: 28px;
display: grid;
place-items: center;
padding: 0;
border: 0;
border-radius: var(--radius);
background: none;
color: var(--text-dim);
font-size: 20px;
line-height: 1;
cursor: pointer;
}
.modal-close:hover {
color: var(--white);
background: rgba(255, 255, 255, 0.1);
}
.modal-body {
flex: 1;
min-height: 0;
overflow: auto;
padding: 18px 22px 22px;
user-select: text;
}
.modal-foot {
display: flex;
justify-content: flex-end;
gap: 10px;
padding: 10px 20px;
border-top: 1px solid var(--line);
background: rgba(0, 0, 0, 0.17);
}
.markdown-body {
font-size: 13.5px;
line-height: 1.65;
color: var(--text);
overflow-wrap: anywhere;
}
.markdown-body > :first-child {
margin-top: 0;
}
.markdown-body > :last-child {
margin-bottom: 0;
}
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
margin: 18px 0 8px;
font-family: var(--font-display);
font-weight: 500;
color: var(--white);
line-height: 1.3;
}
.markdown-body h1 {
font-size: 20px;
padding-bottom: 6px;
border-bottom: 1px solid var(--line);
}
.markdown-body h2 {
font-size: 17px;
padding-bottom: 5px;
border-bottom: 1px solid var(--line);
}
.markdown-body h3 {
font-size: 15px;
}
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
font-size: 14px;
color: var(--text);
}
.markdown-body p {
margin: 0 0 10px;
}
.markdown-body strong {
color: var(--white);
font-weight: 600;
}
.markdown-body ul,
.markdown-body ol {
margin: 6px 0 10px;
padding-left: 22px;
}
.markdown-body li {
margin: 4px 0;
}
.markdown-body li > ul,
.markdown-body li > ol {
margin: 4px 0;
}
.markdown-body li > p {
margin: 0 0 4px;
}
.markdown-body li > p:last-child {
margin-bottom: 0;
}
.markdown-body code {
padding: 1px 5px;
font-family: var(--font-mono);
font-size: 12.5px;
color: #e6eff2;
background: rgba(0, 0, 0, 0.34);
border: 1px solid var(--line-soft);
border-radius: 2px;
}
.markdown-body pre {
margin: 10px 0;
padding: 10px 12px;
overflow: auto;
background: rgba(0, 0, 0, 0.38);
border: 1px solid var(--line);
border-radius: var(--radius);
}
.markdown-body pre code {
padding: 0;
background: none;
border: 0;
font-size: 12.5px;
line-height: 1.5;
}
.markdown-body blockquote {
margin: 10px 0;
padding: 6px 12px;
color: var(--text-dim);
background: rgba(255, 255, 255, 0.03);
border-left: 3px solid var(--line-strong);
}
.markdown-body hr {
margin: 16px 0;
border: 0;
border-top: 1px solid var(--line);
}
.markdown-body a {
color: #7feaf2;
text-decoration: none;
border-bottom: 1px solid rgba(127, 234, 242, 0.35);
}
.markdown-body a:hover {
color: var(--white);
border-bottom-color: var(--white);
}
.markdown-table-wrap {
margin: 10px 0;
overflow-x: auto;
}
.markdown-body table {
width: 100%;
border-collapse: collapse;
font-size: 12.5px;
}
.markdown-body th,
.markdown-body td {
padding: 6px 10px;
border: 1px solid var(--line);
text-align: left;
}
.markdown-body th {
color: var(--white);
background: rgba(255, 255, 255, 0.05);
}
.markdown-task {
margin-right: 6px;
vertical-align: middle;
}
@@ -344,9 +344,12 @@
}
/* ===== About ===== */
.update-notes {
white-space: pre-line;
overflow-wrap: anywhere;
.update-actions {
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
margin-top: 10px;
}
.about-head {
+13
View File
@@ -29,6 +29,7 @@
"@wto/server": "*",
"fflate": "^0.8.2",
"i18next": "^26.3.6",
"marked": "^18.4.0",
"react": "^19.2.8",
"react-dom": "^19.2.8",
"react-i18next": "^17.0.10",
@@ -4067,6 +4068,18 @@
"@jridgewell/sourcemap-codec": "^1.5.5"
}
},
"node_modules/marked": {
"version": "18.4.0",
"resolved": "https://registry.npmjs.org/marked/-/marked-18.4.0.tgz",
"integrity": "sha512-evbvoaiBPFO2lk2kSkIbrwjPMuX/HjKyefPdZbtwccsJWY91xlLF/SzB5hRsAOwStztmKC09i+IGsUcTYMt//A==",
"license": "MIT",
"bin": {
"marked": "bin/marked.js"
},
"engines": {
"node": ">= 20"
}
},
"node_modules/matcher": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/matcher/-/matcher-3.0.0.tgz",