@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,738 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Node-side citation engine: loads bibliography files, formats entries with citeproc (via
|
|
3
|
+
* citation-js) and computes deck-wide citation numbering.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {readFile, stat} from "node:fs/promises";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import {Cite} from "@citation-js/core";
|
|
9
|
+
import "@citation-js/plugin-bibtex";
|
|
10
|
+
import "@citation-js/plugin-csl";
|
|
11
|
+
import {CitationError, isNotFound, slideMessage, terminalMessage} from "./errors.ts";
|
|
12
|
+
import {ensureLocale, type ResolvedStyle, resolveStyle} from "./styles.ts";
|
|
13
|
+
import {citedKeys, type ListedKeys, listedKeys} from "./syntax.ts";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A formatted bibliography entry
|
|
17
|
+
*/
|
|
18
|
+
export interface CitationEntry {
|
|
19
|
+
/**
|
|
20
|
+
* Whether the entry is cited anywhere in the deck
|
|
21
|
+
*/
|
|
22
|
+
cited: boolean;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Formatted reference (HTML)
|
|
26
|
+
*/
|
|
27
|
+
html: string;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Short in-text label for author-date styles (e.g. "Smith et al., 2024")
|
|
31
|
+
*/
|
|
32
|
+
label: string;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 1-based position in the bibliography (the citation number for numeric styles)
|
|
36
|
+
*/
|
|
37
|
+
n: number;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Formatted reference (plain text)
|
|
41
|
+
*/
|
|
42
|
+
text: string;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Issued year (used by `[-@key]`)
|
|
46
|
+
*/
|
|
47
|
+
year: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Everything the client needs to render citations
|
|
52
|
+
*/
|
|
53
|
+
export interface CitationData {
|
|
54
|
+
/**
|
|
55
|
+
* Formatted entries by key: the cited ones, and uncited ones a reference list asks for
|
|
56
|
+
*/
|
|
57
|
+
entries: Record<string, CitationEntry>;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Configuration, download or parsing error (without local paths or file contents)
|
|
61
|
+
*/
|
|
62
|
+
error?: string;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* How multiple numeric citations are grouped: IEEE uses `[1], [2]`, most others `[1, 2]`
|
|
66
|
+
*/
|
|
67
|
+
grouping: "combined" | "separate";
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Whether the style is numeric (`[1]`) or author-date (`(Smith, 2024)`)
|
|
71
|
+
*/
|
|
72
|
+
numeric: boolean;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Keys of the formatted entries in bibliography order (cited entries first for numeric styles)
|
|
76
|
+
*/
|
|
77
|
+
order: string[];
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Cited keys per slide number
|
|
81
|
+
*/
|
|
82
|
+
slides: Record<number, string[]>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* A slide of the deck
|
|
87
|
+
*/
|
|
88
|
+
export interface SlideSource {
|
|
89
|
+
/**
|
|
90
|
+
* Slide markdown
|
|
91
|
+
*/
|
|
92
|
+
content: string;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Slide frontmatter
|
|
96
|
+
*/
|
|
97
|
+
frontmatter: Record<string, unknown>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Options for {@link buildCitations}
|
|
102
|
+
*/
|
|
103
|
+
interface CitationOptions {
|
|
104
|
+
/**
|
|
105
|
+
* Bibliography file(s), relative to `root`
|
|
106
|
+
*/
|
|
107
|
+
bibliography: string | string[] | undefined;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Directory downloaded styles and locales are cached in
|
|
111
|
+
*/
|
|
112
|
+
cacheDir: string;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Locale (default `en-US`)
|
|
116
|
+
*/
|
|
117
|
+
lang: string | undefined;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Directory relative paths are resolved against
|
|
121
|
+
*/
|
|
122
|
+
root: string;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Slides, indexed by slide number - 1
|
|
126
|
+
*/
|
|
127
|
+
slides: SlideSource[];
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Style name or path to a `.csl` file (default `ieee`)
|
|
131
|
+
*/
|
|
132
|
+
style: string | undefined;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* A parsed bibliography file
|
|
137
|
+
*/
|
|
138
|
+
interface ParsedFile {
|
|
139
|
+
/**
|
|
140
|
+
* CSL-JSON items
|
|
141
|
+
*/
|
|
142
|
+
data: Record<string, unknown>[];
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* File modification time (ms) when parsed
|
|
146
|
+
*/
|
|
147
|
+
mtime: number;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Last formatted result (formatting is skipped when nothing relevant changed)
|
|
152
|
+
*/
|
|
153
|
+
interface FormatCache {
|
|
154
|
+
/**
|
|
155
|
+
* Formatted citation data
|
|
156
|
+
*/
|
|
157
|
+
data: CitationData;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Inputs the data was computed from (serialized)
|
|
161
|
+
*/
|
|
162
|
+
key: string;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* CSL-JSON date
|
|
167
|
+
*/
|
|
168
|
+
interface CslDate {
|
|
169
|
+
/**
|
|
170
|
+
* Date parts (`[[year, month, day]]`)
|
|
171
|
+
*/
|
|
172
|
+
"date-parts"?: unknown[][];
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Free-form date
|
|
176
|
+
*/
|
|
177
|
+
literal?: string;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* citation-js input options
|
|
182
|
+
*/
|
|
183
|
+
type InputOptions = NonNullable<ConstructorParameters<typeof Cite>[1]>;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Tags of citeproc's HTML output: its text decorations and display blocks, exactly as it writes
|
|
187
|
+
* them (no other attributes)
|
|
188
|
+
*/
|
|
189
|
+
const CITEPROC_TAG_RE =
|
|
190
|
+
/<\/?(?:b|div|em|i|span|sub|sup)>|<div class="csl-(?:block|entry|indent|left-margin|right-inline)">|<span style="(?:baseline|font-style:normal;|font-variant:normal;|font-variant:small-caps;|font-weight:normal;|text-decoration:none;|text-decoration:underline;)">/gu;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* HTML entities produced by citeproc
|
|
194
|
+
*/
|
|
195
|
+
const ENTITY_RE = /&(#x?[0-9a-f]+|amp|apos|gt|lt|nbsp|quot);/giu;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Characters of the named entities in `ENTITY_RE`
|
|
199
|
+
*/
|
|
200
|
+
const NAMED_ENTITIES: Record<string, string> = {
|
|
201
|
+
amp: "&",
|
|
202
|
+
apos: "'",
|
|
203
|
+
gt: ">",
|
|
204
|
+
lt: "<",
|
|
205
|
+
nbsp: " ",
|
|
206
|
+
quot: '"',
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Parsed bibliography files by absolute path
|
|
211
|
+
*/
|
|
212
|
+
const fileCache = new Map<string, ParsedFile>();
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Last formatted result
|
|
216
|
+
*/
|
|
217
|
+
let formatCache: FormatCache | undefined = undefined;
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Log a warning to the Slidev terminal
|
|
221
|
+
* @param message Message
|
|
222
|
+
*/
|
|
223
|
+
const warn = (message: string) => {
|
|
224
|
+
// oxlint-disable-next-line no-console -- surfaced in the Slidev terminal
|
|
225
|
+
console.warn(`[slidev-theme-illinois] ${message}`);
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Absolute paths of the configured bibliography files
|
|
230
|
+
* @param bibliography Configured path(s)
|
|
231
|
+
* @param root Directory relative paths are resolved against
|
|
232
|
+
* @returns Absolute paths
|
|
233
|
+
*/
|
|
234
|
+
export const bibliographyPaths = (bibliography: string | string[] | undefined, root: string) =>
|
|
235
|
+
[bibliography ?? []].flat().map(file => path.resolve(root, file));
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Read and parse (or reuse) a bibliography file
|
|
239
|
+
* @param file Absolute path
|
|
240
|
+
* @returns CSL-JSON items
|
|
241
|
+
*/
|
|
242
|
+
const parseFile = async (file: string): Promise<Record<string, unknown>[]> => {
|
|
243
|
+
const {mtimeMs} = await stat(file);
|
|
244
|
+
|
|
245
|
+
const cached = fileCache.get(file);
|
|
246
|
+
if (cached?.mtime === mtimeMs) {
|
|
247
|
+
return cached.data;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Name the input type instead of letting citation-js guess it (it would, e.g., fetch a file
|
|
251
|
+
// whose content is a URL)
|
|
252
|
+
// (citation-js types require every input option; the others have defaults)
|
|
253
|
+
const text = await readFile(file, "utf8");
|
|
254
|
+
const json = path.extname(file).toLowerCase() === ".json";
|
|
255
|
+
const inputOptions = {forceType: json ? "@csl/list+object" : "@biblatex/text"} as InputOptions;
|
|
256
|
+
const cite = new Cite(json ? (JSON.parse(text) as unknown) : text, inputOptions);
|
|
257
|
+
const data = cite.data as Record<string, unknown>[];
|
|
258
|
+
fileCache.set(file, {data, mtime: mtimeMs});
|
|
259
|
+
return data;
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Load a bibliography file. Failures are reported under the configured name: the underlying error
|
|
264
|
+
* names the absolute path and, for syntax errors, quotes the file.
|
|
265
|
+
* @param file Absolute path
|
|
266
|
+
* @param name Path as configured
|
|
267
|
+
* @returns CSL-JSON items
|
|
268
|
+
*/
|
|
269
|
+
const loadFile = (file: string, name: string) =>
|
|
270
|
+
parseFile(file).catch((error: unknown) => {
|
|
271
|
+
const missing = isNotFound(error);
|
|
272
|
+
const problem = missing ? "was not found" : "could not be loaded";
|
|
273
|
+
throw new CitationError(`Bibliography "${name}" ${problem}`, error, !missing);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Convert formatted HTML to plain text (used for tooltips)
|
|
278
|
+
* @param html HTML
|
|
279
|
+
* @returns Plain text
|
|
280
|
+
*/
|
|
281
|
+
const htmlToText = (html: string) =>
|
|
282
|
+
html.replaceAll(/<[^>]+>/gu, "").replaceAll(ENTITY_RE, (_, entity: string) => {
|
|
283
|
+
if (entity.startsWith("#x") || entity.startsWith("#X")) {
|
|
284
|
+
return String.fromCodePoint(Number.parseInt(entity.slice(2), 16));
|
|
285
|
+
}
|
|
286
|
+
if (entity.startsWith("#")) {
|
|
287
|
+
return String.fromCodePoint(Number.parseInt(entity.slice(1), 10));
|
|
288
|
+
}
|
|
289
|
+
return NAMED_ENTITIES[entity.toLowerCase()] ?? "";
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Build an external link
|
|
294
|
+
* @param href Link target
|
|
295
|
+
* @param text Link text
|
|
296
|
+
* @returns HTML
|
|
297
|
+
*/
|
|
298
|
+
const anchor = (href: string, text: string) =>
|
|
299
|
+
`<a href="${href.replaceAll('"', """)}" target="_blank" rel="noopener">${text}</a>`;
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* A URL, or a DOI after `doi:`, in the text of a formatted reference. Quotes, `<` and `>` end a
|
|
303
|
+
* match (citeproc escapes `<` and `>` but not quotes); trailing punctuation is left out.
|
|
304
|
+
*/
|
|
305
|
+
const LINK_RE =
|
|
306
|
+
/(^|[\s(>])(https?:\/\/[^\s<>"']*[^\s<>"'.,;:])|(doi:\s*)(10\.[^\s<>"']*[^\s<>"'.,;:])/giu;
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Move unbalanced closing parentheses from the end of a link back into the text (`(see
|
|
310
|
+
* https://x.org/a)` vs `https://x.org/a_(b)`)
|
|
311
|
+
* @param link Matched URL or DOI
|
|
312
|
+
* @returns Link and the text that follows it
|
|
313
|
+
*/
|
|
314
|
+
const trimParens = (link: string): [string, string] => {
|
|
315
|
+
let end = link.length;
|
|
316
|
+
while (
|
|
317
|
+
link[end - 1] === ")" &&
|
|
318
|
+
link.slice(0, end).split(")").length > link.slice(0, end).split("(").length
|
|
319
|
+
) {
|
|
320
|
+
end -= 1;
|
|
321
|
+
}
|
|
322
|
+
return [link.slice(0, end), link.slice(end)];
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Turn URLs and DOIs in formatted references into links. Reference text is third-party text, and
|
|
327
|
+
* the result is rendered with `v-html`: only text between tags (outside existing links) is scanned,
|
|
328
|
+
* in a single pass, so a link is never matched again inside another link's `href`.
|
|
329
|
+
* @param html Formatted reference
|
|
330
|
+
* @returns HTML with links
|
|
331
|
+
*/
|
|
332
|
+
const linkify = (html: string) => {
|
|
333
|
+
let inLink = false;
|
|
334
|
+
|
|
335
|
+
return html
|
|
336
|
+
.split(/(<[^>]*>)/u)
|
|
337
|
+
.map(part => {
|
|
338
|
+
if (part.startsWith("<")) {
|
|
339
|
+
if (/^<a[\s>]/iu.test(part)) {
|
|
340
|
+
inLink = true;
|
|
341
|
+
} else if (/^<\/a>/iu.test(part)) {
|
|
342
|
+
inLink = false;
|
|
343
|
+
}
|
|
344
|
+
return part;
|
|
345
|
+
}
|
|
346
|
+
if (inLink) {
|
|
347
|
+
return part;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return part.replaceAll(
|
|
351
|
+
LINK_RE,
|
|
352
|
+
(_, urlLead?: string, url?: string, doiLead?: string, doi?: string) => {
|
|
353
|
+
if (url !== undefined) {
|
|
354
|
+
const [link, rest] = trimParens(url);
|
|
355
|
+
return `${urlLead ?? ""}${anchor(link, link)}${rest}`;
|
|
356
|
+
}
|
|
357
|
+
const [link, rest] = trimParens(doi ?? "");
|
|
358
|
+
return `${doiLead ?? ""}${anchor(`https://doi.org/${link}`, link)}${rest}`;
|
|
359
|
+
},
|
|
360
|
+
);
|
|
361
|
+
})
|
|
362
|
+
.join("");
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Turn the characters that start and end a tag into text
|
|
367
|
+
* @param text Text between two tags
|
|
368
|
+
* @returns Text without `<` and `>`
|
|
369
|
+
*/
|
|
370
|
+
const escapeAngles = (text: string) => text.replaceAll("<", "<").replaceAll(">", ">");
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Keep citeproc's own formatting tags in a formatted reference and turn every other `<` and `>`
|
|
374
|
+
* into text. citeproc escapes bibliography text, but not everything a style supplies: a `.csl`
|
|
375
|
+
* file can redefine the locale's quotation marks, which are written out as they are, and the
|
|
376
|
+
* result is rendered with `v-html`.
|
|
377
|
+
* @param html Formatted reference
|
|
378
|
+
* @returns HTML with no other markup than citeproc's formatting tags
|
|
379
|
+
*/
|
|
380
|
+
const sanitize = (html: string) => {
|
|
381
|
+
let end = 0;
|
|
382
|
+
let result = "";
|
|
383
|
+
for (const match of html.matchAll(CITEPROC_TAG_RE)) {
|
|
384
|
+
result += escapeAngles(html.slice(end, match.index)) + match[0];
|
|
385
|
+
end = match.index + match[0].length;
|
|
386
|
+
}
|
|
387
|
+
return result + escapeAngles(html.slice(end));
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Split citeproc bibliography HTML into per-entry HTML (without the numeric left margin)
|
|
392
|
+
* @param html citeproc bibliography output
|
|
393
|
+
* @returns `[entry ID, html]` pairs in bibliography order
|
|
394
|
+
*/
|
|
395
|
+
const splitBibliography = (html: string): [string, string][] =>
|
|
396
|
+
html
|
|
397
|
+
.split('<div data-csl-entry-id="')
|
|
398
|
+
.slice(1)
|
|
399
|
+
.map(chunk => {
|
|
400
|
+
const id = chunk.slice(0, chunk.indexOf('"'));
|
|
401
|
+
const body = chunk.slice(chunk.indexOf(">") + 1);
|
|
402
|
+
|
|
403
|
+
// Numeric styles wrap the reference in `csl-right-inline` next to a `csl-left-margin` label
|
|
404
|
+
const inline = /<div class="csl-right-inline">([\s\S]*?)<\/div>/u.exec(body)?.[1];
|
|
405
|
+
const content =
|
|
406
|
+
inline ??
|
|
407
|
+
body
|
|
408
|
+
.replace(/(?:\s*<\/div>)+\s*$/u, "")
|
|
409
|
+
.replace(/<div class="csl-left-margin">[\s\S]*?<\/div>/u, "");
|
|
410
|
+
|
|
411
|
+
return [id, linkify(sanitize(content.trim()))];
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Issued year of a CSL-JSON item
|
|
416
|
+
* @param item CSL-JSON item
|
|
417
|
+
* @returns Year, or `n.d.`
|
|
418
|
+
*/
|
|
419
|
+
const issuedYear = (item: Record<string, unknown>) => {
|
|
420
|
+
const issued = item["issued"] as CslDate | undefined;
|
|
421
|
+
const year = issued?.["date-parts"]?.[0]?.[0];
|
|
422
|
+
if (year !== undefined) {
|
|
423
|
+
return String(year);
|
|
424
|
+
}
|
|
425
|
+
return issued?.literal ?? "n.d.";
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Cite keys of a `keys` option: a string separated by commas, semicolons or spaces
|
|
430
|
+
* @param value Option value, or an item of it
|
|
431
|
+
* @returns Keys (none for anything but a string or a number)
|
|
432
|
+
*/
|
|
433
|
+
const splitKeys = (value: unknown) => {
|
|
434
|
+
if (typeof value === "number") {
|
|
435
|
+
return [String(value)];
|
|
436
|
+
}
|
|
437
|
+
return typeof value === "string" ? value.split(/[,;\s]+/u).filter(Boolean) : [];
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Entries the deck's reference lists show besides the cited ones: `<Bibliography all>` and
|
|
442
|
+
* `<Bibliography keys>` in slides, and the same options in the frontmatter of `references` slides.
|
|
443
|
+
* Other uncited entries are not formatted, so a published deck does not contain the rest of the
|
|
444
|
+
* bibliography file.
|
|
445
|
+
* @param slides Slides
|
|
446
|
+
* @returns Listed keys
|
|
447
|
+
*/
|
|
448
|
+
const listedEntries = (slides: SlideSource[]): ListedKeys => {
|
|
449
|
+
let all = false;
|
|
450
|
+
const keys = new Set<string>();
|
|
451
|
+
|
|
452
|
+
for (const {content, frontmatter} of slides) {
|
|
453
|
+
const listed = listedKeys(content);
|
|
454
|
+
const options: unknown[] = [];
|
|
455
|
+
|
|
456
|
+
// The `references` layout passes its frontmatter on to `<Bibliography>`
|
|
457
|
+
if (frontmatter["layout"] === "references") {
|
|
458
|
+
all ||= frontmatter["all"] === true;
|
|
459
|
+
options.push(frontmatter["keys"]);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
all ||= listed.all;
|
|
463
|
+
for (const key of [...listed.keys, ...options.flat().flatMap(option => splitKeys(option))]) {
|
|
464
|
+
keys.add(key);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
return {all, keys: [...keys].toSorted()};
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Inputs of `formatEntries`
|
|
473
|
+
*/
|
|
474
|
+
interface FormatInput {
|
|
475
|
+
/**
|
|
476
|
+
* Bibliography items by key
|
|
477
|
+
*/
|
|
478
|
+
byId: Map<string, Record<string, unknown>>;
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Cited keys that exist in the bibliography
|
|
482
|
+
*/
|
|
483
|
+
citedPresent: string[];
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Reference list grouping
|
|
487
|
+
*/
|
|
488
|
+
grouping: CitationData["grouping"];
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Locale
|
|
492
|
+
*/
|
|
493
|
+
lang: string;
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Cited keys missing from the bibliography
|
|
497
|
+
*/
|
|
498
|
+
missing: string[];
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Whether the style is numeric
|
|
502
|
+
*/
|
|
503
|
+
numeric: boolean;
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Set of cited keys
|
|
507
|
+
*/
|
|
508
|
+
seen: Set<string>;
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Keys cited per slide
|
|
512
|
+
*/
|
|
513
|
+
slides: CitationData["slides"];
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Resolved style
|
|
517
|
+
*/
|
|
518
|
+
style: ResolvedStyle;
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Bibliography keys that are not cited but listed by a reference list
|
|
522
|
+
*/
|
|
523
|
+
uncited: string[];
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Format the bibliography and in-text labels (throws when the style is malformed)
|
|
528
|
+
* @param input Bibliography items, cited keys and style
|
|
529
|
+
* @returns Citation data
|
|
530
|
+
*/
|
|
531
|
+
const formatEntries = (input: FormatInput): CitationData => {
|
|
532
|
+
const {byId, citedPresent, grouping, lang, missing, numeric, seen, slides, style, uncited} =
|
|
533
|
+
input;
|
|
534
|
+
|
|
535
|
+
// citation-js writes each entry's ID into the bibliography HTML without escaping it, so a key
|
|
536
|
+
// with a quote or `<` in it would end up as markup (rendered with `v-html`). Entries are
|
|
537
|
+
// therefore formatted under generated IDs
|
|
538
|
+
const ids = new Map<string, string>();
|
|
539
|
+
const items: Record<string, unknown>[] = [];
|
|
540
|
+
const keyOf = new Map<string, string>();
|
|
541
|
+
for (const [index, key] of [...citedPresent, ...uncited].entries()) {
|
|
542
|
+
const id = `entry-${index}`;
|
|
543
|
+
ids.set(key, id);
|
|
544
|
+
items.push({...byId.get(key), id});
|
|
545
|
+
keyOf.set(id, key);
|
|
546
|
+
}
|
|
547
|
+
const cite = new Cite(items);
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Format bibliography entries
|
|
551
|
+
* @param entryKeys Keys to format
|
|
552
|
+
* @returns `[key, html]` pairs in bibliography order
|
|
553
|
+
*/
|
|
554
|
+
const format = (entryKeys: string[]): [string, string][] => {
|
|
555
|
+
if (entryKeys.length === 0) {
|
|
556
|
+
return [];
|
|
557
|
+
}
|
|
558
|
+
const entry = entryKeys.map(key => ids.get(key) ?? "");
|
|
559
|
+
const html = cite.format("bibliography", {entry, format: "html", lang, template: style.name});
|
|
560
|
+
return splitBibliography(html).flatMap(([id, body]): [string, string][] => {
|
|
561
|
+
const key = keyOf.get(id);
|
|
562
|
+
return key === undefined ? [] : [[key, body]];
|
|
563
|
+
});
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
// Numeric styles number cited entries first (some styles, e.g. ACM, sort them alphabetically),
|
|
567
|
+
// then append uncited entries; author-date styles sort everything together
|
|
568
|
+
const formatted = numeric
|
|
569
|
+
? [...format(citedPresent), ...format(uncited)]
|
|
570
|
+
: format([...citedPresent, ...uncited]);
|
|
571
|
+
|
|
572
|
+
// Assigning to an object by key would, for the key `__proto__`, replace the object's prototype
|
|
573
|
+
// instead of adding an entry
|
|
574
|
+
const entries: [string, CitationEntry][] = [];
|
|
575
|
+
const order: string[] = [];
|
|
576
|
+
|
|
577
|
+
for (const [index, [id, html]] of formatted.entries()) {
|
|
578
|
+
const item = byId.get(id);
|
|
579
|
+
if (item === undefined) {
|
|
580
|
+
continue;
|
|
581
|
+
}
|
|
582
|
+
order.push(id);
|
|
583
|
+
|
|
584
|
+
// Author-date styles need a short in-text label ("Smith et al., 2024")
|
|
585
|
+
let label = "";
|
|
586
|
+
if (!numeric && seen.has(id)) {
|
|
587
|
+
// citation-js types only describe bibliography options; citations accept the same ones
|
|
588
|
+
const citationOptions = {entry: ids.get(id), format: "text", lang, template: style.name};
|
|
589
|
+
const text = cite.format("citation", citationOptions as never);
|
|
590
|
+
label = text.trim().replace(/^\((.*)\)$/su, "$1");
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
entries.push([
|
|
594
|
+
id,
|
|
595
|
+
{
|
|
596
|
+
cited: seen.has(id),
|
|
597
|
+
html,
|
|
598
|
+
label,
|
|
599
|
+
n: index + 1,
|
|
600
|
+
text: htmlToText(html),
|
|
601
|
+
year: issuedYear(item),
|
|
602
|
+
},
|
|
603
|
+
]);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
if (missing.length > 0) {
|
|
607
|
+
warn(`Missing citation keys: ${missing.join(", ")}`);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
return {entries: Object.fromEntries(entries), grouping, numeric, order, slides};
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* Build the citation data for a deck
|
|
615
|
+
* @param options Options
|
|
616
|
+
* @returns Citation data (never throws; problems are reported in `error`)
|
|
617
|
+
*/
|
|
618
|
+
export const buildCitations = async (options: CitationOptions): Promise<CitationData> => {
|
|
619
|
+
// Collect cite keys per slide and in order of first appearance
|
|
620
|
+
const cited: string[] = [];
|
|
621
|
+
const seen = new Set<string>();
|
|
622
|
+
const slides: Record<number, string[]> = {};
|
|
623
|
+
|
|
624
|
+
for (const [index, {content}] of options.slides.entries()) {
|
|
625
|
+
const keys = citedKeys(content);
|
|
626
|
+
if (keys.length > 0) {
|
|
627
|
+
slides[index + 1] = keys;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
for (const key of keys) {
|
|
631
|
+
if (!seen.has(key)) {
|
|
632
|
+
seen.add(key);
|
|
633
|
+
cited.push(key);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
const styleId = options.style ?? "ieee";
|
|
639
|
+
const lang = options.lang ?? "en-US";
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* Result without any formatted entries
|
|
643
|
+
* @param error Error message
|
|
644
|
+
* @returns Citation data
|
|
645
|
+
*/
|
|
646
|
+
const empty = (error?: string): CitationData => ({
|
|
647
|
+
entries: {},
|
|
648
|
+
...(error === undefined ? {} : {error}),
|
|
649
|
+
grouping: "combined",
|
|
650
|
+
numeric: true,
|
|
651
|
+
order: [],
|
|
652
|
+
slides,
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
const names = [options.bibliography ?? []].flat();
|
|
656
|
+
const paths = bibliographyPaths(names, options.root);
|
|
657
|
+
if (paths.length === 0) {
|
|
658
|
+
return empty(cited.length > 0 ? "No bibliography configured" : undefined);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// Load bibliography files, style and locale
|
|
662
|
+
const loaded = await Promise.all([
|
|
663
|
+
Promise.all(paths.map((file, index) => loadFile(file, names[index] ?? file))),
|
|
664
|
+
resolveStyle(styleId, options.root, options.cacheDir),
|
|
665
|
+
ensureLocale(lang, options.cacheDir),
|
|
666
|
+
]).catch((error: unknown) => {
|
|
667
|
+
warn(terminalMessage(error));
|
|
668
|
+
return slideMessage(error);
|
|
669
|
+
});
|
|
670
|
+
if (typeof loaded === "string") {
|
|
671
|
+
return empty(loaded);
|
|
672
|
+
}
|
|
673
|
+
const [files, style] = loaded;
|
|
674
|
+
const listed = listedEntries(options.slides);
|
|
675
|
+
|
|
676
|
+
// Reuse the previous result if nothing changed
|
|
677
|
+
const cacheKey = JSON.stringify([
|
|
678
|
+
listed,
|
|
679
|
+
paths,
|
|
680
|
+
paths.map(file => fileCache.get(file)?.mtime),
|
|
681
|
+
style.name,
|
|
682
|
+
lang,
|
|
683
|
+
cited,
|
|
684
|
+
slides,
|
|
685
|
+
]);
|
|
686
|
+
if (formatCache?.key === cacheKey) {
|
|
687
|
+
return formatCache.data;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
const byId = new Map<string, Record<string, unknown>>();
|
|
691
|
+
const duplicates = new Set<string>();
|
|
692
|
+
for (const item of files.flat()) {
|
|
693
|
+
const id = String(item["id"]);
|
|
694
|
+
if (byId.has(id)) {
|
|
695
|
+
duplicates.add(id);
|
|
696
|
+
}
|
|
697
|
+
byId.set(id, item);
|
|
698
|
+
}
|
|
699
|
+
if (duplicates.size > 0) {
|
|
700
|
+
warn(`Duplicate bibliography keys (the last one wins): ${[...duplicates].join(", ")}`);
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
const citedPresent = cited.filter(key => byId.has(key));
|
|
704
|
+
const grouping = /<title>IEEE/u.test(style.xml) ? "separate" : "combined";
|
|
705
|
+
const missing = cited.filter(key => !byId.has(key));
|
|
706
|
+
const numeric = /citation-format="numeric"/u.test(style.xml);
|
|
707
|
+
const listedSet = new Set(listed.keys);
|
|
708
|
+
const uncited = [...byId.keys()].filter(
|
|
709
|
+
key => !seen.has(key) && (listed.all || listedSet.has(key)),
|
|
710
|
+
);
|
|
711
|
+
|
|
712
|
+
// A malformed style (or a corrupt download) throws while formatting: report it on the slides
|
|
713
|
+
// instead of failing the build
|
|
714
|
+
try {
|
|
715
|
+
const data = formatEntries({
|
|
716
|
+
byId,
|
|
717
|
+
citedPresent,
|
|
718
|
+
grouping,
|
|
719
|
+
lang,
|
|
720
|
+
missing,
|
|
721
|
+
numeric,
|
|
722
|
+
seen,
|
|
723
|
+
slides,
|
|
724
|
+
style,
|
|
725
|
+
uncited,
|
|
726
|
+
});
|
|
727
|
+
formatCache = {data, key: cacheKey};
|
|
728
|
+
return data;
|
|
729
|
+
} catch (error) {
|
|
730
|
+
const failure = new CitationError(
|
|
731
|
+
`Formatting references with style "${styleId}" failed`,
|
|
732
|
+
error,
|
|
733
|
+
true,
|
|
734
|
+
);
|
|
735
|
+
warn(terminalMessage(failure));
|
|
736
|
+
return empty(slideMessage(failure));
|
|
737
|
+
}
|
|
738
|
+
};
|