@publisle/adapter-core 0.2.2 → 0.2.4

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.
@@ -0,0 +1,475 @@
1
+ // Class-based highlighting for common code languages. Tokens are spans, never
2
+ // inline styles, so a host content-security policy can still forbid them.
3
+
4
+ export type TokenKind =
5
+ "plain" | "comment" | "string" | "number" | "keyword" | "tag" | "attr";
6
+
7
+ export interface CodeToken {
8
+ readonly kind: TokenKind;
9
+ readonly text: string;
10
+ }
11
+
12
+ export interface HighlightedCode {
13
+ /** Visible language name. Absent when the block names none. */
14
+ readonly label?: string;
15
+ /** Safe `language-*` class. Absent for unnamed or unusual spellings. */
16
+ readonly className?: string;
17
+ readonly tokens: readonly CodeToken[];
18
+ }
19
+
20
+ type Family =
21
+ | "c"
22
+ | "python"
23
+ | "json"
24
+ | "markup"
25
+ | "css"
26
+ | "shell"
27
+ | "sql"
28
+ | "yaml"
29
+ | "plain";
30
+
31
+ interface Language {
32
+ readonly id: string;
33
+ readonly label: string;
34
+ readonly family: Family;
35
+ readonly aliases: readonly string[];
36
+ }
37
+
38
+ const LANGUAGES: readonly Language[] = [
39
+ {
40
+ id: "javascript",
41
+ label: "JavaScript",
42
+ family: "c",
43
+ aliases: ["js", "javascript"],
44
+ },
45
+ {
46
+ id: "typescript",
47
+ label: "TypeScript",
48
+ family: "c",
49
+ aliases: ["ts", "typescript"],
50
+ },
51
+ { id: "jsx", label: "JSX", family: "c", aliases: ["jsx"] },
52
+ { id: "tsx", label: "TSX", family: "c", aliases: ["tsx"] },
53
+ {
54
+ id: "python",
55
+ label: "Python",
56
+ family: "python",
57
+ aliases: ["py", "python"],
58
+ },
59
+ { id: "json", label: "JSON", family: "json", aliases: ["json"] },
60
+ {
61
+ id: "html",
62
+ label: "HTML",
63
+ family: "markup",
64
+ aliases: ["html", "xml", "svg"],
65
+ },
66
+ { id: "css", label: "CSS", family: "css", aliases: ["css"] },
67
+ {
68
+ id: "bash",
69
+ label: "Bash",
70
+ family: "shell",
71
+ aliases: ["bash", "sh", "shell", "zsh"],
72
+ },
73
+ { id: "rust", label: "Rust", family: "c", aliases: ["rs", "rust"] },
74
+ { id: "go", label: "Go", family: "c", aliases: ["go", "golang"] },
75
+ { id: "java", label: "Java", family: "c", aliases: ["java"] },
76
+ { id: "c", label: "C", family: "c", aliases: ["c", "h"] },
77
+ {
78
+ id: "cpp",
79
+ label: "C++",
80
+ family: "c",
81
+ aliases: ["cpp", "c++", "cc", "hpp"],
82
+ },
83
+ { id: "sql", label: "SQL", family: "sql", aliases: ["sql"] },
84
+ { id: "yaml", label: "YAML", family: "yaml", aliases: ["yaml", "yml"] },
85
+ {
86
+ id: "markdown",
87
+ label: "Markdown",
88
+ family: "plain",
89
+ aliases: ["md", "markdown"],
90
+ },
91
+ {
92
+ id: "text",
93
+ label: "Text",
94
+ family: "plain",
95
+ aliases: ["text", "txt", "plaintext"],
96
+ },
97
+ ];
98
+
99
+ const BY_ALIAS = new Map<string, Language>();
100
+ for (const language of LANGUAGES)
101
+ for (const alias of language.aliases) BY_ALIAS.set(alias, language);
102
+
103
+ const C_KEYWORDS = words(
104
+ "as async await bool boolean break case catch chan class const continue declare default defer do else enum export extends false fn for from func function go if impl import in interface let loop map match mod mut namespace new null nullptr of package private protected pub public readonly return select self Self static struct super switch template this throw trait true try type typeof undefined use using var void while yield",
105
+ );
106
+ const PYTHON_KEYWORDS = words(
107
+ "and as assert async await break class continue def del elif else except False finally for from global if import in is lambda None nonlocal not or pass raise return True try while with yield",
108
+ );
109
+ const SQL_KEYWORDS = words(
110
+ "and as by create delete desc distinct from group having in inner insert into join left limit not null on or order outer right select set table update values where",
111
+ );
112
+ const SHELL_KEYWORDS = words(
113
+ "cd do done echo elif else esac exit export fi for if in then while",
114
+ );
115
+ const JSON_KEYWORDS = words("true false null");
116
+
117
+ function words(value: string): Set<string> {
118
+ return new Set(value.split(/\s+/u));
119
+ }
120
+
121
+ function lookup(language: string | undefined): Language | undefined {
122
+ const key = language?.trim().toLowerCase();
123
+ if (!key) return undefined;
124
+ return BY_ALIAS.get(key);
125
+ }
126
+
127
+ function visibleLabel(language: string): string | undefined {
128
+ let cleaned = "";
129
+ for (const char of language.trim()) {
130
+ const code = char.codePointAt(0) ?? 0;
131
+ if (code >= 32) cleaned += char;
132
+ }
133
+ if (!cleaned) return undefined;
134
+ return cleaned.slice(0, 32);
135
+ }
136
+
137
+ /** Tokenize `source` for `language`. Unknown languages stay plain text and still name themselves. */
138
+ export function highlightCode(
139
+ source: string,
140
+ language: string | undefined,
141
+ ): HighlightedCode {
142
+ const known = lookup(language);
143
+ const label = known?.label ?? (language ? visibleLabel(language) : undefined);
144
+ const className = known ? `language-${known.id}` : undefined;
145
+ const tokens =
146
+ !known || known.family === "plain"
147
+ ? [{ kind: "plain" as const, text: source }]
148
+ : tokenize(source, known.family);
149
+ return {
150
+ ...(label ? { label } : {}),
151
+ ...(className ? { className } : {}),
152
+ tokens,
153
+ };
154
+ }
155
+
156
+ function tokenize(source: string, family: Family): CodeToken[] {
157
+ if (family === "markup") return markup(source);
158
+ if (family === "json") return scan(source, "json");
159
+ if (family === "css") return css(source);
160
+ if (family === "yaml") return yaml(source);
161
+ if (family === "plain") return [{ kind: "plain", text: source }];
162
+ return scan(source, family);
163
+ }
164
+
165
+ function scan(
166
+ source: string,
167
+ family: Exclude<Family, "markup" | "css" | "yaml" | "plain">,
168
+ ): CodeToken[] {
169
+ const keywords =
170
+ family === "python"
171
+ ? PYTHON_KEYWORDS
172
+ : family === "sql"
173
+ ? SQL_KEYWORDS
174
+ : family === "shell"
175
+ ? SHELL_KEYWORDS
176
+ : family === "json"
177
+ ? JSON_KEYWORDS
178
+ : C_KEYWORDS;
179
+ const tokens: CodeToken[] = [];
180
+ let index = 0;
181
+ while (index < source.length) {
182
+ const comment = readComment(source, index, family);
183
+ if (comment) {
184
+ push(tokens, "comment", comment);
185
+ index += comment.length;
186
+ continue;
187
+ }
188
+ const string = readString(source, index, family);
189
+ if (string) {
190
+ push(tokens, "string", string);
191
+ index += string.length;
192
+ continue;
193
+ }
194
+ const number = match(
195
+ source,
196
+ index,
197
+ /0[xX][0-9a-fA-F]+|\d[\d_]*(?:\.\d[\d_]*)?/y,
198
+ );
199
+ if (number) {
200
+ push(tokens, "number", number);
201
+ index += number.length;
202
+ continue;
203
+ }
204
+ const word = match(source, index, /[A-Za-z_][A-Za-z0-9_]*/y);
205
+ if (word) {
206
+ push(tokens, keywords.has(word) ? "keyword" : "plain", word);
207
+ index += word.length;
208
+ continue;
209
+ }
210
+ push(tokens, "plain", source[index] ?? "");
211
+ index += 1;
212
+ }
213
+ return tokens;
214
+ }
215
+
216
+ function readComment(
217
+ source: string,
218
+ index: number,
219
+ family: Exclude<Family, "markup" | "css" | "yaml" | "plain">,
220
+ ): string | undefined {
221
+ if (family === "json") return undefined;
222
+ if (family === "python" || family === "shell") {
223
+ if (source[index] !== "#") return undefined;
224
+ if (
225
+ family === "shell" &&
226
+ (source[index - 1] === "$" || source[index - 1] === "\\")
227
+ )
228
+ return undefined;
229
+ const end = source.indexOf("\n", index);
230
+ return source.slice(index, end === -1 ? source.length : end);
231
+ }
232
+ if (source.startsWith("//", index)) {
233
+ const end = source.indexOf("\n", index);
234
+ return source.slice(index, end === -1 ? source.length : end);
235
+ }
236
+ if (source.startsWith("/*", index)) {
237
+ const end = source.indexOf("*/", index + 2);
238
+ return source.slice(index, end === -1 ? source.length : end + 2);
239
+ }
240
+ if (
241
+ (family === "c" || family === "sql") &&
242
+ source[index] === "#" &&
243
+ (index === 0 || source[index - 1] === "\n")
244
+ ) {
245
+ const end = source.indexOf("\n", index);
246
+ return source.slice(index, end === -1 ? source.length : end);
247
+ }
248
+ if (family === "sql" && source.startsWith("--", index)) {
249
+ const end = source.indexOf("\n", index);
250
+ return source.slice(index, end === -1 ? source.length : end);
251
+ }
252
+ return undefined;
253
+ }
254
+
255
+ function readString(
256
+ source: string,
257
+ index: number,
258
+ family: Exclude<Family, "markup" | "css" | "yaml" | "plain">,
259
+ ): string | undefined {
260
+ let start = index;
261
+ if (family === "python") {
262
+ const prefix = match(source, index, /[rRuUbBfF]{1,2}(?=["'])/y);
263
+ if (prefix) start += prefix.length;
264
+ }
265
+ const triple = source.startsWith('"""', start)
266
+ ? '"""'
267
+ : source.startsWith("'''", start)
268
+ ? "'''"
269
+ : undefined;
270
+ if (family === "python" && triple) {
271
+ const end = source.indexOf(triple, start + 3);
272
+ return source.slice(index, end === -1 ? source.length : end + 3);
273
+ }
274
+ const quote = source[start];
275
+ if (quote !== '"' && quote !== "'" && !(quote === "`" && family === "c"))
276
+ return undefined;
277
+ if (family === "json" && quote !== '"') return undefined;
278
+ let cursor = start + 1;
279
+ while (cursor < source.length) {
280
+ if (source[cursor] === "\\") {
281
+ cursor += 2;
282
+ continue;
283
+ }
284
+ if (source[cursor] === quote) return source.slice(index, cursor + 1);
285
+ if (source[cursor] === "\n" && quote !== "`") break;
286
+ cursor += 1;
287
+ }
288
+ return source.slice(index, cursor);
289
+ }
290
+
291
+ function markup(source: string): CodeToken[] {
292
+ const tokens: CodeToken[] = [];
293
+ let index = 0;
294
+ while (index < source.length) {
295
+ if (source.startsWith("<!--", index)) {
296
+ const end = source.indexOf("-->", index + 4);
297
+ const next = end === -1 ? source.length : end + 3;
298
+ push(tokens, "comment", source.slice(index, next));
299
+ index = next;
300
+ continue;
301
+ }
302
+ if (source[index] === "<") {
303
+ const end = source.indexOf(">", index + 1);
304
+ const next = end === -1 ? source.length : end + 1;
305
+ paintTag(tokens, source.slice(index, next));
306
+ index = next;
307
+ continue;
308
+ }
309
+ const next = source.indexOf("<", index);
310
+ const end = next === -1 ? source.length : next;
311
+ push(tokens, "plain", source.slice(index, end));
312
+ index = end;
313
+ }
314
+ return tokens;
315
+ }
316
+
317
+ function paintTag(tokens: CodeToken[], chunk: string): void {
318
+ let index = 0;
319
+ if (chunk[index] === "<") {
320
+ push(tokens, "tag", "<");
321
+ index += 1;
322
+ }
323
+ if (chunk[index] === "/") {
324
+ push(tokens, "tag", "/");
325
+ index += 1;
326
+ }
327
+ const name = match(chunk, index, /[A-Za-z][\w:-]*/y);
328
+ if (name) {
329
+ push(tokens, "tag", name);
330
+ index += name.length;
331
+ }
332
+ while (index < chunk.length) {
333
+ const string = readQuoted(chunk, index);
334
+ if (string) {
335
+ push(tokens, "string", string);
336
+ index += string.length;
337
+ continue;
338
+ }
339
+ const ident = match(chunk, index, /[\w:-]+/y);
340
+ if (ident) {
341
+ const next = chunk[index + ident.length];
342
+ push(tokens, next === "=" ? "attr" : "plain", ident);
343
+ index += ident.length;
344
+ continue;
345
+ }
346
+ const mark = chunk[index] ?? "";
347
+ push(tokens, mark === ">" || mark === "/" ? "tag" : "plain", mark);
348
+ index += 1;
349
+ }
350
+ }
351
+
352
+ function css(source: string): CodeToken[] {
353
+ const tokens: CodeToken[] = [];
354
+ let index = 0;
355
+ while (index < source.length) {
356
+ if (source.startsWith("/*", index)) {
357
+ const end = source.indexOf("*/", index + 2);
358
+ const next = end === -1 ? source.length : end + 2;
359
+ push(tokens, "comment", source.slice(index, next));
360
+ index = next;
361
+ continue;
362
+ }
363
+ const string = readQuoted(source, index);
364
+ if (string) {
365
+ push(tokens, "string", string);
366
+ index += string.length;
367
+ continue;
368
+ }
369
+ const color = match(source, index, /#[0-9a-fA-F]{3,8}\b/y);
370
+ const number = color ?? match(source, index, /\d[\d.]*(?:[a-z%]+)?/y);
371
+ if (number) {
372
+ push(tokens, "number", number);
373
+ index += number.length;
374
+ continue;
375
+ }
376
+ const word = match(source, index, /[@A-Za-z_-][A-Za-z0-9_-]*/y);
377
+ if (word) {
378
+ const next = source[index + word.length];
379
+ const kind = word.startsWith("@")
380
+ ? "keyword"
381
+ : next === ":"
382
+ ? "attr"
383
+ : "plain";
384
+ push(tokens, kind, word);
385
+ index += word.length;
386
+ continue;
387
+ }
388
+ push(tokens, "plain", source[index] ?? "");
389
+ index += 1;
390
+ }
391
+ return tokens;
392
+ }
393
+
394
+ function yaml(source: string): CodeToken[] {
395
+ const tokens: CodeToken[] = [];
396
+ let index = 0;
397
+ while (index < source.length) {
398
+ if (
399
+ source[index] === "#" &&
400
+ (index === 0 || /\s/u.test(source[index - 1] ?? ""))
401
+ ) {
402
+ const end = source.indexOf("\n", index);
403
+ push(
404
+ tokens,
405
+ "comment",
406
+ source.slice(index, end === -1 ? source.length : end),
407
+ );
408
+ index = end === -1 ? source.length : end;
409
+ continue;
410
+ }
411
+ const string = readQuoted(source, index);
412
+ if (string) {
413
+ push(tokens, "string", string);
414
+ index += string.length;
415
+ continue;
416
+ }
417
+ const number = match(source, index, /\d[\d.]*/y);
418
+ if (
419
+ number &&
420
+ (index === 0 || !/[A-Za-z_]/u.test(source[index - 1] ?? ""))
421
+ ) {
422
+ push(tokens, "number", number);
423
+ index += number.length;
424
+ continue;
425
+ }
426
+ const word = match(source, index, /[A-Za-z_][A-Za-z0-9_-]*/y);
427
+ if (word) {
428
+ const rest = source.slice(index + word.length);
429
+ const key = /^\s*:/u.test(rest);
430
+ const flag = /^(?:true|false|yes|no|null)$/iu.test(word);
431
+ push(tokens, key ? "attr" : flag ? "keyword" : "plain", word);
432
+ index += word.length;
433
+ continue;
434
+ }
435
+ push(tokens, "plain", source[index] ?? "");
436
+ index += 1;
437
+ }
438
+ return tokens;
439
+ }
440
+
441
+ function readQuoted(source: string, index: number): string | undefined {
442
+ const quote = source[index];
443
+ if (quote !== '"' && quote !== "'") return undefined;
444
+ let cursor = index + 1;
445
+ while (cursor < source.length) {
446
+ if (source[cursor] === "\\") {
447
+ cursor += 2;
448
+ continue;
449
+ }
450
+ if (source[cursor] === quote) return source.slice(index, cursor + 1);
451
+ if (source[cursor] === "\n") break;
452
+ cursor += 1;
453
+ }
454
+ return source.slice(index, cursor);
455
+ }
456
+
457
+ function match(
458
+ source: string,
459
+ index: number,
460
+ pattern: RegExp,
461
+ ): string | undefined {
462
+ pattern.lastIndex = index;
463
+ const found = pattern.exec(source);
464
+ return found?.[0];
465
+ }
466
+
467
+ function push(tokens: CodeToken[], kind: TokenKind, text: string): void {
468
+ if (!text) return;
469
+ const last = tokens.at(-1);
470
+ if (last?.kind === "plain" && kind === "plain") {
471
+ tokens[tokens.length - 1] = { kind, text: last.text + text };
472
+ return;
473
+ }
474
+ tokens.push({ kind, text });
475
+ }
@@ -21,6 +21,7 @@ export function installMathRenderer(
21
21
  mathRenderer = renderer;
22
22
  }
23
23
 
24
+ import { highlightCode } from "./highlight.ts";
24
25
  import type {
25
26
  AdapterCompilerOptions,
26
27
  IslandRendererReference,
@@ -365,18 +366,35 @@ function flow(
365
366
  flow(node["children"] as Flow[], diagnostics, block, references),
366
367
  ),
367
368
  ];
368
- case "code":
369
+ case "code": {
370
+ const value = String(node["value"] ?? "");
371
+ const language =
372
+ typeof node["language"] === "string" ? node["language"] : undefined;
373
+ const highlighted = highlightCode(value, language);
374
+ const code = element(
375
+ "code",
376
+ highlighted.className ? { class: highlighted.className } : {},
377
+ highlighted.tokens.map((token) =>
378
+ token.kind === "plain"
379
+ ? text(token.text)
380
+ : element("span", { class: `tok-${token.kind}` }, [
381
+ text(token.text),
382
+ ]),
383
+ ),
384
+ );
369
385
  return [
370
- element("pre", {}, [
371
- element(
372
- "code",
373
- typeof node["language"] === "string"
374
- ? { class: `language-${node["language"]}` }
375
- : {},
376
- [text(String(node["value"] ?? ""))],
377
- ),
386
+ element("pre", highlighted.label ? { class: "publisle-code" } : {}, [
387
+ ...(highlighted.label
388
+ ? [
389
+ element("span", { class: "publisle-code-language" }, [
390
+ text(highlighted.label),
391
+ ]),
392
+ ]
393
+ : []),
394
+ code,
378
395
  ]),
379
396
  ];
397
+ }
380
398
  case "divider":
381
399
  return [element("hr", {}, [])];
382
400
  case "rawHtml":
@@ -740,13 +758,14 @@ function blockNodes(
740
758
  case "publisle:divider":
741
759
  return [element("hr", {}, [])];
742
760
  case "publisle:figure": {
743
- const src = safeUrl(String(data["src"] ?? ""));
761
+ const rawSrc = String(data["src"] ?? "").trim();
762
+ const src = rawSrc ? safeUrl(rawSrc) : undefined;
744
763
  const original = data["original"] as Record<string, unknown> | undefined;
745
764
  const originalSrc =
746
765
  typeof original?.["src"] === "string"
747
766
  ? safeUrl(original["src"])
748
767
  : undefined;
749
- if (!src)
768
+ if (rawSrc && !src)
750
769
  diagnostics.push({
751
770
  level: "warning",
752
771
  code: "unsafe-url",
@@ -783,14 +802,26 @@ function blockNodes(
783
802
  ...(themed ? { class: "publisle-figure-themed" } : {}),
784
803
  },
785
804
  [
786
- element(
787
- "img",
788
- {
789
- ...(src ? { src } : {}),
790
- alt: String(data["alt"] ?? "Figure description missing."),
791
- },
792
- [],
793
- ),
805
+ ...(src
806
+ ? [
807
+ element(
808
+ "img",
809
+ {
810
+ src,
811
+ alt: String(data["alt"] ?? "Figure description missing."),
812
+ },
813
+ [],
814
+ ),
815
+ ]
816
+ : [
817
+ element("p", { class: "publisle-figure-placeholder" }, [
818
+ text(
819
+ rawSrc
820
+ ? "An unsafe figure URL was removed."
821
+ : "Add an image URL",
822
+ ),
823
+ ]),
824
+ ]),
794
825
  ...variants.map(([name, variantSrc]) =>
795
826
  // The base image carries the description; renditions are decorative.
796
827
  element(