diff --git a/website/builder/build.py b/website/builder/build.py index dc732d6b..a0a530a5 100644 --- a/website/builder/build.py +++ b/website/builder/build.py @@ -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) diff --git a/website/templates/edgeone.json b/website/templates/edgeone.json deleted file mode 100644 index 199dd80c..00000000 --- a/website/templates/edgeone.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "headers": [ - { "source": "/*.md", "headers": [{ "key": "Content-Type", "value": "text/plain; charset=utf-8" }] } - ] -} diff --git a/website/templates/index.html b/website/templates/index.html index 2e9fdd24..23622287 100644 --- a/website/templates/index.html +++ b/website/templates/index.html @@ -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'; diff --git a/website/templates/site.js b/website/templates/site.js index eb863e85..5170ffa4 100644 --- a/website/templates/site.js +++ b/website/templates/site.js @@ -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, diff --git a/website/templates/sw.js b/website/templates/sw.js index 21136bee..7c68d8dc 100644 --- a/website/templates/sw.js +++ b/website/templates/sw.js @@ -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)); }