Merge pull request #1548 from CyC2018/site-qs

Update static_notes.cjs
This commit is contained in:
CyC2018
2026-11-06 07:32:38 +08:00
committed by GitHub
3 changed files with 23 additions and 8 deletions
+3 -1
View File
@@ -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')
+17 -6
View File
@@ -44,10 +44,18 @@ function foldToc(html) {
if (links.length < 6 || !links.every(a => a.startsWith('<a href="#'))) return html;
return html.slice(0, start) + '<details class="toc"><summary>本文目录</summary>\n' + list + '</details>' + html.slice(end);
}
function render(markdown, name) {
// 学习站中原文链接指向这一节的面试题,列在小节标题下面,链接到每道题的页面
function questionsHtml(list) {
return '<aside class="qs"><b>面试题</b><ul>' + list.map(([id, text, heat]) =>
`<li><a href="/learn/q/${encodeURIComponent(id)}.html">${escapeHtml(text)}</a>${heat > 1 ? '<span class="hot-2"></span>' : heat ? '<span class="hot-1"></span>' : ''}</li>`).join('') + '</ul></aside>\n';
}
function render(markdown, name, questions) {
const slug = slugger();
const renderer = new marked.Renderer();
renderer.heading = (text, level) => `<h${level} id="${slug(text)}">${text}</h${level}>\n`;
renderer.heading = (text, level) => {
const id = slug(text);
return `<h${level} id="${id}">${text}</h${level}>\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)));
}
+3 -1
View File
@@ -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('<a href="/notes/%E7%A4%BA%E4%BE%8B.html">示例</a>', (dist / 'notes' / 'index.html').read_text())
@@ -114,6 +115,7 @@ class SearchIndexTests(unittest.TestCase):
self.assertIn('<meta name="description" content="正文第一段,见 另一篇。">', page)
self.assertIn('href="/#/notes/%E7%A4%BA%E4%BE%8B"', page)
self.assertNotIn('<script', page)
self.assertIn('<h2 id="_1-爬楼梯">1. 爬楼梯</h2>\n<aside class="qs"><b>面试题</b><ul><li><a href="/learn/q/q1.html">怎样爬楼梯?</a>', page)
stub = (dist / 'notes' / '算法.html').read_text()
self.assertIn('url=/notes/%E7%AE%97%E6%B3%95%20-%20%E7%9B%AE%E5%BD%95.html', stub)