Merge pull request #1542 from CyC2018/site-static

Add static_notes.cjs
This commit is contained in:
CyC2018
2026-11-06 06:19:57 +08:00
committed by GitHub
6 changed files with 261 additions and 4 deletions
+3 -1
View File
@@ -53,7 +53,9 @@ verify_updates.py 在临时副本中修改文章、替换配图、新增文章
首页学习站横幅下面是“今日面试题”:每天按日期从面试最常问的题中换一道,链接到这道题在学习站中的页面。
构建时在根目录写入 robots.txt,指向学习站的站点地图(learn/sitemap.xml,列出每道题的静态页面)。
每篇笔记另外生成一个静态页面(notes/<名字>.html,builder/static_notes.cjs):网站本身的地址在 # 之后,搜索引擎只收录得到首页,静态页面是渲染好的正文,没有脚本,标题的 id 与网站一致(按 Docsify 的规则),站内链接指向其它笔记的静态页面,页面上有回到阅读器的链接;只剩一个链接的旧页面跳到目标页。Markdown 用 marked 1.2.9 渲染(和 Docsify 4.13.1 依赖的版本相同,文件及许可证在 builder/vendor 中),构建时需要 Node.js。
构建时在根目录写入站点地图 sitemap.xml(首页和这些静态页面)和 robots.txt,robots.txt 同时指向学习站的站点地图(learn/sitemap.xml,列出每道题的静态页面)。
## 自动更新与发布
+11 -3
View File
@@ -298,7 +298,7 @@ def build(optimize=True):
(dist / '_404.md').write_text('# 页面暂未找到\n\n请从目录或搜索查找笔记。\n\n[返回首页](/)\n')
(dist / '.nojekyll').touch()
# 允许收录;学习站每道题的页面列在它的站点地图中(笔记页面在 # 之后,没有单独的地址)
(dist / 'robots.txt').write_text('User-agent: *\nAllow: /\n' + (f'Sitemap: {SITE_URL}learn/sitemap.xml\n' if LEARN_BUILD.is_file() else ''))
(dist / 'robots.txt').write_text('User-agent: *\nAllow: /\n' + f'Sitemap: {SITE_URL}sitemap.xml\n' + (f'Sitemap: {SITE_URL}learn/sitemap.xml\n' if LEARN_BUILD.is_file() else ''))
shutil.copyfile(ROOT / 'docs/_media/LogoMakr_1J56bI.png', dist / 'favicon.png')
write_app_icons(dist, ROOT / 'docs/_media/LogoMakr_1J56bI.png')
learn_report = None
@@ -333,6 +333,14 @@ def build(optimize=True):
image_report['sized'] = add_image_sizes(dist)
index_name, search_report = write_search_index(dist, sorted((dist / 'notes').glob('*.md')))
animations = write_animations(dist)
# 每篇笔记的静态页面(notes/<名字>.html,搜索引擎收录用)和站点地图;配图已经换成优化后的地址、补上高度
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)],
check=True, capture_output=True, text=True).stdout)
(dist / 'sitemap.xml').write_text('<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n' +
''.join(f' <url><loc>{html.escape(url)}</loc></url>\n' for url in [SITE_URL] + static_urls) + '</urlset>\n')
legacy = json.loads((SITE / 'legacy-routes.json').read_text())
(dist / 'search-paths.js').write_text(
'window.CS_NOTES_PATHS = ' + json.dumps(note_paths, ensure_ascii=False) + ';\n'
@@ -345,12 +353,12 @@ def build(optimize=True):
'window.CS_NOTES_LEARN_SECTIONS = ' + json.dumps(learn_sections, ensure_ascii=False, separators=(',', ':')) + ';\n')
generator = hashlib.sha256()
learn = [*LEARN_BUILD.parent.glob('*.py'), *(LEARN_BUILD.parent / 'src').rglob('*')] if LEARN_BUILD.is_file() else []
for resource in sorted([*Path(__file__).parent.glob('*.py'), *SITE.rglob('*'), *learn]):
for resource in sorted([*Path(__file__).parent.glob('*.py'), *Path(__file__).parent.glob('*.cjs'), *(Path(__file__).parent / 'vendor').rglob('*'), *SITE.rglob('*'), *learn]):
if resource.is_file():
generator.update(resource.relative_to(BASE.parent).as_posix().encode())
generator.update(resource.read_bytes())
build_info = {'sourceCommit': SOURCE_COMMIT, 'generatorHash': generator.hexdigest(), 'notes': len(note_paths),
'searchIndex': index_name, 'legacyRoutes': len(legacy)}
'searchIndex': index_name, 'legacyRoutes': len(legacy), 'staticPages': len(static_urls)}
(dist / 'build-info.json').write_text(json.dumps(build_info, indent=2) + '\n')
# 网站读取的是同内容的 .txt:EdgeOne 只压缩 text/plain 等类型,.md 的 text/markdown 不压缩
for document in list(dist.rglob('*.md')):
+165
View File
@@ -0,0 +1,165 @@
// 每篇笔记生成一个静态页面(notes/<名字>.html):网站本身用 # 之后的地址,搜索引擎只收录得到首页;
// 静态页面是渲染好的正文,没有脚本,搜索进来的读者直接能读,页面上有回到阅读器(目录、搜索、深色模式、面试题练习)的链接。
// 用法:node static_notes.cjs <dist 目录> <站点地址>,输出生成的页面数;Markdown 用和 Docsify 4.13.1 同一个大版本的 marked 渲染,
// 标题的 id 按 Docsify 的规则生成(与 site_assets.py 的 heading_slug 相同),网站上的小节链接在静态页面上同样可用。
'use strict';
const fs = require('fs');
const path = require('path');
const marked = require('./vendor/marked.min.js');
const PUNCTUATION = /[\u2000-\u206f\u2e00-\u2e7f\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g;
function slugger() {
const counts = {};
return function (html) {
let slug = html.replace(/<[^>]+>/g, '').trim().toLowerCase().replace(PUNCTUATION, '');
slug = slug.replace(/\s/g, '-').replace(/-+/g, '-').replace(/^(\d)/, '_$1');
const count = counts[slug] || 0;
counts[slug] = count + 1;
return slug + (count ? '-' + count : '');
};
}
function escapeHtml(text) {
return String(text).replace(/[&<>"']/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]));
}
function plain(html) {
return html.replace(/<[^>]+>/g, '').replace(/&nbsp;/g, ' ').replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>')
.replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/\s+/g, ' ').trim();
}
const route = name => name.split('/').map(encodeURIComponent).join('/');
// 一级标题后面只有页内链接的列表是文首目录,折叠起来,和网站上一样
function foldToc(html) {
const head = html.match(/<\/h1>\s*<ul>/);
if (!head) return html;
const start = head.index + head[0].length - 4;
let depth = 0, i = start, end = -1;
const tag = /<(\/?)ul>/g;
tag.lastIndex = start;
for (let m; (m = tag.exec(html));) {
depth += m[1] ? -1 : 1;
if (!depth) { end = m.index + 5; break; }
}
if (end < 0) return html;
const list = html.slice(start, end), links = list.match(/<a href="([^"]*)"/g) || [];
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) {
const slug = slugger();
const renderer = new marked.Renderer();
renderer.heading = (text, level) => `<h${level} id="${slug(text)}">${text}</h${level}>\n`;
// 站内笔记的链接(/notes/X.md#小节)改为静态页面;其它链接不变,外链在新窗口打开
renderer.link = (href, title, text) => {
let url = href || '';
const note = url.match(/^\/notes\/([^#?]+)\.md(#.*)?$/);
if (note) url = '/notes/' + note[1] + '.html' + (note[2] || '');
const external = /^https?:\/\//.test(url) && !url.startsWith('https://cyc2018.xyz/');
return `<a href="${escapeHtml(url)}"${title ? ` title="${escapeHtml(title)}"` : ''}${external ? ' target="_blank" rel="noopener"' : ''}>${text}</a>`;
};
let html = marked(markdown.replace(/<!-- GFM-TOC -->/g, ''), { renderer, headerIds: false, mangle: false });
// 正文中直接写的 HTML 链接(<a href="/notes/X.md">)同样改为静态页面
html = html.replace(/(<a\b[^>]*?\bhref=")\/notes\/([^"#?]+)\.md(#[^"]*)?"/g, (m, a, n, h) => `${a}/notes/${n}.html${h || ''}"`);
// 配图按出现顺序,第一张直接加载,其余延迟加载;没写说明的用所在小节的标题作说明(和网站上一样)
let first = true, heading = '';
html = html.replace(/<h[1-6]\b[^>]*>([\s\S]*?)<\/h[1-6]>|<img\b([^>]*)>/g, (tag, title, attrs) => {
if (title !== undefined) { heading = plain(title); return tag; }
let out = '<img' + (/\bloading=/.test(attrs) ? '' : first ? ' decoding="async"' : ' loading="lazy" decoding="async"');
if (!/\balt=/.test(attrs)) out += ' alt="' + escapeHtml(heading ? heading + (/[!-~]$/.test(heading) ? ' ' : '') + '的配图' : '配图') + '"';
first = false;
return out + attrs + '>';
});
// 能横向滚动的代码块和表格要能用键盘聚焦后滚动
html = html.replace(/<(pre|table)>/g, '<$1 tabindex="0">');
html = foldToc(html);
const h1 = html.match(/<h1\b[^>]*>([\s\S]*?)<\/h1>/);
const title = h1 ? plain(h1[1]) : name.split('/').pop();
const para = html.match(/<p>([\s\S]*?)<\/p>/);
return { html, title, description: para ? plain(para[1]).slice(0, 120) : title };
}
const STYLE = `
:root { color-scheme: light dark; --bg: #f3f2ee; --surface: #fafbf8; --text: #34495e; --heading: #2c3e50; --muted: #5f6d66; --border: #dfe3da; --theme: #2a7a5a; --soft: #e7f1eb; --code: #9a3f2a; }
@media (prefers-color-scheme: dark) { :root { --bg: #1a1d1c; --surface: #1f2422; --text: #c9d1cc; --heading: #e6ebe7; --muted: #98a59d; --border: #2f3633; --theme: #5cbf92; --soft: #23312b; --code: #e7a28f; } img { filter: brightness(.9); background: #ecebe6; } }
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.95 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif; -webkit-text-size-adjust: 100%; }
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; }
.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; }
h1 { font-size: 2rem; margin: .4em 0 1em; }
h2 { margin-top: 2.6rem; padding-bottom: .4rem; border-bottom: 1px solid var(--border); }
img { max-width: 100%; height: auto; border-radius: 4px; }
pre { overflow: auto; padding: 14px 16px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); line-height: 1.65; }
code { font-family: "SFMono-Regular", Consolas, monospace; font-size: .88em; }
:not(pre) > code { color: var(--code); overflow-wrap: anywhere; }
table { display: block; overflow: auto; border-collapse: collapse; }
pre:focus-visible, table:focus-visible { outline: 2px solid var(--theme); outline-offset: 2px; }
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: " 🔥🔥"; }
.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; }
@media (max-width: 640px) { main { padding: 20px 18px 48px; } h1 { font-size: 1.6rem; } .top { padding: 8px 14px; } }
@media print { .top, .reader, footer { display: none; } body { background: #fff; } }
`.replace(/\n\s*/g, '');
function page({ html, title, description }, name, site, stub) {
const url = site + 'notes/' + route(name) + '.html';
const reader = '/#/notes/' + route(name);
const canonical = stub ? site + 'notes/' + route(stub) + '.html' : url;
return `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>${escapeHtml(title)} · CS-Notes</title>
<meta name="description" content="${escapeHtml(description)}">
<link rel="canonical" href="${escapeHtml(canonical)}">
${stub ? ` <meta http-equiv="refresh" content="0; url=/notes/${escapeHtml(route(stub))}.html">\n` : ''} <meta property="og:type" content="article">
<meta property="og:site_name" content="CS-Notes">
<meta property="og:title" content="${escapeHtml(title)}">
<meta property="og:description" content="${escapeHtml(description)}">
<meta property="og:url" content="${escapeHtml(url)}">
<meta name="theme-color" content="#f3f2ee" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#1a1d1c" media="(prefers-color-scheme: dark)">
<link rel="icon" href="/icons/favicon-32.png" sizes="32x32">
<style>${STYLE}</style>
</head>
<body>
<header class="top"><a class="brand" href="/">CS-Notes</a><a class="open" href="${escapeHtml(reader)}">在阅读器中打开</a></header>
<main>
<article>
${html}
</article>
<p class="reader">在阅读器中可以用目录和搜索、切换深色模式、练习这篇笔记中的面试题:<a href="${escapeHtml(reader)}">打开《${escapeHtml(title)}》</a></p>
</main>
<footer><a href="https://github.com/CyC2018/CS-Notes" target="_blank" rel="noopener">CyC2018 · CS-Notes</a> · <a href="https://beian.miit.gov.cn/" target="_blank" rel="noopener">粤ICP备19071404号-1</a></footer>
</body>
</html>
`;
}
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();
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);
fs.writeFileSync(path.join(dir, name + '.html'), page(rendered, name, site, stub));
if (!stub) urls.push(site + 'notes/' + route(name) + '.html');
}
return urls;
}
module.exports = { render, slugger, build };
if (require.main === module) {
const [dist, site, stubsFile] = process.argv.slice(2);
const stubs = stubsFile ? JSON.parse(fs.readFileSync(stubsFile, 'utf8')) : {};
process.stdout.write(JSON.stringify(build(dist, site, stubs)));
}
+32
View File
@@ -1,4 +1,6 @@
"""Regression checks for generated search anchors and asset collection."""
import re
import json
import sys
import tempfile
import unittest
@@ -84,5 +86,35 @@ class SearchIndexTests(unittest.TestCase):
'```html\n<img src="/pics/a.png"/>\n```\n')
def test_static_note_pages(self):
# 静态页面的标题 id 与网站(Docsify)一致,站内链接换成静态页面,文首目录折叠,只剩一个链接的旧页面跳到目标
import collections, subprocess
with tempfile.TemporaryDirectory() as directory:
dist = Path(directory)
(dist / 'notes').mkdir()
titles = ['示例', '1. 爬楼梯', 'HashMap 的 `put()`', '"引号"', '示例', 'A1 实测']
toc = '\n'.join(f'* [{t}](#x{i})' for i, t in enumerate(titles))
body = f'# {titles[0]}\n<!-- GFM-TOC -->\n{toc}\n<!-- GFM-TOC -->\n\n正文第一段,见 [另一篇](/notes/%E5%8F%A6.md#_1-%E7%88%AC%E6%A5%BC%E6%A2%AF)。\n\n' + \
''.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 / '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'])
page = (dist / 'notes' / '示例.html').read_text()
counts = collections.Counter()
expected = [heading_slug(t, counts) for t in titles]
self.assertEqual(re.findall(r'<h[1-6] id="([^"]*)"', page), expected)
self.assertIn('href="/notes/%E5%8F%A6.html#_1-%E7%88%AC%E6%A5%BC%E6%A2%AF"', page)
self.assertIn('href="/notes/%E5%8F%A6.html"', page)
self.assertIn('<details class="toc"><summary>本文目录</summary>', page)
self.assertIn('<meta name="description" content="正文第一段,见 另一篇。">', page)
self.assertIn('href="/#/notes/%E7%A4%BA%E4%BE%8B"', page)
self.assertNotIn('<script', 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)
if __name__ == '__main__':
unittest.main()
+44
View File
@@ -0,0 +1,44 @@
# License information
## Contribution License Agreement
If you contribute code to this project, you are implicitly allowing your code
to be distributed under the MIT license. You are also implicitly verifying that
all code is your original work. `</legalese>`
## Marked
Copyright (c) 2018+, MarkedJS (https://github.com/markedjs/)
Copyright (c) 2011-2018, Christopher Jeffrey (https://github.com/chjj/)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
## Markdown
Copyright © 2004, John Gruber
http://daringfireball.net/
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name “Markdown” nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
This software is provided by the copyright holders and contributors “as is” and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the copyright owner or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
File diff suppressed because one or more lines are too long