diff --git a/website/builder/build.py b/website/builder/build.py index 8e3909e5..6ebc5860 100644 --- a/website/builder/build.py +++ b/website/builder/build.py @@ -352,7 +352,9 @@ def build(optimize=True): stubs_file = CACHE / 'stubs.json' stubs_file.parent.mkdir(parents=True, exist_ok=True) stubs_file.write_text(json.dumps(stubs, ensure_ascii=False)) - static_urls = json.loads(subprocess.run(['node', str(Path(__file__).parent / 'static_notes.cjs'), str(dist), SITE_URL, str(stubs_file)], + sections_file = CACHE / 'learn-sections.json' + sections_file.write_text(json.dumps(learn_sections, ensure_ascii=False)) + static_urls = json.loads(subprocess.run(['node', str(Path(__file__).parent / 'static_notes.cjs'), str(dist), SITE_URL, str(stubs_file), str(sections_file)], check=True, capture_output=True, text=True).stdout) # IndexNow 的验证文件:向必应等搜索引擎提交新地址时,用它证明网站归属(内容就是文件名中的密钥,公开的) (dist / '55327585d023607ab2aea4353f71eaeb.txt').write_text('55327585d023607ab2aea4353f71eaeb') diff --git a/website/builder/static_notes.cjs b/website/builder/static_notes.cjs index fb43b424..c52fb061 100644 --- a/website/builder/static_notes.cjs +++ b/website/builder/static_notes.cjs @@ -44,10 +44,18 @@ function foldToc(html) { if (links.length < 6 || !links.every(a => a.startsWith('本文目录\n' + list + '' + html.slice(end); } -function render(markdown, name) { +// 学习站中原文链接指向这一节的面试题,列在小节标题下面,链接到每道题的页面 +function questionsHtml(list) { + return '\n'; +} +function render(markdown, name, questions) { const slug = slugger(); const renderer = new marked.Renderer(); - renderer.heading = (text, level) => `${text}\n`; + renderer.heading = (text, level) => { + const id = slug(text); + return `${text}\n` + (questions && questions[id] && level > 1 ? questionsHtml(questions[id]) : ''); + }; // 站内笔记的链接(/notes/X.md#小节)改为静态页面;其它链接不变,外链在新窗口打开 renderer.link = (href, title, text) => { let url = href || ''; @@ -102,6 +110,7 @@ td, th { padding: 6px 12px; border: 1px solid var(--border); } blockquote { margin: 1em 0; padding: 2px 16px; border-left: 4px solid var(--theme); background: var(--soft); } .toc { margin: 0 0 28px; padding: 6px 18px; border: 1px solid var(--border); border-radius: 7px; background: var(--surface); }.toc summary { color: var(--theme); font-weight: 600; cursor: pointer; padding: 6px 0; }.toc a { text-decoration: none; }.toc ul { margin: 4px 0; line-height: 1.75; } .hot-1::after { content: " 🔥"; } .hot-2::after { content: " 🔥🔥"; } +.qs { margin: -4px 0 16px; padding: 8px 14px; border: 1px solid var(--border); border-radius: 7px; background: var(--surface); font-size: 14.5px; }.qs b { color: var(--theme); font-size: 13px; }.qs ul { margin: 2px 0 0; padding-left: 18px; line-height: 1.8; }.qs a { text-decoration: none; } .reader { margin: 40px 0 0; padding: 14px 18px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); font-size: 15px; } footer { padding: 20px; border-top: 1px solid var(--border); color: var(--muted); font-size: 12px; text-align: center; } footer a { color: inherit; } @@ -164,13 +173,14 @@ function indexPage(dist, site) { return site + 'notes/'; } -function build(dist, site, stubs) { +function build(dist, site, stubs, sections) { + sections = sections || {}; const dir = path.join(dist, 'notes'); const names = fs.readdirSync(dir).filter(f => f.endsWith('.md')).map(f => f.slice(0, -3)).sort(); const urls = []; for (const name of names) { const stub = stubs['/notes/' + name] && stubs['/notes/' + name].replace(/^\/notes\//, ''); - const rendered = render(fs.readFileSync(path.join(dir, name + '.md'), 'utf8'), name); + const rendered = render(fs.readFileSync(path.join(dir, name + '.md'), 'utf8'), name, sections[name]); fs.writeFileSync(path.join(dir, name + '.html'), page(rendered, name, site, stub)); if (!stub) urls.push(site + 'notes/' + route(name) + '.html'); } @@ -180,7 +190,8 @@ function build(dist, site, stubs) { module.exports = { render, slugger, build }; if (require.main === module) { - const [dist, site, stubsFile] = process.argv.slice(2); + const [dist, site, stubsFile, sectionsFile] = process.argv.slice(2); const stubs = stubsFile ? JSON.parse(fs.readFileSync(stubsFile, 'utf8')) : {}; - process.stdout.write(JSON.stringify(build(dist, site, stubs))); + const sections = sectionsFile ? JSON.parse(fs.readFileSync(sectionsFile, 'utf8')) : {}; + process.stdout.write(JSON.stringify(build(dist, site, stubs, sections))); } diff --git a/website/builder/test_site_assets.py b/website/builder/test_site_assets.py index 17a1e822..b1da9376 100644 --- a/website/builder/test_site_assets.py +++ b/website/builder/test_site_assets.py @@ -100,7 +100,8 @@ class SearchIndexTests(unittest.TestCase): (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')], + (dist / 'sections.json').write_text(json.dumps({'示例': {'_1-爬楼梯': [['q1', '怎样爬楼梯?', 2]]}}, 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'), str(dist / 'sections.json')], check=True, capture_output=True, text=True).stdout) 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()) @@ -114,6 +115,7 @@ class SearchIndexTests(unittest.TestCase): self.assertIn('', page) self.assertIn('href="/#/notes/%E7%A4%BA%E4%BE%8B"', page) self.assertNotIn('1. 爬楼梯\n