在阅读器中可以用目录和搜索、切换深色模式、练习这篇笔记中的面试题:打开《${escapeHtml(title)}》
+([\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 ` + +
+ + +在阅读器中可以用目录和搜索、切换深色模式、练习这篇笔记中的面试题:打开《${escapeHtml(title)}》
+
\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\n{toc}\n\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:]) + 'HTML 链接\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'