@taekwondo-db/web-app 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -8
- package/assets/css/components/detail.css +17 -4
- package/assets/css/components/downloads.css +86 -0
- package/assets/css/components/layout.css +12 -0
- package/assets/css/components/pronunciation.css +113 -0
- package/assets/css/style.css +2 -0
- package/assets/fonts/noto-serif-kr.css +2 -1
- package/assets/js/main.js +4 -1
- package/assets/js/pronunciation.js +35 -0
- package/assets/js/pwa.js +160 -23
- package/assets/js/search.js +1 -1
- package/bin/taekwondo-db.js +30 -13
- package/i18n/de.json +25 -2
- package/i18n/en.json +25 -2
- package/i18n/ko.json +19 -2
- package/layouts/_partials/category-cards.html +1 -1
- package/layouts/_partials/category-section.html +1 -1
- package/layouts/_partials/downloads-dialog.html +20 -0
- package/layouts/_partials/fonts.html +1 -1
- package/layouts/_partials/head.html +6 -1
- package/layouts/_partials/meta-description.html +60 -0
- package/layouts/_partials/pronunciation.html +23 -0
- package/layouts/_partials/scripts.html +3 -7
- package/layouts/_partials/search-index.html +2 -0
- package/layouts/_partials/structured-data.html +54 -0
- package/layouts/baseof.html +5 -3
- package/layouts/robots.txt +4 -0
- package/layouts/source/page.html +1 -1
- package/layouts/technique/page.html +9 -5
- package/layouts/term/page.html +2 -1
- package/lib/database.js +1 -0
- package/lib/export.js +83 -11
- package/lib/fonts.js +36 -0
- package/lib/service-worker.js +170 -46
- package/lib/validate.js +14 -2
- package/package.json +4 -3
|
@@ -20,10 +20,11 @@
|
|
|
20
20
|
</a>
|
|
21
21
|
|
|
22
22
|
<div class="title-block__line">
|
|
23
|
-
<h1{{ if $korean }} lang="ko"{{ end }}>{{ $name }}</h1>
|
|
23
|
+
<h1{{ if $korean }} lang="ko"{{ end }}>{{ cond $korean $name $t.hyphenated }}</h1>
|
|
24
24
|
{{- if not $korean }}
|
|
25
25
|
<span class="hangeul" lang="ko">{{ $t.hangeul }}</span>
|
|
26
26
|
{{- end }}
|
|
27
|
+
{{- partial "pronunciation.html" (dict "pronunciation" $t.pronunciation) }}
|
|
27
28
|
</div>
|
|
28
29
|
|
|
29
30
|
{{- with index $t.translation $lang }}
|
|
@@ -52,10 +53,13 @@
|
|
|
52
53
|
<section class="card card--full">
|
|
53
54
|
<div class="card-body">
|
|
54
55
|
<h2 class="section-title mb-3">{{ T "weight" }}</h2>
|
|
55
|
-
<
|
|
56
|
-
<
|
|
57
|
-
<
|
|
58
|
-
|
|
56
|
+
<svg class="weight-bar" viewBox="0 0 {{ add .front .back }} 1" preserveAspectRatio="none" role="img" aria-label="{{ .front }} % {{ T "front" }}, {{ .back }} % {{ T "back" }}">
|
|
57
|
+
<rect class="weight-bar__front" width="{{ .front }}" height="1" />
|
|
58
|
+
<rect class="weight-bar__back" x="{{ .front }}" width="{{ .back }}" height="1" />
|
|
59
|
+
{{- if and .front .back }}
|
|
60
|
+
<line class="weight-bar__gap" x1="{{ .front }}" x2="{{ .front }}" y2="1" />
|
|
61
|
+
{{- end }}
|
|
62
|
+
</svg>
|
|
59
63
|
<div class="weight-values">
|
|
60
64
|
<div><b>{{ .front }} %</b> <span>{{ T "front" }}</span></div>
|
|
61
65
|
<div><b>{{ .back }} %</b> <span>{{ T "back" }}</span></div>
|
package/layouts/term/page.html
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
{{- if not $korean }}
|
|
15
15
|
<span class="hangeul" lang="ko">{{ $term.hangeul }}</span>
|
|
16
16
|
{{- end }}
|
|
17
|
+
{{- partial "pronunciation.html" (dict "pronunciation" $term.pronunciation) }}
|
|
17
18
|
{{- with $term.hanja }}
|
|
18
19
|
<span class="romanization">{{ . }}</span>
|
|
19
20
|
{{- end }}
|
|
@@ -50,7 +51,7 @@
|
|
|
50
51
|
{{- partial "term/list.html" (dict "title" "termCombinations" "keys" $term.combinations) }}
|
|
51
52
|
|
|
52
53
|
{{- with $term.techniques }}
|
|
53
|
-
<section class="stack card--full"
|
|
54
|
+
<section class="stack-sm card--full">
|
|
54
55
|
<h2 class="section-title">{{ T "occurs in" }} · {{ len . }} {{ T "techniques" }}</h2>
|
|
55
56
|
<ul class="result-list">
|
|
56
57
|
{{- range . }}
|
package/lib/database.js
CHANGED
package/lib/export.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* .taekwondo-db/themes/taekwondo-data/
|
|
10
10
|
* data/taekwondo.json the resolved database
|
|
11
11
|
* assets/img/… images of the loaded techniques only
|
|
12
|
+
* assets/audio/… pronunciations of the loaded entries only
|
|
12
13
|
* assets/fonts/… fonts, flat — see copyFonts()
|
|
13
14
|
* assets/vendor/…, assets/js/vendor/… Bootstrap, the database's search
|
|
14
15
|
*
|
|
@@ -20,6 +21,7 @@ import { createRequire } from "node:module";
|
|
|
20
21
|
import path from "node:path";
|
|
21
22
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
22
23
|
import {
|
|
24
|
+
builtinAudioDir,
|
|
23
25
|
builtinImageDir,
|
|
24
26
|
composeDatabase,
|
|
25
27
|
stripRefs,
|
|
@@ -85,6 +87,11 @@ function conventionalDbOptions(dir) {
|
|
|
85
87
|
return db;
|
|
86
88
|
}
|
|
87
89
|
|
|
90
|
+
function dirsOf(db, root, dirs, builtinDir) {
|
|
91
|
+
const own = dirs.map((dir) => path.resolve(root, dir));
|
|
92
|
+
return db.includeBuiltins === false ? own : [builtinDir, ...own];
|
|
93
|
+
}
|
|
94
|
+
|
|
88
95
|
/**
|
|
89
96
|
* @param {Record<string, any>} db
|
|
90
97
|
* @param {string} root
|
|
@@ -92,8 +99,17 @@ function conventionalDbOptions(dir) {
|
|
|
92
99
|
* @returns {string[]} absolute image directories; later ones win
|
|
93
100
|
*/
|
|
94
101
|
export function imageDirsOf(db, root, dirs = ["img"]) {
|
|
95
|
-
|
|
96
|
-
|
|
102
|
+
return dirsOf(db, root, dirs, builtinImageDir);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @param {Record<string, any>} db
|
|
107
|
+
* @param {string} root
|
|
108
|
+
* @param {string[]} [dirs] own audio directories, relative to `root`
|
|
109
|
+
* @returns {string[]} absolute audio directories; later ones win
|
|
110
|
+
*/
|
|
111
|
+
export function audioDirsOf(db, root, dirs = ["audio"]) {
|
|
112
|
+
return dirsOf(db, root, dirs, builtinAudioDir);
|
|
97
113
|
}
|
|
98
114
|
|
|
99
115
|
/**
|
|
@@ -101,9 +117,10 @@ export function imageDirsOf(db, root, dirs = ["img"]) {
|
|
|
101
117
|
* @param {string} options.root site directory
|
|
102
118
|
* @param {Record<string, any>} options.db
|
|
103
119
|
* @param {string[]} options.imageDirs absolute
|
|
120
|
+
* @param {string[]} options.audioDirs absolute
|
|
104
121
|
* @returns {{ themesDir: string }}
|
|
105
122
|
*/
|
|
106
|
-
export function exportSite({ root, db, imageDirs }) {
|
|
123
|
+
export function exportSite({ root, db, imageDirs, audioDirs }) {
|
|
107
124
|
const database = new TaekwondoDatabase({
|
|
108
125
|
includeBuiltins: false,
|
|
109
126
|
...composeDatabase(db),
|
|
@@ -121,7 +138,15 @@ export function exportSite({ root, db, imageDirs }) {
|
|
|
121
138
|
path.join(dataTheme, "data", "taekwondo.json"),
|
|
122
139
|
JSON.stringify(model),
|
|
123
140
|
);
|
|
124
|
-
|
|
141
|
+
const images = Object.values(model.techniques).flatMap((t) =>
|
|
142
|
+
t.images.map((i) => i.src),
|
|
143
|
+
);
|
|
144
|
+
copyFound(files, images, imageDirs, path.join(assets, "img"));
|
|
145
|
+
const pronunciations = [
|
|
146
|
+
...Object.values(model.techniques),
|
|
147
|
+
...Object.values(model.terms),
|
|
148
|
+
].flatMap((entry) => entry.pronunciation?.src ?? []);
|
|
149
|
+
copyFound(files, pronunciations, audioDirs, path.join(assets, "audio"));
|
|
125
150
|
copyFonts(files, path.join(assets, "fonts"));
|
|
126
151
|
copyVendor(files, assets);
|
|
127
152
|
syncTree(dataTheme, files);
|
|
@@ -192,6 +217,48 @@ export function slugify(value) {
|
|
|
192
217
|
/** `$space`, `$missing` … are building blocks of the data, not pages. */
|
|
193
218
|
const isInternal = (key) => key.startsWith("$");
|
|
194
219
|
|
|
220
|
+
/**
|
|
221
|
+
* The name with soft hyphens between its terms, so a long word breaks there
|
|
222
|
+
* rather than off the screen. Sound changes alter a term's spelling inside a
|
|
223
|
+
* name (bakkat + makgi → Bakkanmakgi), so terms are matched from both ends.
|
|
224
|
+
*/
|
|
225
|
+
function hyphenate(name, terms) {
|
|
226
|
+
const words = name.split(" ");
|
|
227
|
+
const groups = [[]];
|
|
228
|
+
for (const term of terms) {
|
|
229
|
+
if (term === "$space") groups.push([]);
|
|
230
|
+
else groups.at(-1).push(term.toLowerCase());
|
|
231
|
+
}
|
|
232
|
+
if (groups.length !== words.length) return name;
|
|
233
|
+
|
|
234
|
+
return words
|
|
235
|
+
.map((word, index) => {
|
|
236
|
+
const lower = word.toLowerCase();
|
|
237
|
+
const parts = groups[index];
|
|
238
|
+
const cuts = new Set();
|
|
239
|
+
let start = 0;
|
|
240
|
+
let first = 0;
|
|
241
|
+
while (first < parts.length && lower.startsWith(parts[first], start)) {
|
|
242
|
+
start += parts[first++].length;
|
|
243
|
+
cuts.add(start);
|
|
244
|
+
}
|
|
245
|
+
let end = lower.length;
|
|
246
|
+
for (let last = parts.length - 1; last > first; last--) {
|
|
247
|
+
if (!lower.slice(0, end).endsWith(parts[last])) break;
|
|
248
|
+
end -= parts[last].length;
|
|
249
|
+
cuts.add(end);
|
|
250
|
+
}
|
|
251
|
+
let hyphenated = word;
|
|
252
|
+
for (const cut of [...cuts].sort((a, b) => b - a)) {
|
|
253
|
+
if (cut > 0 && cut < word.length) {
|
|
254
|
+
hyphenated = `${hyphenated.slice(0, cut)}\u00ad${hyphenated.slice(cut)}`;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return hyphenated;
|
|
258
|
+
})
|
|
259
|
+
.join(" ");
|
|
260
|
+
}
|
|
261
|
+
|
|
195
262
|
const mapValues = (object = {}, fn) =>
|
|
196
263
|
Object.fromEntries(
|
|
197
264
|
Object.entries(object).map(([key, value]) => [key, fn(value)]),
|
|
@@ -238,6 +305,8 @@ function buildModel(database) {
|
|
|
238
305
|
);
|
|
239
306
|
const citations = (sources = []) =>
|
|
240
307
|
sources.map((s) => ({ key: s.type.name, page: s.page ?? "" }));
|
|
308
|
+
const pronunciation = (p) =>
|
|
309
|
+
p ? { ai: Boolean(p.ai), speaker: p.speaker ?? "", src: p.src } : null;
|
|
241
310
|
const historic = (entries = []) =>
|
|
242
311
|
entries.map((h) => ({
|
|
243
312
|
hangeul: h.hangeul,
|
|
@@ -281,12 +350,17 @@ function buildModel(database) {
|
|
|
281
350
|
group: t.group,
|
|
282
351
|
hangeul: terms.map((term) => term.hangeul).join(""),
|
|
283
352
|
historic: historic(t.historic),
|
|
353
|
+
hyphenated: hyphenate(
|
|
354
|
+
key,
|
|
355
|
+
terms.map((term) => term.name),
|
|
356
|
+
),
|
|
284
357
|
images: (t.images ?? []).map((image) => ({
|
|
285
358
|
ai: Boolean(image.ai),
|
|
286
359
|
description: image.description ?? {},
|
|
287
360
|
src: image.src,
|
|
288
361
|
})),
|
|
289
362
|
name: key,
|
|
363
|
+
pronunciation: pronunciation(t.pronunciation),
|
|
290
364
|
references: (t.references ?? []).map((r) => ({
|
|
291
365
|
key: r.technique.name,
|
|
292
366
|
type: r.type,
|
|
@@ -315,6 +389,7 @@ function buildModel(database) {
|
|
|
315
389
|
lemma: term.lemma?.name ?? "",
|
|
316
390
|
name: key,
|
|
317
391
|
plain: mapValues(term.description, stripRefs),
|
|
392
|
+
pronunciation: pronunciation(term.pronunciation),
|
|
318
393
|
slug: slugOf("term", key),
|
|
319
394
|
techniques: uniqueNames(term.techniques),
|
|
320
395
|
terms: uniqueNames(term.terms),
|
|
@@ -343,13 +418,10 @@ function buildModel(database) {
|
|
|
343
418
|
return { grades, groups, sources, techniques, terms };
|
|
344
419
|
}
|
|
345
420
|
|
|
346
|
-
/** Only
|
|
347
|
-
function
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
);
|
|
351
|
-
for (const src of wanted) {
|
|
352
|
-
const found = imageDirs
|
|
421
|
+
/** Only files a loaded entry refers to; a site's own file wins over the database's. */
|
|
422
|
+
function copyFound(files, srcs, dirs, target) {
|
|
423
|
+
for (const src of new Set(srcs)) {
|
|
424
|
+
const found = dirs
|
|
353
425
|
.map((dir) => path.join(dir, src))
|
|
354
426
|
.findLast((file) => fs.existsSync(file));
|
|
355
427
|
if (found) files.set(path.join(target, src), { from: found });
|
package/lib/fonts.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Hangeul font holds some 11,000 syllables, 1 MB per weight; the site
|
|
3
|
+
* shows a few hundred. Cut down to the characters of the finished build, it
|
|
4
|
+
* takes a few dozen KB. Hangeul typed into the search that no page shows falls
|
|
5
|
+
* back to a system font.
|
|
6
|
+
*/
|
|
7
|
+
import fs from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import subsetFont from "subset-font";
|
|
10
|
+
|
|
11
|
+
const SUBSET = /^noto-serif-kr-.+\.woff2$/;
|
|
12
|
+
const TEXT = /\.(html|json)$/;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} outDir
|
|
16
|
+
* @returns {Promise<{ file: string, before: number, after: number }[]>}
|
|
17
|
+
*/
|
|
18
|
+
export async function subsetFonts(outDir) {
|
|
19
|
+
const characters = new Set();
|
|
20
|
+
for (const file of fs.readdirSync(outDir, { recursive: true })) {
|
|
21
|
+
if (!TEXT.test(String(file))) continue;
|
|
22
|
+
const content = fs.readFileSync(path.join(outDir, String(file)), "utf8");
|
|
23
|
+
for (const character of content) characters.add(character);
|
|
24
|
+
}
|
|
25
|
+
const text = [...characters].join("");
|
|
26
|
+
|
|
27
|
+
const fontDir = path.join(outDir, "fonts");
|
|
28
|
+
const results = [];
|
|
29
|
+
for (const file of fs.readdirSync(fontDir).filter((f) => SUBSET.test(f))) {
|
|
30
|
+
const font = fs.readFileSync(path.join(fontDir, file));
|
|
31
|
+
const subset = await subsetFont(font, text, { targetFormat: "woff2" });
|
|
32
|
+
fs.writeFileSync(path.join(fontDir, file), subset);
|
|
33
|
+
results.push({ after: subset.length, before: font.length, file });
|
|
34
|
+
}
|
|
35
|
+
return results;
|
|
36
|
+
}
|
package/lib/service-worker.js
CHANGED
|
@@ -1,98 +1,210 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Writes `sw.js` into a finished build. It makes the
|
|
3
|
-
* every page, script, stylesheet, font
|
|
4
|
-
* online-only.
|
|
2
|
+
* Writes `sw.js` into a finished build. It makes the site work offline in the
|
|
3
|
+
* visitor's language: every page, script, stylesheet, font, image and
|
|
4
|
+
* pronunciation. Only the linked videos stay online-only.
|
|
5
5
|
*
|
|
6
6
|
* The first install takes only the shell (CORE), so it is in control within
|
|
7
|
-
* seconds
|
|
8
|
-
* language
|
|
9
|
-
*
|
|
7
|
+
* seconds. The rest follows file by file in the background: the pages of the
|
|
8
|
+
* language the visitor reads, then what all languages share. It resumes on the
|
|
9
|
+
* next visit if it was cut off, and a visitor who asked the browser to save
|
|
10
|
+
* data is spared it (see `assets/js/pwa.js`). Pages visited meanwhile, in any
|
|
11
|
+
* language, are cached as they come.
|
|
10
12
|
*
|
|
11
13
|
* Offline-first: pages come from the cache even when online. A new deploy
|
|
12
|
-
* changes the file list and with it `sw.js`; the browser installs it in
|
|
13
|
-
*
|
|
14
|
+
* changes the file list and with it `sw.js`; the browser installs it in the
|
|
15
|
+
* background and the page offers to switch. The new version takes over the
|
|
16
|
+
* files that did not change, by their hash, and fetches only the others.
|
|
17
|
+
*
|
|
18
|
+
* Both downloads, the background one and an update's, report their progress
|
|
19
|
+
* to the open pages, and a page may cancel them (see `assets/js/pwa.js`);
|
|
20
|
+
* what was fetched stays.
|
|
14
21
|
*/
|
|
15
22
|
import crypto from "node:crypto";
|
|
16
23
|
import fs from "node:fs";
|
|
17
24
|
import path from "node:path";
|
|
18
25
|
|
|
19
|
-
const
|
|
20
|
-
/\.(html|js|css|woff2|jpe?g|png|svg|webp|avif|ico|webmanifest|json)$/i;
|
|
26
|
+
const CACHEABLE =
|
|
27
|
+
/\.(html|js|css|woff2|jpe?g|png|svg|webp|avif|ico|webmanifest|json|mp3|m4a|ogg|opus|wav)$/i;
|
|
21
28
|
const SKIP = new Set(["sw.js", "sw-reset.js"]);
|
|
29
|
+
/**
|
|
30
|
+
* Caches of versions with the download dialog. Earlier versions named theirs
|
|
31
|
+
* without the "2-"; their pages could not offer an update reliably, so an
|
|
32
|
+
* update from one of them takes over by itself.
|
|
33
|
+
*/
|
|
34
|
+
const CACHE_PREFIX = "taekwondo-db-2-";
|
|
35
|
+
/** Scripts none of the languages is written in; the browser fetches them only for such text. */
|
|
36
|
+
const ON_DEMAND = /^fonts\/ibm-plex-sans-(?!latin)/;
|
|
22
37
|
|
|
23
38
|
/** Styles, scripts, icons, the Latin fonts and the top-level pages. */
|
|
24
39
|
function isCore(relative) {
|
|
25
|
-
if (
|
|
26
|
-
return false;
|
|
40
|
+
if (/^(img|audio|search)\//.test(relative)) return false;
|
|
27
41
|
if (relative.startsWith("fonts/"))
|
|
28
42
|
return /^fonts\/(.+\.css|ibm-plex-sans-latin-)/.test(relative);
|
|
29
43
|
if (/\.(css|js|webmanifest|ico|png)$/.test(relative)) return true;
|
|
30
44
|
return /^([^/]+\/)?(index|404)\.html$/.test(relative);
|
|
31
45
|
}
|
|
32
46
|
|
|
47
|
+
/**
|
|
48
|
+
* The home of the language a file belongs to: its pages and its search index.
|
|
49
|
+
* Undefined for what all languages share.
|
|
50
|
+
*/
|
|
51
|
+
function languageOf(url, relative, languages) {
|
|
52
|
+
const search = /^search\/([^./]+)\./.exec(relative);
|
|
53
|
+
if (search) return languages.find(({ code }) => code === search[1])?.home;
|
|
54
|
+
if (!relative.endsWith(".html")) return undefined;
|
|
55
|
+
return languages
|
|
56
|
+
.map(({ home }) => home)
|
|
57
|
+
.filter((home) => url.startsWith(home))
|
|
58
|
+
.sort((a, b) => b.length - a.length)[0];
|
|
59
|
+
}
|
|
60
|
+
|
|
33
61
|
/**
|
|
34
62
|
* @param {string} outDir
|
|
35
|
-
* @param {
|
|
36
|
-
* @
|
|
63
|
+
* @param {object} [options]
|
|
64
|
+
* @param {string} [options.base] deploy base path, ending in `/`
|
|
65
|
+
* @param {{ code: string, home: string }[]} [options.languages] with their home paths
|
|
66
|
+
* @returns {number} number of files the service worker knows
|
|
37
67
|
*/
|
|
38
|
-
export function writeServiceWorker(
|
|
39
|
-
|
|
40
|
-
|
|
68
|
+
export function writeServiceWorker(
|
|
69
|
+
outDir,
|
|
70
|
+
{ base = "/", languages = [] } = {},
|
|
71
|
+
) {
|
|
72
|
+
const version = crypto.createHash("sha256");
|
|
73
|
+
/** @type {Record<string, string>} */
|
|
74
|
+
const files = {};
|
|
41
75
|
const core = [];
|
|
76
|
+
const shared = [];
|
|
77
|
+
/** @type {Record<string, string[]>} */
|
|
78
|
+
const byLanguage = Object.fromEntries(
|
|
79
|
+
languages.map(({ home }) => [home, []]),
|
|
80
|
+
);
|
|
42
81
|
|
|
43
82
|
for (const file of walk(outDir).sort()) {
|
|
44
83
|
const relative = path.relative(outDir, file).split(path.sep).join("/");
|
|
45
|
-
if (SKIP.has(relative) || !
|
|
46
|
-
hash
|
|
84
|
+
if (SKIP.has(relative) || !CACHEABLE.test(relative)) continue;
|
|
85
|
+
const hash = crypto
|
|
86
|
+
.createHash("sha256")
|
|
87
|
+
.update(fs.readFileSync(file))
|
|
88
|
+
.digest("hex")
|
|
89
|
+
.slice(0, 12);
|
|
90
|
+
version.update(relative).update(hash);
|
|
47
91
|
const url = base + relative.replace(/(^|\/)index\.html$/, "$1");
|
|
48
|
-
|
|
92
|
+
files[url] = hash;
|
|
49
93
|
if (isCore(relative)) core.push(url);
|
|
94
|
+
else if (!ON_DEMAND.test(relative))
|
|
95
|
+
(byLanguage[languageOf(url, relative, languages)] ?? shared).push(url);
|
|
50
96
|
}
|
|
51
97
|
|
|
52
|
-
const version = hash.digest("hex").slice(0, 16);
|
|
53
98
|
fs.writeFileSync(
|
|
54
99
|
path.join(outDir, "sw.js"),
|
|
55
|
-
`const CACHE = ${JSON.stringify(
|
|
100
|
+
`const CACHE = ${JSON.stringify(CACHE_PREFIX + version.digest("hex").slice(0, 16))};
|
|
101
|
+
const PREFIX = ${JSON.stringify(CACHE_PREFIX)};
|
|
56
102
|
const BASE = ${JSON.stringify(base)};
|
|
103
|
+
const FILES = ${JSON.stringify(files)};
|
|
57
104
|
const CORE = ${JSON.stringify(core)};
|
|
58
|
-
const
|
|
59
|
-
const
|
|
105
|
+
const SHARED = ${JSON.stringify(shared)};
|
|
106
|
+
const LANGUAGES = ${JSON.stringify(byLanguage)};
|
|
107
|
+
const HASH = "x-taekwondo-db-hash";
|
|
60
108
|
const PARALLEL = 8;
|
|
61
109
|
|
|
62
110
|
const request = (url) => new Request(url, { credentials: "include" });
|
|
111
|
+
const known = (url) => Object.hasOwn(FILES, url);
|
|
63
112
|
|
|
64
|
-
/**
|
|
65
|
-
|
|
66
|
-
const
|
|
113
|
+
/** Stored with the file's hash, by which the next version tells whether it changed. */
|
|
114
|
+
function put(cache, url, response) {
|
|
115
|
+
const headers = new Headers(response.headers);
|
|
116
|
+
headers.set(HASH, FILES[url]);
|
|
117
|
+
const { status, statusText } = response;
|
|
118
|
+
return cache.put(url, new Response(response.body, { headers, status, statusText }));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function uncached(cache, urls) {
|
|
67
122
|
const cached = new Set((await cache.keys()).map((r) => new URL(r.url).pathname));
|
|
68
|
-
|
|
123
|
+
return urls.filter((url) => !cached.has(url));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Fetches what the cache lacks, a few at a time; unless required, failures
|
|
128
|
+
* are left for the next run. Fetched files stay when the signal aborts.
|
|
129
|
+
*/
|
|
130
|
+
async function fill(cache, urls, { required = false, signal, fetched = () => {} } = {}) {
|
|
131
|
+
const queue = await uncached(cache, urls);
|
|
69
132
|
const worker = async () => {
|
|
70
133
|
for (let url = queue.shift(); url; url = queue.shift()) {
|
|
134
|
+
signal?.throwIfAborted();
|
|
71
135
|
try {
|
|
72
|
-
const response = await fetch(request(url));
|
|
73
|
-
if (response.ok)
|
|
74
|
-
|
|
136
|
+
const response = await fetch(request(url), { signal });
|
|
137
|
+
if (!response.ok) throw new Error(\`\${url}: \${response.status}\`);
|
|
138
|
+
await put(cache, url, response);
|
|
139
|
+
} catch (error) {
|
|
140
|
+
if (required || signal?.aborted) throw error;
|
|
141
|
+
}
|
|
142
|
+
fetched();
|
|
75
143
|
}
|
|
76
144
|
};
|
|
77
145
|
await Promise.all(Array.from({ length: PARALLEL }, worker));
|
|
78
146
|
}
|
|
79
147
|
|
|
80
|
-
/**
|
|
81
|
-
function
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
148
|
+
/** Tells the open pages how far a download has got, a few times a second. */
|
|
149
|
+
function reporter(kind, total) {
|
|
150
|
+
if (!total) return undefined;
|
|
151
|
+
let done = 0;
|
|
152
|
+
let sent = 0;
|
|
153
|
+
const send = async () => {
|
|
154
|
+
sent = Date.now();
|
|
155
|
+
for (const client of await self.clients.matchAll({ includeUncontrolled: true, type: "window" })) {
|
|
156
|
+
client.postMessage({ done, kind, total, type: "PROGRESS" });
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
send();
|
|
160
|
+
return () => {
|
|
161
|
+
done++;
|
|
162
|
+
if (done === total || Date.now() - sent > 250) send();
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Copies the unchanged files of earlier versions; returns those that changed. */
|
|
167
|
+
async function carryOver(cache) {
|
|
168
|
+
const changed = new Set();
|
|
169
|
+
for (const key of await caches.keys()) {
|
|
170
|
+
if (key === CACHE) continue;
|
|
171
|
+
const previous = await caches.open(key);
|
|
172
|
+
await Promise.all(
|
|
173
|
+
(await previous.keys()).map(async (entry) => {
|
|
174
|
+
const url = new URL(entry.url).pathname;
|
|
175
|
+
if (!known(url)) return;
|
|
176
|
+
const response = await previous.match(entry);
|
|
177
|
+
if (response?.headers.get(HASH) === FILES[url]) await cache.put(url, response);
|
|
178
|
+
else changed.add(url);
|
|
179
|
+
}),
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
return [...changed];
|
|
85
183
|
}
|
|
86
184
|
|
|
87
185
|
let filling;
|
|
186
|
+
/** The downloads a page may cancel: of an update, in the background. */
|
|
187
|
+
let installing;
|
|
188
|
+
let precaching;
|
|
88
189
|
|
|
89
190
|
self.addEventListener("install", (event) => {
|
|
191
|
+
installing = new AbortController();
|
|
192
|
+
const { signal } = installing;
|
|
90
193
|
event.waitUntil(
|
|
91
194
|
(async () => {
|
|
92
|
-
|
|
195
|
+
const cache = await caches.open(CACHE);
|
|
196
|
+
const changed = await carryOver(cache);
|
|
197
|
+
signal.throwIfAborted();
|
|
93
198
|
// An update is offered only once complete; the current version serves meanwhile.
|
|
94
|
-
|
|
95
|
-
|
|
199
|
+
const updating = Boolean(self.registration.active);
|
|
200
|
+
const fromLegacy =
|
|
201
|
+
updating && !(await caches.keys()).some((key) => key !== CACHE && key.startsWith(PREFIX));
|
|
202
|
+
const fetched = updating
|
|
203
|
+
? reporter("update", (await uncached(cache, [...new Set([...CORE, ...changed])])).length)
|
|
204
|
+
: undefined;
|
|
205
|
+
await fill(cache, CORE, { fetched, required: true, signal });
|
|
206
|
+
await fill(cache, changed, { fetched, signal });
|
|
207
|
+
if (!updating || fromLegacy) await self.skipWaiting();
|
|
96
208
|
})(),
|
|
97
209
|
);
|
|
98
210
|
});
|
|
@@ -108,10 +220,22 @@ self.addEventListener("activate", (event) => {
|
|
|
108
220
|
|
|
109
221
|
self.addEventListener("message", (event) => {
|
|
110
222
|
if (event.data?.type === "SKIP_WAITING") self.skipWaiting();
|
|
223
|
+
if (event.data?.type === "CANCEL") {
|
|
224
|
+
installing?.abort();
|
|
225
|
+
precaching?.abort();
|
|
226
|
+
}
|
|
111
227
|
if (event.data?.type === "PRECACHE") {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
228
|
+
const urls = [...(LANGUAGES[event.data.home] ?? []), ...SHARED];
|
|
229
|
+
filling ??= (async () => {
|
|
230
|
+
precaching = new AbortController();
|
|
231
|
+
const cache = await caches.open(CACHE);
|
|
232
|
+
const fetched = reporter("offline", (await uncached(cache, urls)).length);
|
|
233
|
+
await fill(cache, urls, { fetched, signal: precaching.signal });
|
|
234
|
+
})()
|
|
235
|
+
.catch(() => {})
|
|
236
|
+
.finally(() => {
|
|
237
|
+
filling = undefined;
|
|
238
|
+
});
|
|
115
239
|
event.waitUntil(filling);
|
|
116
240
|
}
|
|
117
241
|
});
|
|
@@ -132,12 +256,12 @@ async function respond(event) {
|
|
|
132
256
|
|
|
133
257
|
try {
|
|
134
258
|
const response = await fetch(request);
|
|
135
|
-
if (response.ok &&
|
|
136
|
-
event.waitUntil(
|
|
259
|
+
if (response.ok && known(url.pathname)) {
|
|
260
|
+
event.waitUntil(put(cache, url.pathname, response.clone()));
|
|
137
261
|
}
|
|
138
262
|
return response;
|
|
139
263
|
} catch (error) {
|
|
140
|
-
if (request.mode !== "navigate" ||
|
|
264
|
+
if (request.mode !== "navigate" || known(url.pathname)) throw error;
|
|
141
265
|
// "/de/…" gets the German 404 page; anything else the default language's.
|
|
142
266
|
const prefix = url.pathname.slice(BASE.length).split("/")[0];
|
|
143
267
|
const notFound =
|
|
@@ -156,7 +280,7 @@ self.addEventListener("fetch", (event) => {
|
|
|
156
280
|
`,
|
|
157
281
|
);
|
|
158
282
|
|
|
159
|
-
return
|
|
283
|
+
return Object.keys(files).length;
|
|
160
284
|
}
|
|
161
285
|
|
|
162
286
|
function walk(dir) {
|
package/lib/validate.js
CHANGED
|
@@ -12,8 +12,9 @@
|
|
|
12
12
|
* unresolvable sources
|
|
13
13
|
* 5. site invariants: every group has a term, every technique a translation in
|
|
14
14
|
* every configured language but Korean
|
|
15
|
-
* 6. only warnings: images that are no existing file (the
|
|
16
|
-
* out; the public database ships without images), and
|
|
15
|
+
* 6. only warnings: images and pronunciations that are no existing file (the
|
|
16
|
+
* build leaves them out; the public database ships without images), and
|
|
17
|
+
* `#[...]` references
|
|
17
18
|
* in descriptions to what is not loaded — with `include` a builtin
|
|
18
19
|
* description may refer to a technique the site leaves out, which is then
|
|
19
20
|
* shown as plain text
|
|
@@ -55,6 +56,7 @@ function jsonFiles(dir) {
|
|
|
55
56
|
* @param {string} options.dataDir directory with the JSON data, relative to `root`
|
|
56
57
|
* @param {Record<string, any>} options.db
|
|
57
58
|
* @param {string[]} options.imageDirs absolute
|
|
59
|
+
* @param {string[]} options.audioDirs absolute
|
|
58
60
|
* @param {string[]} options.languages languages of the Hugo site
|
|
59
61
|
* @returns {Promise<boolean>} whether everything is valid
|
|
60
62
|
*/
|
|
@@ -63,6 +65,7 @@ export async function validateSite({
|
|
|
63
65
|
dataDir,
|
|
64
66
|
db,
|
|
65
67
|
imageDirs,
|
|
68
|
+
audioDirs,
|
|
66
69
|
languages,
|
|
67
70
|
}) {
|
|
68
71
|
let failed = false;
|
|
@@ -145,6 +148,15 @@ export async function validateSite({
|
|
|
145
148
|
}
|
|
146
149
|
}
|
|
147
150
|
}
|
|
151
|
+
for (const [name, entry] of [
|
|
152
|
+
...database.getAllTechniques(),
|
|
153
|
+
...database.getAllTerms(),
|
|
154
|
+
]) {
|
|
155
|
+
const src = entry.pronunciation?.src;
|
|
156
|
+
if (src && !audioDirs.some((dir) => fs.existsSync(path.join(dir, src)))) {
|
|
157
|
+
console.warn(`! ${name}: pronunciation not found, left out: ${src}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
148
160
|
|
|
149
161
|
// 6. description references
|
|
150
162
|
const describedEntries = [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taekwondo-db/web-app",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"description": "Hugo theme for the Taekwondo technique database — statically rendered, usable as a base for derived, customized sites.",
|
|
@@ -23,9 +23,10 @@
|
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@fontsource/ibm-plex-sans": "^5.3.0",
|
|
25
25
|
"@fontsource/noto-serif-kr": "^5.3.1",
|
|
26
|
-
"@taekwondo-db/database": "^0.
|
|
26
|
+
"@taekwondo-db/database": "^0.5.0",
|
|
27
27
|
"ajv": "^8.20.0",
|
|
28
|
-
"bootstrap": "^5.3.8"
|
|
28
|
+
"bootstrap": "^5.3.8",
|
|
29
|
+
"subset-font": "^2.9.0"
|
|
29
30
|
},
|
|
30
31
|
"engines": { "node": ">=22.15" },
|
|
31
32
|
"files": [
|