add dynamic stylesheet loading
This commit is contained in:
parent
04abd9f5b4
commit
95423a7e4d
9
build.py
9
build.py
|
|
@ -64,13 +64,17 @@ def make_page(page, dest):
|
|||
post = frontmatter.loads(page)
|
||||
post['title'] = post['title']
|
||||
post.content = re.sub(r"py{{ (.+?) }}", lambda x: str(eval(x.group(1))), post.content)
|
||||
out = base.replace('{{ content }}', post.content).replace('{{ title }}', post['title']) #+ f' - {"μ.twoha.cc" if minimal else "u.twoha.cc"}')
|
||||
path_write(dest, out)
|
||||
out = base.replace('{{ content }}', post.content).replace('{{ title }}', post['title'] + f' - {"μ.twoha.cc" if minimal else "u.twoha.cc"}')
|
||||
if not minimal:
|
||||
dump = json.dumps(post.metadata)
|
||||
assert '-->' not in dump
|
||||
content = f'<!--{dump}-->\n{post.content}'
|
||||
sheets = (f'<link href="{x}" rel="stylesheet">' for x in ['/static/style.css', *post.get('sheets', [])])
|
||||
out = out.replace('{{ sheets }}', '\n'.join(sheets))
|
||||
path_write(outdir / '_partial' / dest.relative_to(outdir), content)
|
||||
path_write(dest, out)
|
||||
else:
|
||||
path_write(dest, out)
|
||||
|
||||
def main(_):
|
||||
outdir.mkdir(exist_ok=True, parents=True)
|
||||
|
|
@ -122,6 +126,7 @@ def md_to_html(path):
|
|||
html = markdown.markdown(input.content, extensions=ext)
|
||||
return f'''---
|
||||
title: {repr(input['title'])}
|
||||
sheets: ["/static/highlight.css"]
|
||||
---
|
||||
<div class="container">
|
||||
<article class="panel">
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import { popup } from '/static/util.js';
|
||||
import { popup, addSheet } from '/static/util.js';
|
||||
|
||||
const content = document.getElementById('content');
|
||||
let currentPathname = new URL(window.location).pathname;
|
||||
let sheets = {};
|
||||
|
||||
async function loadContent(u, push=false) {
|
||||
const sheet = window.document.styleSheets[0];
|
||||
|
|
@ -21,6 +22,18 @@ async function _loadContent(u, push=false) {
|
|||
if (r.ok) {
|
||||
let t = await r.text();
|
||||
let metadata = JSON.parse(t.substring(4, t.indexOf('-->\n')))
|
||||
let newSheets = metadata.sheets ?? [];
|
||||
let loading = []
|
||||
for (let s in sheets)
|
||||
if (!newSheets.includes(s))
|
||||
sheets[s].disabled = true;
|
||||
for (let s of newSheets) {
|
||||
if (!sheets[s])
|
||||
loading.push(addSheet(s).then(e => sheets[s] = e));
|
||||
else if (sheets[s].disabled)
|
||||
sheets[s].disabled = false;
|
||||
}
|
||||
await Promise.all(loading);
|
||||
content.innerHTML = t;
|
||||
if (push)
|
||||
history.pushState('', '', u);
|
||||
|
|
|
|||
|
|
@ -19,3 +19,13 @@ export function shuffle(array) {
|
|||
[array[i], array[j]] = [array[j], array[i]];
|
||||
}
|
||||
}
|
||||
export function addSheet(href) {
|
||||
return new Promise((res, rej) => {
|
||||
let link = document.createElement('link');
|
||||
link.rel = 'stylesheet';
|
||||
link.href = href;
|
||||
link.onload = () => res(link);
|
||||
link.onerror = () => rej();
|
||||
document.head.appendChild(link);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,7 @@
|
|||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{ title }}</title>
|
||||
<link href="/static/style.css" rel="stylesheet">
|
||||
<link href="/static/highlight.css" rel="stylesheet">
|
||||
{{ sheets }}
|
||||
<link rel="icon" href="/favicon.ico">
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
|||
Loading…
Reference in a new issue