@gjsify/site-highlight 0.0.0-stage → 0.64.15-next.287

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 CHANGED
@@ -1,3 +1,91 @@
1
- # Temporary Holding Version
1
+ # @gjsify/site-highlight
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Build-time syntax highlighting for the code blocks of a [@gjsify/site](../site/README.md) blog —
4
+ [Shiki](https://shiki.style) on its JavaScript regex engine, so it runs on GJS, Node, Bun and Deno.
5
+ The colours are in the HTML the build writes, and one rendered block follows the reader's light or
6
+ dark scheme through CSS variables. No client JavaScript, no second HTML copy per scheme.
7
+
8
+ Shiki carries no Blueprint grammar, so this package ships one: `.blp` fences in a widget gallery
9
+ are highlighted like every other language.
10
+
11
+ Part of the [gjsify](https://github.com/gjsify/gjsify) project — Node.js and Web APIs for GJS
12
+ (GNOME JavaScript).
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ gjsify install -D @gjsify/site-highlight
18
+ ```
19
+
20
+ ## Use
21
+
22
+ Install the hook at the site build:
23
+
24
+ ```typescript
25
+ import { buildSite } from '@gjsify/site';
26
+ import { createHighlighter } from '@gjsify/site-highlight';
27
+
28
+ const highlighter = await createHighlighter();
29
+ await buildSite({
30
+ root: process.cwd(),
31
+ out: 'dist',
32
+ markdown: { highlight: (code, lang) => highlighter.highlight(code, lang) },
33
+ });
34
+ ```
35
+
36
+ `createHighlighter()` parses every grammar once, so create it once per build and pass the same
37
+ instance to every render. `highlight(code, lang)` answers Shiki's `<pre class="shiki …">` HTML, or
38
+ `undefined` for a language this package doesn't ship — `@gjsify/site` then falls back to its plain
39
+ `<pre><code>` block instead of pretending the fence was highlighted.
40
+
41
+ The same hook reaches the renderer directly, which is what a caller that renders a single post
42
+ needs:
43
+
44
+ ```typescript
45
+ import { renderPostBody } from '@gjsify/site';
46
+
47
+ const html = renderPostBody(config, post, postsDir, posts, { highlight });
48
+ ```
49
+
50
+ ## The stylesheet
51
+
52
+ The HTML sets `--shiki-light*` and `--shiki-dark*` on every token and no `color`, so it needs
53
+ `highlighter.css` — **appended after the site's own stylesheet**, because a block's background is
54
+ painted by the site's `pre` rule too and the later rule wins:
55
+
56
+ ```typescript
57
+ const stylesheet = `${readFileSync(siteCssPath, 'utf8')}\n${highlighter.css}`;
58
+ ```
59
+
60
+ The sheet colours from `@media (prefers-color-scheme: dark)`, and additionally from
61
+ `[data-theme='dark']` / `[data-theme='light']` for a site whose toggle sets a `data-theme`
62
+ attribute on `<html>`. A site that spells the attribute differently names it:
63
+
64
+ ```typescript
65
+ const highlighter = await createHighlighter({ schemeAttribute: 'data-scheme' });
66
+ ```
67
+
68
+ `highlightCss(attribute)` answers the same stylesheet without loading any grammar. A toggle that
69
+ is a class rather than an attribute needs its own selectors.
70
+
71
+ ## Languages
72
+
73
+ `ts`, `js`, `css`, `scss`, `json`, `sh`, `xml`, `yaml`, `md`, `diff` and `blp` — each with the
74
+ aliases its grammar declares (`typescript`, `javascript`, `bash`, `shell`, `yml`, `markdown`,
75
+ `blueprint`, …). A fence's info string may carry more than the name: ```` ```sh title=build ````
76
+ highlights as `sh`.
77
+
78
+ Extending the set means adding a `@shikijs/langs/*` import to `LANGUAGES` in
79
+ [`src/index.ts`](src/index.ts) — they are static imports so the build bundles only what is listed,
80
+ and so the browser bundle of a consumer does not grow by a grammar it never shows.
81
+
82
+ ## The Blueprint grammar
83
+
84
+ [`grammars/blueprint.tmLanguage.json`](grammars/blueprint.tmLanguage.json) also feeds the
85
+ website's own `.blp` tabs, which read it as a JSON module. It is deliberately a TextMate grammar
86
+ rather than a parser: it names keywords, types, strings and comments, and leaves the structure to
87
+ the compiler. Its `_comment` header records what it covers and what it doesn't.
88
+
89
+ ## License
90
+
91
+ MIT
@@ -0,0 +1,99 @@
1
+ {
2
+ "_comment": [
3
+ "A MINIMAL TextMate grammar for Blueprint (.blp), the GTK UI-definition language.",
4
+ "",
5
+ "Shiki bundles no Blueprint grammar, and a fenced ```blueprint block without one",
6
+ "falls back to plain text — which next to three highlighted sibling tabs reads as",
7
+ "a broken tab rather than a deliberate one.",
8
+ "",
9
+ "DELIBERATELY SMALL. It covers exactly the constructs the widget gallery writes:",
10
+ "imports, a template header, namespaced type names, property assignments, style",
11
+ "classes, strings (including the translatable forms), numbers and comments. It is",
12
+ "not a validator and not a complete grammar — blueprint-compiler is the authority",
13
+ "on the language, and nothing here should be read as a second definition of it.",
14
+ "Anything unhandled simply renders unstyled, which is the correct failure for a",
15
+ "highlighter.",
16
+ "",
17
+ "Reference: https://gnome.pages.gitlab.gnome.org/blueprint-compiler/"
18
+ ],
19
+ "name": "blueprint",
20
+ "aliases": ["blp"],
21
+ "displayName": "Blueprint",
22
+ "scopeName": "source.blueprint",
23
+ "patterns": [
24
+ { "include": "#comment" },
25
+ { "include": "#import" },
26
+ { "include": "#string" },
27
+ { "include": "#keyword" },
28
+ { "include": "#template-target" },
29
+ { "include": "#type" },
30
+ { "include": "#property" },
31
+ { "include": "#constant" }
32
+ ],
33
+ "repository": {
34
+ "comment": {
35
+ "patterns": [
36
+ { "name": "comment.line.double-slash.blueprint", "match": "//.*$" },
37
+ { "name": "comment.block.blueprint", "begin": "/\\*", "end": "\\*/" }
38
+ ]
39
+ },
40
+ "import": {
41
+ "comment": "`using Gtk 4.0;` — the version is a number, not a property value.",
42
+ "match": "\\b(using)\\s+([A-Z][A-Za-z0-9]*)\\s+([0-9]+(?:\\.[0-9]+)?)",
43
+ "captures": {
44
+ "1": { "name": "keyword.control.import.blueprint" },
45
+ "2": { "name": "entity.name.namespace.blueprint" },
46
+ "3": { "name": "constant.numeric.blueprint" }
47
+ }
48
+ },
49
+ "string": {
50
+ "patterns": [
51
+ {
52
+ "comment": "Translatable strings: _(\"…\") and C_(\"context\", \"…\").",
53
+ "match": "\\b(_|C_)(?=\\()",
54
+ "name": "entity.name.function.blueprint"
55
+ },
56
+ {
57
+ "name": "string.quoted.double.blueprint",
58
+ "begin": "\"",
59
+ "end": "\"",
60
+ "patterns": [{ "name": "constant.character.escape.blueprint", "match": "\\\\." }]
61
+ },
62
+ {
63
+ "name": "string.quoted.single.blueprint",
64
+ "begin": "'",
65
+ "end": "'",
66
+ "patterns": [{ "name": "constant.character.escape.blueprint", "match": "\\\\." }]
67
+ }
68
+ ]
69
+ },
70
+ "keyword": {
71
+ "name": "keyword.control.blueprint",
72
+ "match": "\\b(using|template|styles|menu|section|submenu|item|bind|bind-property|after|swapped|inverted|sync-create|no-sync-create|bidirectional)\\b"
73
+ },
74
+ "template-target": {
75
+ "comment": "`template $MainWindow: Adw.ApplicationWindow` — the app's own class.",
76
+ "name": "entity.name.class.blueprint",
77
+ "match": "\\$[A-Za-z_][A-Za-z0-9_]*"
78
+ },
79
+ "type": {
80
+ "comment": "`Adw.HeaderBar`, `Gtk.Box` — a namespaced widget type opening a block.",
81
+ "match": "\\b([A-Z][A-Za-z0-9]*)\\.([A-Z][A-Za-z0-9]*)\\b",
82
+ "captures": {
83
+ "1": { "name": "entity.name.namespace.blueprint" },
84
+ "2": { "name": "entity.name.type.blueprint" }
85
+ }
86
+ },
87
+ "property": {
88
+ "comment": "`title: \"…\";` — an identifier immediately before a colon.",
89
+ "name": "variable.other.property.blueprint",
90
+ "match": "\\b([a-z][A-Za-z0-9_-]*)\\s*(?=:)"
91
+ },
92
+ "constant": {
93
+ "patterns": [
94
+ { "name": "constant.language.blueprint", "match": "\\b(true|false|null)\\b" },
95
+ { "name": "constant.numeric.blueprint", "match": "\\b[0-9]+(?:\\.[0-9]+)?\\b" }
96
+ ]
97
+ }
98
+ }
99
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Build-time syntax highlighting for the code blocks a Markdown page renders — the `highlight` hook
3
+ * `@gjsify/site`'s `renderMarkdown` accepts.
4
+ *
5
+ * Shiki, not a client-side highlighter: the pages of a `@gjsify/site` build work without
6
+ * JavaScript, so the colours have to be in the HTML that leaves the build. Shiki's JavaScript
7
+ * regex engine is what makes that possible on GJS — the WASM (Oniguruma) engine needs a WebAssembly
8
+ * instantiation GJS has no business doing for a static page, and a JS `RegExp` is already there.
9
+ *
10
+ * Two themes and `defaultColor: false`: Shiki writes BOTH palettes into the `--shiki-light*` /
11
+ * `--shiki-dark*` custom properties and sets no `color` of its own, so one rendered block follows
12
+ * the reader's scheme through CSS alone (see {@link HIGHLIGHT_CSS}). Rendering twice, or shipping a
13
+ * second HTML copy per scheme, is the alternative and is strictly more bytes.
14
+ */
15
+ /**
16
+ * The stylesheet {@link CodeHighlighter.highlight}'s HTML needs — without it a block has no colour
17
+ * at all, because `defaultColor: false` deliberately emits none.
18
+ *
19
+ * `pre.shiki` rather than `.shiki`: a consumer stylesheet that paints `pre` (the site's own blog CSS
20
+ * does) ties with a bare class selector, so appending this AFTER that stylesheet is what lets the
21
+ * highlighter's background win, and a later override is still a plain CSS edit away.
22
+ *
23
+ * The attribute selectors exist for sites whose scheme toggle is not the OS preference: a reader
24
+ * who forces `dark` gets it while their OS is light, and one who forces `light` keeps it while
25
+ * their OS is dark. `schemeAttribute` names the attribute the site sets on an ancestor (usually
26
+ * `<html>`) to `light` or `dark`.
27
+ */
28
+ export declare function highlightCss(schemeAttribute?: string): string;
29
+ /** {@link highlightCss} for the default `data-theme` attribute. */
30
+ export declare const HIGHLIGHT_CSS: string;
31
+ /** What {@link createHighlighter} accepts. */
32
+ export interface HighlighterOptions {
33
+ /**
34
+ * The attribute the site's scheme toggle sets to `light` or `dark` on an ancestor of the code
35
+ * blocks. Default `data-theme`.
36
+ */
37
+ schemeAttribute?: string;
38
+ }
39
+ /** What {@link createHighlighter} answers: the hook for `@gjsify/site`, plus the CSS it needs. */
40
+ export interface CodeHighlighter {
41
+ /**
42
+ * Shiki's `<pre class="shiki …">` for a language this package ships, `undefined` for any other
43
+ * — which is what makes `renderMarkdown` fall back to its plain `<pre><code>` block instead of
44
+ * printing an unhighlighted one that pretends to be highlighted.
45
+ *
46
+ * The returned HTML is complete and escaped by Shiki; `renderMarkdown` inserts it as markup.
47
+ */
48
+ highlight(code: string, lang: string): string | undefined;
49
+ /** The stylesheet the HTML needs, to be appended after the site's own. */
50
+ css: string;
51
+ }
52
+ /**
53
+ * Loads the grammars and themes once; keep the result for the whole build. Creating one per block
54
+ * would re-parse every grammar per code block — measurably slower, and it is the only thing this
55
+ * package does that is not free.
56
+ */
57
+ export declare function createHighlighter(options?: HighlighterOptions): Promise<CodeHighlighter>;
package/lib/index.js ADDED
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Build-time syntax highlighting for the code blocks a Markdown page renders — the `highlight` hook
3
+ * `@gjsify/site`'s `renderMarkdown` accepts.
4
+ *
5
+ * Shiki, not a client-side highlighter: the pages of a `@gjsify/site` build work without
6
+ * JavaScript, so the colours have to be in the HTML that leaves the build. Shiki's JavaScript
7
+ * regex engine is what makes that possible on GJS — the WASM (Oniguruma) engine needs a WebAssembly
8
+ * instantiation GJS has no business doing for a static page, and a JS `RegExp` is already there.
9
+ *
10
+ * Two themes and `defaultColor: false`: Shiki writes BOTH palettes into the `--shiki-light*` /
11
+ * `--shiki-dark*` custom properties and sets no `color` of its own, so one rendered block follows
12
+ * the reader's scheme through CSS alone (see {@link HIGHLIGHT_CSS}). Rendering twice, or shipping a
13
+ * second HTML copy per scheme, is the alternative and is strictly more bytes.
14
+ */
15
+ import { createHighlighterCore } from 'shiki/core';
16
+ import { createJavaScriptRegexEngine } from 'shiki/engine/javascript';
17
+ import languageCss from '@shikijs/langs/css';
18
+ import languageDiff from '@shikijs/langs/diff';
19
+ import languageJavascript from '@shikijs/langs/javascript';
20
+ import languageJson from '@shikijs/langs/json';
21
+ import languageMarkdown from '@shikijs/langs/markdown';
22
+ import languageScss from '@shikijs/langs/scss';
23
+ import languageShellscript from '@shikijs/langs/shellscript';
24
+ import languageTypescript from '@shikijs/langs/typescript';
25
+ import languageXml from '@shikijs/langs/xml';
26
+ import languageYaml from '@shikijs/langs/yaml';
27
+ import themeDark from '@shikijs/themes/github-dark';
28
+ import themeLight from '@shikijs/themes/github-light';
29
+ import blueprint from '../grammars/blueprint.tmLanguage.json' with { type: 'json' };
30
+ /** The two palettes, by the names Shiki writes into `--shiki-<scheme>`. */
31
+ const THEMES = { light: 'github-light', dark: 'github-dark' };
32
+ /** Everything this package highlights. An alias (`ts`, `js`, `sh`, `bash`, `yml`, `blp`, …) comes
33
+ * with its grammar, so nothing has to be listed twice. */
34
+ const LANGUAGES = [
35
+ ...languageTypescript,
36
+ ...languageJavascript,
37
+ ...languageCss,
38
+ ...languageScss,
39
+ ...languageJson,
40
+ ...languageShellscript,
41
+ ...languageXml,
42
+ ...languageYaml,
43
+ ...languageMarkdown,
44
+ ...languageDiff,
45
+ // Shiki bundles no Blueprint grammar. `name` is repeated because registration ignores the
46
+ // grammar's own `name` field; the `aliases` (`blp`) travel with the JSON and still apply.
47
+ { ...blueprint, name: 'blueprint' },
48
+ ];
49
+ /**
50
+ * The stylesheet {@link CodeHighlighter.highlight}'s HTML needs — without it a block has no colour
51
+ * at all, because `defaultColor: false` deliberately emits none.
52
+ *
53
+ * `pre.shiki` rather than `.shiki`: a consumer stylesheet that paints `pre` (the site's own blog CSS
54
+ * does) ties with a bare class selector, so appending this AFTER that stylesheet is what lets the
55
+ * highlighter's background win, and a later override is still a plain CSS edit away.
56
+ *
57
+ * The attribute selectors exist for sites whose scheme toggle is not the OS preference: a reader
58
+ * who forces `dark` gets it while their OS is light, and one who forces `light` keeps it while
59
+ * their OS is dark. `schemeAttribute` names the attribute the site sets on an ancestor (usually
60
+ * `<html>`) to `light` or `dark`.
61
+ */
62
+ export function highlightCss(schemeAttribute = 'data-theme') {
63
+ // The name lands in a selector verbatim; a build-time option, but a typo should fail loudly
64
+ // here rather than ship a stylesheet that silently matches nothing.
65
+ if (!/^[a-z_][\w-]*$/i.test(schemeAttribute)) {
66
+ throw new Error(`highlightCss: "${schemeAttribute}" is not an attribute name`);
67
+ }
68
+ const dark = `[${schemeAttribute}='dark']`;
69
+ const light = `[${schemeAttribute}='light']`;
70
+ return `pre.shiki,
71
+ pre.shiki span {
72
+ color: var(--shiki-light);
73
+ background-color: var(--shiki-light-bg);
74
+ }
75
+ @media (prefers-color-scheme: dark) {
76
+ pre.shiki,
77
+ pre.shiki span {
78
+ color: var(--shiki-dark);
79
+ background-color: var(--shiki-dark-bg);
80
+ }
81
+ }
82
+ ${dark} pre.shiki,
83
+ ${dark} pre.shiki span {
84
+ color: var(--shiki-dark);
85
+ background-color: var(--shiki-dark-bg);
86
+ }
87
+ ${light} pre.shiki,
88
+ ${light} pre.shiki span {
89
+ color: var(--shiki-light);
90
+ background-color: var(--shiki-light-bg);
91
+ }
92
+ `;
93
+ }
94
+ /** {@link highlightCss} for the default `data-theme` attribute. */
95
+ export const HIGHLIGHT_CSS = highlightCss();
96
+ /**
97
+ * The Markdown fence's info string, reduced to the language name: ```` ```sh title=build ```` names
98
+ * `sh`. An empty info string answers `''`, which no grammar claims, so the block stays plain.
99
+ */
100
+ function languageName(info) {
101
+ return info.trim().split(/\s+/)[0]?.toLowerCase() ?? '';
102
+ }
103
+ /**
104
+ * Loads the grammars and themes once; keep the result for the whole build. Creating one per block
105
+ * would re-parse every grammar per code block — measurably slower, and it is the only thing this
106
+ * package does that is not free.
107
+ */
108
+ export async function createHighlighter(options = {}) {
109
+ const css = highlightCss(options.schemeAttribute);
110
+ const core = await createHighlighterCore({
111
+ themes: [themeLight, themeDark],
112
+ langs: LANGUAGES,
113
+ engine: createJavaScriptRegexEngine(),
114
+ });
115
+ // `getLoadedLanguages()` lists the grammar names AND their aliases, so one membership test
116
+ // answers for `ts` and `typescript` alike. It is not `getLanguage(name)`: that one THROWS on an
117
+ // unknown name, and a `try`/`catch` here would also swallow a grammar that fails on real code —
118
+ // which is a bug worth failing the build on.
119
+ const known = new Set(core.getLoadedLanguages());
120
+ return {
121
+ highlight(code, lang) {
122
+ const name = languageName(lang);
123
+ if (!known.has(name))
124
+ return undefined;
125
+ return core.codeToHtml(code, { lang: name, themes: THEMES, defaultColor: false });
126
+ },
127
+ css,
128
+ };
129
+ }
package/package.json CHANGED
@@ -1,6 +1,67 @@
1
1
  {
2
- "name": "@gjsify/site-highlight",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@gjsify/site-highlight",
3
+ "version": "0.64.15-next.287",
4
+ "description": "Build-time syntax highlighting for the code blocks of a @gjsify/site blog: Shiki on the JavaScript regex engine, light and dark from CSS variables, no client JavaScript.",
5
+ "type": "module",
6
+ "main": "lib/index.js",
7
+ "module": "lib/index.js",
8
+ "types": "lib/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./lib/index.d.ts",
12
+ "default": "./lib/index.js"
13
+ },
14
+ "./package.json": "./package.json"
15
+ },
16
+ "files": [
17
+ "lib",
18
+ "grammars"
19
+ ],
20
+ "scripts": {
21
+ "clear": "gjsify clear lib tsconfig.tsbuildinfo test.gjs.mjs test.node.mjs",
22
+ "check": "gjsify tsc --noEmit",
23
+ "build": "gjsify tsc -p tsconfig.build.json",
24
+ "build:test": "gjsify run build:test:gjs && gjsify run build:test:node",
25
+ "build:test:gjs": "gjsify build src/test.gjs.mts --app gjs --outfile test.gjs.mjs",
26
+ "build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
27
+ "test": "gjsify run build:test && gjsify run test:node && gjsify run test:gjs",
28
+ "test:gjs": "gjsify run test.gjs.mjs",
29
+ "test:node": "node test.node.mjs",
30
+ "test:bun": "bun test.node.mjs",
31
+ "test:deno": "deno run -A --no-lock --node-modules-dir=none --min-dep-age=0 test.node.mjs",
32
+ "test:cross-runtime": "gjsify run build:test:node && gjsify run test:node && gjsify run test:bun && gjsify run test:deno"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/gjsify/gjsify.git",
37
+ "directory": "packages/infra/site-highlight"
38
+ },
39
+ "bugs": {
40
+ "url": "https://github.com/gjsify/gjsify/issues"
41
+ },
42
+ "homepage": "https://github.com/gjsify/gjsify/tree/main/packages/infra/site-highlight#readme",
43
+ "keywords": [
44
+ "shiki",
45
+ "syntax-highlighting",
46
+ "static-site",
47
+ "markdown",
48
+ "blueprint",
49
+ "gnome"
50
+ ],
51
+ "license": "MIT",
52
+ "dependencies": {
53
+ "@shikijs/langs": "^4.5.0",
54
+ "@shikijs/themes": "^4.5.0",
55
+ "shiki": "^4.5.0"
56
+ },
57
+ "devDependencies": {
58
+ "@gjsify/cli": "0.64.15-next.287",
59
+ "@gjsify/unit": "0.64.15-next.287",
60
+ "@types/node": "^25.9.2",
61
+ "typescript": "^6.0.3"
62
+ },
63
+ "gjsify": {
64
+ "tier": 2
65
+ },
66
+ "gitHead": "5742b282a8a2bc8633dd8384ef436768447f6a83"
67
+ }