diff --git a/website/builder/build.py b/website/builder/build.py index f76177cc..8e3909e5 100644 --- a/website/builder/build.py +++ b/website/builder/build.py @@ -239,6 +239,21 @@ def write_app_icons(dist, logo): (dist / 'manifest.webmanifest').write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + '\n') +def first_images(dist): + """每篇文章开头附近(前面的正文不到 1200 字,文首目录不算)的第一张站内配图,入口页据此预加载;与 site.js 的 nearTop 一致。GIF 在网站上换成逐帧播放器,不预加载。""" + result = {} + for path in sorted((dist / 'notes').glob('*.md')): + text = re.sub(r'[\s\S]*?', '', path.read_text()) + text = VERBATIM.sub('', text) + match = re.search(r']*\bsrc=["\'](/[^"\']+)["\']', text) + if not match or match[1].startswith('//') or match[1].lower().endswith('.gif'): + continue + before = re.sub(r'<[^>]+>|\]\([^)]*\)|^#+ ', '', text[:match.start()], flags=re.M) + if len(re.sub(r'\s+', '', before)) < 1200: + result['/notes/' + path.stem] = match[1] + return result + + def build(optimize=True): global IMAGE_BY_NAME, ANCHORS IMAGE_BY_NAME = repository_images() @@ -367,6 +382,7 @@ def build(optimize=True): if document.relative_to(dist).parts[0] != 'learn': document.with_suffix('.txt').write_bytes(document.read_bytes()) entry = (dist / 'index.html').read_text() + entry = entry.replace('{}/*FIRST_IMAGES*/', json.dumps(first_images(dist), ensure_ascii=False, separators=(',', ':'))) entry = bundle_entry(dist, entry) (dist / 'index.html').write_text(entry) shell = ['/', '/_sidebar.txt', '/README.txt'] + re.findall(r'(?:src|href)="(/(?:app|vendor|style)-[0-9a-f]{16}\.(?:js|css))"', entry) diff --git a/website/templates/index.html b/website/templates/index.html index f174961f..c0c207fa 100644 --- a/website/templates/index.html +++ b/website/templates/index.html @@ -32,6 +32,17 @@ link.href = href; document.head.appendChild(link); }); + // 文章开头附近的第一张配图同时开始下载(地址由构建写入) + var images = window.CS_NOTES_FIRST_IMAGES = {}/*FIRST_IMAGES*/, image = null; + try { image = images[decodeURIComponent(path)]; } catch (error) {} + if (image) { + var hint = document.createElement('link'); + hint.rel = 'preload'; + hint.as = 'image'; + hint.href = image; + hint.setAttribute('fetchpriority', 'high'); + document.head.appendChild(hint); + } })();