From d9716ffd8c74503ce265ed9592c41719203c7585 Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Fri, 6 Nov 2026 07:10:29 +0800 Subject: [PATCH] Update static_notes.cjs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 生成全部笔记的目录页(/notes/),按侧边栏分组列出每篇笔记的静态页面;静态页面顶部加“全部笔记”链接。 --- website/builder/static_notes.cjs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/website/builder/static_notes.cjs b/website/builder/static_notes.cjs index 0aae59a4..fb43b424 100644 --- a/website/builder/static_notes.cjs +++ b/website/builder/static_notes.cjs @@ -85,6 +85,8 @@ body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.95 -ap a { color: var(--theme); text-underline-offset: 3px; } .top { position: sticky; top: 0; z-index: 2; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 20px; border-bottom: 1px solid var(--border); background: var(--bg); } .top .brand { color: var(--heading); font-weight: 700; text-decoration: none; } +.top .all { margin-left: 14px; color: var(--muted); font-size: 14px; text-decoration: none; } +.groups { columns: 2 240px; column-gap: 32px; }.groups section { break-inside: avoid; margin: 0 0 20px; }.groups h2 { margin: 0 0 6px; font-size: 1.05rem; border: 0; }.groups ul { margin: 0; padding-left: 20px; line-height: 1.9; } .open { display: inline-block; padding: 4px 12px; border: 1px solid var(--theme); border-radius: 6px; font-size: 14px; text-decoration: none; } main { max-width: 900px; margin: 0 auto; padding: 28px 24px 64px; } h1, h2, h3, h4, h5, h6 { color: var(--heading); line-height: 1.4; scroll-margin-top: 64px; } @@ -130,7 +132,7 @@ ${stub ? ` CS-Notes在阅读器中打开 +
CS-Notes全部笔记在阅读器中打开
${html} @@ -143,6 +145,25 @@ ${html} `; } +// 全部笔记的目录页(notes/index.html):按侧边栏的分组列出,搜索引擎从首页的 noscript 链接进来后能找到每一篇 +function indexPage(dist, site) { + const sidebar = fs.readFileSync(path.join(dist, '_sidebar.md'), 'utf8'); + const groups = []; + for (const line of sidebar.split('\n')) { + const head = line.match(/^- \*\*(.+)\*\*$/); + if (head) { groups.push({ title: head[1], items: [] }); continue; } + const item = line.match(/^\s+- \[([^\]]+)\]\(\/notes\/([^)]+)\.md\)/); + if (item && groups.length) groups[groups.length - 1].items.push([item[1], item[2]]); + } + const body = '

全部笔记

\n
' + groups.filter(g => g.items.length).map(g => '

' + escapeHtml(g.title) + '

').join('\n') + '
'; + const html = page({ html: body, title: '全部笔记', description: 'CS-Notes 全部笔记:算法、操作系统、计算机网络、数据库、Java、系统设计、大模型应用等。' }, '', site, null) + .replace(//, ``).replace(//, ``) + .replace(/在阅读器中打开<\/a>/, '在阅读器中打开').replace(/

[\s\S]*?<\/p>/, ''); + fs.writeFileSync(path.join(dist, 'notes', 'index.html'), html); + return site + 'notes/'; +} + function build(dist, site, stubs) { const dir = path.join(dist, 'notes'); const names = fs.readdirSync(dir).filter(f => f.endsWith('.md')).map(f => f.slice(0, -3)).sort(); @@ -153,7 +174,7 @@ function build(dist, site, stubs) { fs.writeFileSync(path.join(dir, name + '.html'), page(rendered, name, site, stub)); if (!stub) urls.push(site + 'notes/' + route(name) + '.html'); } - return urls; + return [indexPage(dist, site)].concat(urls); } module.exports = { render, slugger, build };