@wakeful-cloud/slidev-theme-illinois 0.0.0-stage → 0.1.1
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/LICENSE +45 -0
- package/README.md +592 -2
- package/assets/logos/block-i-blue.svg +54 -0
- package/assets/logos/block-i-full.svg +54 -0
- package/assets/logos/block-i-orange.svg +54 -0
- package/assets/logos/wordmark-blue.svg +94 -0
- package/assets/logos/wordmark-formal-blue.svg +234 -0
- package/assets/logos/wordmark-formal-full.svg +234 -0
- package/assets/logos/wordmark-formal-orange.svg +234 -0
- package/assets/logos/wordmark-full.svg +94 -0
- package/assets/logos/wordmark-orange.svg +94 -0
- package/assets/logos/wordmark-vertical-blue.svg +94 -0
- package/assets/logos/wordmark-vertical-full.svg +94 -0
- package/assets/logos/wordmark-vertical-orange.svg +94 -0
- package/citations/engine.ts +738 -0
- package/citations/errors.ts +79 -0
- package/citations/plugin.ts +137 -0
- package/citations/styles.ts +300 -0
- package/citations/syntax.ts +357 -0
- package/components/bibliography.vue +185 -0
- package/components/callout.vue +42 -0
- package/components/card.vue +37 -0
- package/components/cite.vue +62 -0
- package/components/frame.vue +71 -0
- package/components/illinois-logo.vue +45 -0
- package/components/math-morph.vue +252 -0
- package/components/qr-code.vue +51 -0
- package/components/stat.vue +43 -0
- package/components/steps.vue +77 -0
- package/global.d.ts +14 -0
- package/internal/brand-logo.vue +48 -0
- package/internal/citations.ts +206 -0
- package/internal/kicker.vue +13 -0
- package/internal/layout-root.vue +42 -0
- package/internal/pattern.vue +20 -0
- package/internal/split.vue +147 -0
- package/internal/theme.ts +878 -0
- package/layouts/agenda.vue +82 -0
- package/layouts/appendix-right.vue +15 -0
- package/layouts/appendix.vue +25 -0
- package/layouts/canvas.vue +21 -0
- package/layouts/center.vue +17 -0
- package/layouts/compare.vue +48 -0
- package/layouts/cover-right.vue +15 -0
- package/layouts/cover.vue +170 -0
- package/layouts/default.vue +15 -0
- package/layouts/end-right.vue +15 -0
- package/layouts/end.vue +89 -0
- package/layouts/fact-right.vue +15 -0
- package/layouts/fact.vue +25 -0
- package/layouts/figure.vue +43 -0
- package/layouts/grid.vue +33 -0
- package/layouts/iframe-left.vue +16 -0
- package/layouts/iframe-right.vue +16 -0
- package/layouts/image-left.vue +16 -0
- package/layouts/image-right.vue +16 -0
- package/layouts/image.vue +40 -0
- package/layouts/media-left.vue +17 -0
- package/layouts/media-right.vue +17 -0
- package/layouts/people.vue +98 -0
- package/layouts/quote-right.vue +15 -0
- package/layouts/quote.vue +41 -0
- package/layouts/references.vue +36 -0
- package/layouts/section-right.vue +15 -0
- package/layouts/section.vue +53 -0
- package/layouts/statement-right.vue +15 -0
- package/layouts/statement.vue +30 -0
- package/layouts/two-cols-header.vue +32 -0
- package/layouts/two-cols.vue +31 -0
- package/package.json +114 -4
- package/setup/mermaid.ts +73 -0
- package/setup/shiki.ts +147 -0
- package/setup/transformers.ts +21 -0
- package/setup/unocss.ts +34 -0
- package/setup/vite-plugins.ts +8 -0
- package/slide-top.vue +149 -0
- package/styles/base.css +309 -0
- package/styles/components.css +336 -0
- package/styles/index.ts +19 -0
- package/styles/layouts.css +1242 -0
- package/styles/looks.css +307 -0
- package/styles/tokens.css +324 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Errors of the citation pipeline. Problems are shown on the slides, and decks are often
|
|
3
|
+
* published, so a slide may only show what the deck already contains (configured names and public
|
|
4
|
+
* URLs). Local paths and file contents stay in the `cause`, which is only logged in the terminal.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Error thrown by Node's file system functions
|
|
9
|
+
*/
|
|
10
|
+
interface FileSystemError {
|
|
11
|
+
/**
|
|
12
|
+
* Error code such as `ENOENT`
|
|
13
|
+
*/
|
|
14
|
+
code?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Suffix of slide messages whose details are only logged
|
|
19
|
+
*/
|
|
20
|
+
const SEE_TERMINAL = "(see the Slidev terminal for details)";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* An error whose message is safe to show on the slides
|
|
24
|
+
*/
|
|
25
|
+
export class CitationError extends Error {
|
|
26
|
+
/**
|
|
27
|
+
* Whether the slides should point to the terminal for the underlying error
|
|
28
|
+
*/
|
|
29
|
+
public readonly detailed: boolean;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Create an error
|
|
33
|
+
* @param message Message shown on the slides (no local paths or file contents)
|
|
34
|
+
* @param cause Underlying error, logged in the terminal only
|
|
35
|
+
* @param detailed Whether the slides should point to the terminal for the underlying error
|
|
36
|
+
*/
|
|
37
|
+
public constructor(message: string, cause?: unknown, detailed = false) {
|
|
38
|
+
super(message, {cause});
|
|
39
|
+
this.detailed = detailed;
|
|
40
|
+
this.name = "CitationError";
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Whether a thrown value is a "file not found" error
|
|
46
|
+
* @param error Thrown value
|
|
47
|
+
* @returns Whether the file does not exist
|
|
48
|
+
*/
|
|
49
|
+
export const isNotFound = (error: unknown) => (error as FileSystemError | null)?.code === "ENOENT";
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Message of a thrown value
|
|
53
|
+
* @param error Thrown value
|
|
54
|
+
* @returns Message
|
|
55
|
+
*/
|
|
56
|
+
const messageOf = (error: unknown) => (error instanceof Error ? error.message : String(error));
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Message shown on the slides for a thrown value. Anything but a `CitationError` may name local
|
|
60
|
+
* paths or quote file contents, so it is replaced with a generic message.
|
|
61
|
+
* @param error Thrown value
|
|
62
|
+
* @returns Message without local paths or file contents
|
|
63
|
+
*/
|
|
64
|
+
export const slideMessage = (error: unknown) => {
|
|
65
|
+
if (!(error instanceof CitationError)) {
|
|
66
|
+
return `Loading citations failed ${SEE_TERMINAL}`;
|
|
67
|
+
}
|
|
68
|
+
return error.detailed ? `${error.message} ${SEE_TERMINAL}` : error.message;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Message logged in the terminal for a thrown value, including the underlying error
|
|
73
|
+
* @param error Thrown value
|
|
74
|
+
* @returns Detailed message
|
|
75
|
+
*/
|
|
76
|
+
export const terminalMessage = (error: unknown) => {
|
|
77
|
+
const cause = error instanceof Error ? error.cause : undefined;
|
|
78
|
+
return cause === undefined ? messageOf(error) : `${messageOf(error)}: ${messageOf(cause)}`;
|
|
79
|
+
};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Vite plugin exposing the deck's formatted citations as `virtual:illinois-citations`. The
|
|
3
|
+
* module is recomputed whenever a markdown or bibliography file changes, so citation numbers stay
|
|
4
|
+
* correct across the whole deck during development.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import type {ResolvedSlidevOptions} from "@slidev/types";
|
|
9
|
+
import type {Plugin} from "vite";
|
|
10
|
+
import {bibliographyPaths, buildCitations} from "./engine.ts";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Citation-related `themeConfig` fields
|
|
14
|
+
*/
|
|
15
|
+
interface CitationThemeConfig {
|
|
16
|
+
/**
|
|
17
|
+
* Bibliography file(s)
|
|
18
|
+
*/
|
|
19
|
+
bibliography?: string | string[];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Citation locale
|
|
23
|
+
*/
|
|
24
|
+
citationLang?: string;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Citation style name or `.csl` path
|
|
28
|
+
*/
|
|
29
|
+
citationStyle?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Resolved (internal) ID of the virtual module
|
|
34
|
+
*/
|
|
35
|
+
const RESOLVED_ID = "\0virtual:illinois-citations";
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Public ID of the virtual module
|
|
39
|
+
*/
|
|
40
|
+
const VIRTUAL_ID = "virtual:illinois-citations";
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Create the citations plugin
|
|
44
|
+
* @param options Resolved Slidev options
|
|
45
|
+
* @returns Vite plugin
|
|
46
|
+
*/
|
|
47
|
+
export const createCitationsPlugin = (options: ResolvedSlidevOptions): Plugin => {
|
|
48
|
+
// Relative paths in the headmatter are relative to the slides file
|
|
49
|
+
const root = path.dirname(path.resolve(options.entry));
|
|
50
|
+
|
|
51
|
+
// Downloaded CSL styles and locales are cached next to the deck's dependencies
|
|
52
|
+
const cacheDir = path.join(options.userRoot, "node_modules", ".cache", "slidev-theme-illinois");
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Current citation settings (the headmatter can change while the dev server runs)
|
|
56
|
+
* @returns Citation settings
|
|
57
|
+
*/
|
|
58
|
+
const themeConfig = () => (options.data.config.themeConfig ?? {}) as CitationThemeConfig;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Compute the virtual module's JSON
|
|
62
|
+
* @returns Serialized citation data
|
|
63
|
+
*/
|
|
64
|
+
const compute = async () => {
|
|
65
|
+
const config = themeConfig();
|
|
66
|
+
const data = await buildCitations({
|
|
67
|
+
bibliography: config.bibliography,
|
|
68
|
+
cacheDir,
|
|
69
|
+
lang: config.citationLang,
|
|
70
|
+
root,
|
|
71
|
+
slides: options.data.slides.map(({content, frontmatter}) => ({content, frontmatter})),
|
|
72
|
+
style: config.citationStyle,
|
|
73
|
+
});
|
|
74
|
+
return JSON.stringify(data);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Files the citations depend on: bibliographies and a local `.csl` style
|
|
79
|
+
* @returns Absolute paths
|
|
80
|
+
*/
|
|
81
|
+
const watchedFiles = () => {
|
|
82
|
+
const config = themeConfig();
|
|
83
|
+
const style = config.citationStyle?.trim();
|
|
84
|
+
const files = bibliographyPaths(config.bibliography, root);
|
|
85
|
+
return style?.endsWith(".csl") ? [...files, path.resolve(root, style)] : files;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// Last served JSON (used to skip no-op updates)
|
|
89
|
+
let last = "";
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
// Run after Slidev's own hot-update handler so `options.data` already holds the new slides
|
|
93
|
+
enforce: "post",
|
|
94
|
+
|
|
95
|
+
async handleHotUpdate(ctx) {
|
|
96
|
+
// Vite reports paths with forward slashes; resolve them like the watched paths (Windows)
|
|
97
|
+
if (!watchedFiles().includes(path.resolve(ctx.file)) && !ctx.file.endsWith(".md")) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const next = await compute();
|
|
102
|
+
if (next === last) {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
last = next;
|
|
106
|
+
|
|
107
|
+
const mod = ctx.server.moduleGraph.getModuleById(RESOLVED_ID);
|
|
108
|
+
if (mod === undefined) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
ctx.server.moduleGraph.invalidateModule(mod);
|
|
112
|
+
return [...ctx.modules, mod];
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
async load(id) {
|
|
116
|
+
if (id !== RESOLVED_ID) {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
for (const file of watchedFiles()) {
|
|
121
|
+
this.addWatchFile(file);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
last = await compute();
|
|
125
|
+
|
|
126
|
+
// Not as an object literal: there, a `__proto__` key would set the object's prototype
|
|
127
|
+
// instead of adding an entry
|
|
128
|
+
return `export default JSON.parse(${JSON.stringify(last)})`;
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
name: "slidev-theme-illinois:citations",
|
|
132
|
+
|
|
133
|
+
resolveId(id) {
|
|
134
|
+
return id === VIRTUAL_ID ? RESOLVED_ID : undefined;
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
};
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Resolves CSL styles and locales. Styles and locales that aren't local files are downloaded
|
|
3
|
+
* from the official Citation Style Language repositories (at a pinned commit, so builds are
|
|
4
|
+
* reproducible) on first use, checked and cached on disk.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {createHash} from "node:crypto";
|
|
8
|
+
import {mkdir, readFile, rename, rm, writeFile} from "node:fs/promises";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import {plugins} from "@citation-js/core";
|
|
11
|
+
import "@citation-js/plugin-csl";
|
|
12
|
+
import {CitationError, isNotFound} from "./errors.ts";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Pinned commit of https://github.com/citation-style-language/locales (update deliberately; the
|
|
16
|
+
* commit is part of the cache path, so updating it re-downloads)
|
|
17
|
+
*/
|
|
18
|
+
const LOCALES_COMMIT = "14940e56f7c783e4173cafbd92e0f5856f2caa14";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Pinned commit of https://github.com/citation-style-language/styles (see `LOCALES_COMMIT`)
|
|
22
|
+
*/
|
|
23
|
+
const STYLES_COMMIT = "eaff8c34fe7e4e9b10c583e53148a26895fdbbb5";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Raw file base URL of the locales repository
|
|
27
|
+
*/
|
|
28
|
+
const LOCALES_URL = `https://raw.githubusercontent.com/citation-style-language/locales/${LOCALES_COMMIT}`;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Raw file base URL of the styles repository
|
|
32
|
+
*/
|
|
33
|
+
const STYLES_URL = `https://raw.githubusercontent.com/citation-style-language/styles/${STYLES_COMMIT}`;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Download timeout
|
|
37
|
+
*/
|
|
38
|
+
const TIMEOUT_MS = 20_000;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Short names for common styles (values are CSL repository file names without `.csl`)
|
|
42
|
+
*/
|
|
43
|
+
const STYLE_ALIASES: Record<string, string> = {
|
|
44
|
+
acm: "association-for-computing-machinery",
|
|
45
|
+
chicago: "chicago-author-date",
|
|
46
|
+
harvard: "harvard-cite-them-right",
|
|
47
|
+
mla: "modern-language-association",
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Valid CSL locale codes (also prevents path traversal in the cache)
|
|
52
|
+
*/
|
|
53
|
+
const LOCALE_RE = /^[a-z]{2,3}(?:-[A-Z]{2})?$/u;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Valid CSL repository style IDs (also prevents path traversal in the cache)
|
|
57
|
+
*/
|
|
58
|
+
const STYLE_ID_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/u;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Parent link of a dependent style
|
|
62
|
+
*/
|
|
63
|
+
const PARENT_RE =
|
|
64
|
+
/<link\b(?=[^>]*\brel="independent-parent")[^>]*\bhref="[^"]*\/styles\/([^"/]+)"/u;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A citation-js registry of named XML documents (only the parts used here)
|
|
68
|
+
*/
|
|
69
|
+
interface CslRegistry {
|
|
70
|
+
/**
|
|
71
|
+
* Register (or replace) a document
|
|
72
|
+
*/
|
|
73
|
+
add: (name: string, xml: string) => void;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Whether a document is registered
|
|
77
|
+
*/
|
|
78
|
+
has: (name: string) => boolean;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The citation-js CSL plugin configuration (only the parts used here)
|
|
83
|
+
*/
|
|
84
|
+
interface CslConfig {
|
|
85
|
+
/**
|
|
86
|
+
* Registered locales
|
|
87
|
+
*/
|
|
88
|
+
locales: CslRegistry;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Registered styles
|
|
92
|
+
*/
|
|
93
|
+
styles: CslRegistry;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The citation-js CSL plugin configuration
|
|
98
|
+
*/
|
|
99
|
+
const csl = plugins.config.get("@csl") as CslConfig;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* A resolved style
|
|
103
|
+
*/
|
|
104
|
+
export interface ResolvedStyle {
|
|
105
|
+
/**
|
|
106
|
+
* Template name registered with citation-js
|
|
107
|
+
*/
|
|
108
|
+
name: string;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Style XML
|
|
112
|
+
*/
|
|
113
|
+
xml: string;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* In-flight and completed downloads, keyed by cache path (avoids duplicate concurrent requests)
|
|
118
|
+
*/
|
|
119
|
+
const downloads = new Map<string, Promise<string | undefined>>();
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Whether a downloaded or cached file is a complete CSL document of the given kind (rejects error
|
|
123
|
+
* pages, captive portals and truncated files)
|
|
124
|
+
* @param text File contents
|
|
125
|
+
* @param root Root element (`locale` or `style`)
|
|
126
|
+
* @returns Whether the document looks complete
|
|
127
|
+
*/
|
|
128
|
+
const isCslDocument = (text: string, root: "locale" | "style") =>
|
|
129
|
+
new RegExp(`<${root}\\b`, "u").test(text) && text.trimEnd().endsWith(`</${root}>`);
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Read a cached file, or download it from the first URL that exists and cache it
|
|
133
|
+
* @param cacheFile Absolute cache file path
|
|
134
|
+
* @param urls Candidate URLs, tried in order (a 404 moves on to the next one)
|
|
135
|
+
* @param root Root element the document must have
|
|
136
|
+
* @returns File contents, or `undefined` if no URL exists
|
|
137
|
+
*/
|
|
138
|
+
const fetchCached = (
|
|
139
|
+
cacheFile: string,
|
|
140
|
+
urls: string[],
|
|
141
|
+
root: "locale" | "style",
|
|
142
|
+
): Promise<string | undefined> => {
|
|
143
|
+
const pending = downloads.get(cacheFile);
|
|
144
|
+
if (pending !== undefined) {
|
|
145
|
+
return pending;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const task = (async () => {
|
|
149
|
+
// Cache hit (a corrupt cache file is downloaded again)
|
|
150
|
+
const cached = await readFile(cacheFile, "utf8").catch(() => undefined);
|
|
151
|
+
if (cached !== undefined && isCslDocument(cached, root)) {
|
|
152
|
+
return cached;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Download
|
|
156
|
+
for (const url of urls) {
|
|
157
|
+
// oxlint-disable-next-line no-await-in-loop -- candidates are tried in order
|
|
158
|
+
const response = await fetch(url, {signal: AbortSignal.timeout(TIMEOUT_MS)}).catch(
|
|
159
|
+
(error: unknown) => {
|
|
160
|
+
throw new CitationError(`Downloading ${url} failed (offline?)`, error);
|
|
161
|
+
},
|
|
162
|
+
);
|
|
163
|
+
if (response.status === 404) {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (!response.ok) {
|
|
167
|
+
throw new CitationError(`Downloading ${url} failed: HTTP ${response.status}`);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// oxlint-disable-next-line no-await-in-loop -- returns on the first hit
|
|
171
|
+
const text = await response.text();
|
|
172
|
+
if (!isCslDocument(text, root)) {
|
|
173
|
+
throw new CitationError(`Downloading ${url} failed: the response is not a CSL ${root}`);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Write atomically, so an interrupted write or a concurrent build never leaves a partial file
|
|
177
|
+
const temporary = `${cacheFile}.${process.pid}.tmp`;
|
|
178
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
179
|
+
await mkdir(path.dirname(cacheFile), {recursive: true});
|
|
180
|
+
try {
|
|
181
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
182
|
+
await writeFile(temporary, text);
|
|
183
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
184
|
+
await rename(temporary, cacheFile);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
187
|
+
await rm(temporary, {force: true});
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
return text;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return undefined;
|
|
194
|
+
})();
|
|
195
|
+
|
|
196
|
+
// Forget failed downloads so they are retried next time
|
|
197
|
+
task.catch(() => downloads.delete(cacheFile));
|
|
198
|
+
downloads.set(cacheFile, task);
|
|
199
|
+
return task;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Resolve a style name or `.csl` path, registering it with citation-js
|
|
204
|
+
* @param style Style name (`ieee`, `apa`, `acm`, any CSL repository style ID) or path to a `.csl`
|
|
205
|
+
* file
|
|
206
|
+
* @param root Directory relative `.csl` paths are resolved against
|
|
207
|
+
* @param cacheDir Directory downloaded files are cached in
|
|
208
|
+
* @returns Resolved style
|
|
209
|
+
*/
|
|
210
|
+
export const resolveStyle = async (
|
|
211
|
+
style: string,
|
|
212
|
+
root: string,
|
|
213
|
+
cacheDir: string,
|
|
214
|
+
): Promise<ResolvedStyle> => {
|
|
215
|
+
const id = style.trim();
|
|
216
|
+
|
|
217
|
+
// Local file
|
|
218
|
+
if (id.endsWith(".csl")) {
|
|
219
|
+
// Failures are reported under the configured name (the underlying error names the absolute
|
|
220
|
+
// path)
|
|
221
|
+
const xml = await readFile(path.resolve(root, id), "utf8").catch((error: unknown) => {
|
|
222
|
+
const missing = isNotFound(error);
|
|
223
|
+
const problem = missing ? "was not found" : "could not be read";
|
|
224
|
+
throw new CitationError(`Citation style file "${id}" ${problem}`, error, !missing);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// citation-js caches engines by template name: a new name per content picks up edits
|
|
228
|
+
const hash = createHash("sha256").update(xml).digest("hex").slice(0, 16);
|
|
229
|
+
const name = `illinois-local-${hash}`;
|
|
230
|
+
if (!csl.styles.has(name)) {
|
|
231
|
+
csl.styles.add(name, xml);
|
|
232
|
+
}
|
|
233
|
+
return {name, xml};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// CSL repository style (independent styles live at the root, dependent ones in `dependent/`)
|
|
237
|
+
const styleId = STYLE_ALIASES[id.toLowerCase()] ?? id.toLowerCase();
|
|
238
|
+
if (!STYLE_ID_RE.test(styleId)) {
|
|
239
|
+
throw new CitationError(`Invalid citation style "${style}"`);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const stylesDir = path.join(cacheDir, "styles", STYLES_COMMIT);
|
|
243
|
+
let xml = await fetchCached(
|
|
244
|
+
path.join(stylesDir, `${styleId}.csl`),
|
|
245
|
+
[`${STYLES_URL}/${styleId}.csl`, `${STYLES_URL}/dependent/${styleId}.csl`],
|
|
246
|
+
"style",
|
|
247
|
+
);
|
|
248
|
+
if (xml === undefined) {
|
|
249
|
+
throw new CitationError(
|
|
250
|
+
`Citation style "${style}" was not found in https://github.com/citation-style-language/styles`,
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Dependent styles only reference their parent; use the parent's formatting rules
|
|
255
|
+
const parent = PARENT_RE.exec(xml)?.[1];
|
|
256
|
+
if (parent !== undefined && STYLE_ID_RE.test(parent)) {
|
|
257
|
+
xml = await fetchCached(
|
|
258
|
+
path.join(stylesDir, `${parent}.csl`),
|
|
259
|
+
[`${STYLES_URL}/${parent}.csl`],
|
|
260
|
+
"style",
|
|
261
|
+
);
|
|
262
|
+
if (xml === undefined) {
|
|
263
|
+
throw new CitationError(
|
|
264
|
+
`Parent style "${parent}" of citation style "${style}" was not found`,
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const name = `illinois-${styleId}`;
|
|
270
|
+
if (!csl.styles.has(name)) {
|
|
271
|
+
csl.styles.add(name, xml);
|
|
272
|
+
}
|
|
273
|
+
return {name, xml};
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Make sure a CSL locale is registered with citation-js (`en-US` is built in)
|
|
278
|
+
* @param lang Locale code such as `de-DE`
|
|
279
|
+
* @param cacheDir Directory downloaded files are cached in
|
|
280
|
+
*/
|
|
281
|
+
export const ensureLocale = async (lang: string, cacheDir: string): Promise<void> => {
|
|
282
|
+
if (csl.locales.has(lang)) {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
if (!LOCALE_RE.test(lang)) {
|
|
286
|
+
throw new CitationError(`Invalid citation locale "${lang}"`);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const xml = await fetchCached(
|
|
290
|
+
path.join(cacheDir, "locales", LOCALES_COMMIT, `${lang}.xml`),
|
|
291
|
+
[`${LOCALES_URL}/locales-${lang}.xml`],
|
|
292
|
+
"locale",
|
|
293
|
+
);
|
|
294
|
+
if (xml === undefined) {
|
|
295
|
+
const hint = lang.includes("-") ? "" : ' (use a regional code such as "de-DE")';
|
|
296
|
+
throw new CitationError(`Citation locale "${lang}" was not found${hint}`);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
csl.locales.add(lang, xml);
|
|
300
|
+
};
|