Merge pull request #1546 from CyC2018/site-index

Update static_notes.cjs
This commit is contained in:
CyC2018
2026-11-06 07:11:31 +08:00
committed by GitHub
4 changed files with 30 additions and 5 deletions
+3 -1
View File
@@ -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',
+23 -2
View File
@@ -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 ? ` <meta http-equiv="refresh" content="0; url=/notes/${escapeHtml(route
<style>${STYLE}</style>
</head>
<body>
<header class="top"><a class="brand" href="/">CS-Notes</a><a class="open" href="${escapeHtml(reader)}">在阅读器中打开</a></header>
<header class="top"><span><a class="brand" href="/">CS-Notes</a><a class="all" href="/notes/">全部笔记</a></span><a class="open" href="${escapeHtml(reader)}">在阅读器中打开</a></header>
<main>
<article>
${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 = '<h1>全部笔记</h1>\n<div class="groups">' + groups.filter(g => g.items.length).map(g => '<section><h2>' + escapeHtml(g.title) + '</h2><ul>' +
g.items.map(([text, file]) => `<li><a href="/notes/${escapeHtml(file)}.html">${escapeHtml(text)}</a></li>`).join('') + '</ul></section>').join('\n') + '</div>';
const html = page({ html: body, title: '全部笔记', description: 'CS-Notes 全部笔记:算法、操作系统、计算机网络、数据库、Java、系统设计、大模型应用等。' }, '', site, null)
.replace(/<link rel="canonical" href="[^"]*">/, `<link rel="canonical" href="${site}notes/">`).replace(/<meta property="og:url" content="[^"]*">/, `<meta property="og:url" content="${site}notes/">`)
.replace(/<a class="open" href="[^"]*">在阅读器中打开<\/a>/, '<a class="open" href="/">在阅读器中打开</a>').replace(/<p class="reader">[\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 };
+3 -1
View File
@@ -98,10 +98,12 @@ class SearchIndexTests(unittest.TestCase):
''.join(f'## {t}\n\n内容\n\n' for t in titles[1:]) + '<a href="/notes/%E5%8F%A6.md">HTML 链接</a>\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('<a href="/notes/%E7%A4%BA%E4%BE%8B.html">示例</a>', (dist / 'notes' / 'index.html').read_text())
page = (dist / 'notes' / '示例.html').read_text()
counts = collections.Counter()
expected = [heading_slug(t, counts) for t in titles]
+1 -1
View File
@@ -60,7 +60,7 @@
</head>
<body>
<div id="app">正在载入 CS-Notes…</div>
<noscript>请启用 JavaScript 阅读网站,或访问 <a href="https://github.com/CyC2018/CS-Notes">GitHub 仓库</a>。</noscript>
<noscript>请启用 JavaScript 阅读网站,或打开不需要脚本的<a href="/notes/">全部笔记</a>,或访问 <a href="https://github.com/CyC2018/CS-Notes">GitHub 仓库</a>。</noscript>
<script src="/search-paths.js" defer></script>
<script src="/site.js" defer></script>
<script src="/search.js" defer></script>