${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 };