Merge pull request #1521 from CyC2018/site-txt

Update website to load notes as text files
This commit is contained in:
CyC2018
2026-11-01 13:23:36 +08:00
committed by GitHub
5 changed files with 14 additions and 14 deletions
+6 -2
View File
@@ -189,7 +189,7 @@ def build(optimize=True):
dist = Path(temporary) / 'dist'
dist.mkdir()
shutil.copytree(SITE / 'vendor', dist / 'vendor')
for name in ('index.html', 'site.css', 'site.js', 'search.js', 'edgeone.json'):
for name in ('index.html', 'site.css', 'site.js', 'search.js'):
shutil.copyfile(SITE / name, dist / name)
for path in sources():
target = dist / path.relative_to(ROOT)
@@ -281,10 +281,14 @@ def build(optimize=True):
build_info = {'sourceCommit': SOURCE_COMMIT, 'generatorHash': generator.hexdigest(), 'notes': len(note_paths),
'searchIndex': index_name, 'legacyRoutes': len(legacy)}
(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')):
if document.relative_to(dist).parts[0] != 'learn':
document.with_suffix('.txt').write_bytes(document.read_bytes())
entry = (dist / 'index.html').read_text()
entry = bundle_entry(dist, entry)
(dist / 'index.html').write_text(entry)
shell = ['/', '/_sidebar.md', '/README.md'] + re.findall(r'(?:src|href)="(/(?:app|vendor|style)-[0-9a-f]{16}\.(?:js|css))"', entry)
shell = ['/', '/_sidebar.txt', '/README.txt'] + re.findall(r'(?:src|href)="(/(?:app|vendor|style)-[0-9a-f]{16}\.(?:js|css))"', entry)
worker = (SITE / 'sw.js').read_text().replace('__SHELL__', json.dumps(shell))
(dist / 'sw.js').write_text(worker.replace('__BUILD__', hashlib.sha256(json.dumps(shell).encode()).hexdigest()[:12]))
write_manifest(dist, build_info)
-5
View File
@@ -1,5 +0,0 @@
{
"headers": [
{ "source": "/*.md", "headers": [{ "key": "Content-Type", "value": "text/plain; charset=utf-8" }] }
]
}
+2 -2
View File
@@ -11,8 +11,8 @@
// 正文和侧边栏的 Markdown 在 Docsify 载入前就开始下载,Docsify 请求同一地址时直接用下载好的内容
(function () {
var path = location.hash.replace(/^#/, '').split('?')[0] || '/';
var file = path === '/' ? '/README.md' : /^\/notes\/[^.]+$/.test(path) ? path + '.md' : null;
[file, '/_sidebar.md'].forEach(function (href) {
var file = path === '/' ? '/README.txt' : /^\/notes\/[^.]+$/.test(path) ? path + '.txt' : null;
[file, '/_sidebar.txt'].forEach(function (href) {
if (!href) return;
var link = document.createElement('link');
link.rel = 'preload';
+3 -2
View File
@@ -332,9 +332,10 @@
window.$docsify = {
name: 'CS-Notes',
repo: 'https://github.com/CyC2018/CS-Notes',
homepage: 'README.md',
homepage: 'README.txt',
ext: '.txt',
loadSidebar: true,
alias: { '/.*/_sidebar.md': '/_sidebar.md' },
alias: { '/.*/_sidebar.txt': '/_sidebar.txt' },
subMaxLevel: 0,
auto2top: true,
notFoundPage: true,
+3 -3
View File
@@ -30,7 +30,7 @@ function put(name, request, response) {
caches.open(name).then(function (cache) { return cache.put(request, copy); }).then(function () { if (LIMITS[name]) return trim(name); });
}
function offlinePage(path) {
var title = decodeURIComponent(path.split('/').pop().replace(/\.md$/, ''));
var title = decodeURIComponent(path.split('/').pop().replace(/\.(md|txt)$/, ''));
return new Response('# 当前离线\n\n《' + title + '》还没有在这台设备上打开过,联网后再试。已经读过的文章离线时也能打开。\n',
{ status: 200, headers: { 'Content-Type': 'text/plain; charset=utf-8', 'X-CS-Notes-Offline': '1' } });
}
@@ -61,8 +61,8 @@ self.addEventListener('fetch', function (event) {
if (path === '/' || path === '/index.html') event.respondWith(networkFirst(new Request('/'), STATIC, 3000, null));
} else if (/^\/(app|vendor|style)-[0-9a-f]{16}\.(js|css)$|^\/search-index-[0-9a-f]+\.json$|^\/optimized\//.test(path)) {
event.respondWith(cacheFirst(request, /^\/optimized\//.test(path) ? IMAGES : STATIC));
} else if (/\.md$/.test(path)) {
event.respondWith(networkFirst(request, PAGES, 3000, path === '/_sidebar.md' ? null : function () { return offlinePage(path); }));
} else if (/\.(md|txt)$/.test(path)) {
event.respondWith(networkFirst(request, PAGES, 3000, /^\/_sidebar\.(md|txt)$/.test(path) ? null : function () { return offlinePage(path); }));
} else if (/\.(png|jpe?g|gif|svg|webp)$/.test(path)) {
event.respondWith(networkFirst(request, IMAGES, 3000, null));
}