开发日志

代码块功能演示:行号、高亮、diff、终端框

· luo980 · devlog · 约 1 分钟阅读

#架构#工具链

主题换成”开发者与研究者”风格后,代码块由 astro-expressive-code 接管,写法都是标准 Markdown 围栏代码块 + meta 字符串,不需要额外组件。

基础:标题 + 行号

行号默认全局开启,标题用 title="..." 标注文件名:

src/lib/content.ts
export function sortEntries<T extends Entry>(entries: T[], key: SortKey = 'date-desc'): T[] {
const arr = [...entries];
switch (key) {
case 'date-asc':
return arr.sort((a, b) => a.data.pubDate.valueOf() - b.data.pubDate.valueOf());
default:
return arr.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf());
}
}

行高亮

大括号里写要高亮的行号,支持区间:

function estimateReadingTime(body: string): number {
const chars = body.replace(/```[\s\S]*?```/g, '').trim().length;
// 中英文混排,按约 350 字/分钟估算
const minutes = chars / 350;
return Math.max(1, Math.round(minutes));
}

增删标记

ins/del 直接把某几行标成绿色新增、红色删除,比整段 diff 更适合小改动:

export const NAV_LINKS = [
{ href: '/', label: '首页' },
{ href: '/archive/', label: '归档' },
{ href: '/devlog/', label: '开发日志' },
] as const;

Diff 代码块

export const CONTENT_DIR = path.join(REPO_ROOT, 'content', 'posts');
export const CONTENT_ROOT = path.join(REPO_ROOT, 'content');
export const COLLECTIONS = {
post: path.join(CONTENT_ROOT, 'posts'),
paper: path.join(CONTENT_ROOT, 'papers'),
};

终端框

frame="terminal" 会渲染成终端窗口样式,适合记录命令行操作:

Terminal window
npm run content -- new "Attention Is All You Need" --kind paper \
--authors "Vaswani;Shazeer" --venue NeurIPS --year 2017 \
--paper-url https://arxiv.org/abs/1706.03762 --status reading

所有代码块都自带复制按钮,且亮/暗色主题会跟随右上角的主题切换按钮联动。