diff --git a/website/builder/check.py b/website/builder/check.py index a272a679..0e858f3a 100644 --- a/website/builder/check.py +++ b/website/builder/check.py @@ -37,7 +37,9 @@ def check(source, output): if name not in files and name + '.md' not in files and name.rstrip('/') + '/index.html' not in files: errors.append('缺失引用:' + name) for name in re.findall(r'(?:src|href)="(/[^"#]+)"', (output / 'index.html').read_text()): - if name.split('?')[0].lstrip('/') not in files: + path = name.split('?')[0].lstrip('/') + # 目录地址(/notes/)由其中的 index.html 提供 + if path not in files and not (path.endswith('/') and path + 'index.html' in files): errors.append('缺失页面资源:' + name) forbidden = [name for name in files if set(Path(name).parts) & { '.git', '.cache', 'node_modules', 'builder', 'templates', 'upstream', 'remote', 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 }; diff --git a/website/builder/test_site_assets.py b/website/builder/test_site_assets.py index 33fef84e..17a1e822 100644 --- a/website/builder/test_site_assets.py +++ b/website/builder/test_site_assets.py @@ -98,10 +98,12 @@ class SearchIndexTests(unittest.TestCase): ''.join(f'## {t}\n\n内容\n\n' for t in titles[1:]) + 'HTML 链接\n\n```java\n# 不是标题\n```\n' (dist / 'notes' / '示例.md').write_text(body) (dist / 'notes' / '算法.md').write_text('见 [算法 - 目录](/notes/算法 - 目录.md)\n') + (dist / '_sidebar.md').write_text('- [首页](/)\n\n- **示例分组**\n - [示例](/notes/%E7%A4%BA%E4%BE%8B.md)\n') (dist / 'stubs.json').write_text(json.dumps({'/notes/算法': '/notes/算法 - 目录'}, ensure_ascii=False)) urls = json.loads(subprocess.run(['node', str(Path(__file__).parent / 'static_notes.cjs'), str(dist), 'https://example.com/', str(dist / 'stubs.json')], check=True, capture_output=True, text=True).stdout) - self.assertEqual(urls, ['https://example.com/notes/%E7%A4%BA%E4%BE%8B.html']) + self.assertEqual(urls, ['https://example.com/notes/', 'https://example.com/notes/%E7%A4%BA%E4%BE%8B.html']) + self.assertIn('示例', (dist / 'notes' / 'index.html').read_text()) page = (dist / 'notes' / '示例.html').read_text() counts = collections.Counter() expected = [heading_slug(t, counts) for t in titles] diff --git a/website/templates/index.html b/website/templates/index.html index c0c207fa..d88907bd 100644 --- a/website/templates/index.html +++ b/website/templates/index.html @@ -60,7 +60,7 @@

正在载入 CS-Notes…
- +