@aparte/plugin-shiki 0.2.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +24 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 aparté
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @aparte/plugin-shiki
|
|
2
|
+
|
|
3
|
+
Syntax highlighting for [aparté](https://github.com/apartejs/aparte) via [shiki](https://shiki.style).
|
|
4
|
+
Registers shiki as the highlight provider, backed by a **single lazily-created highlighter** — grammars
|
|
5
|
+
and the theme load on demand and are cached, so you never re-initialise per code block.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @aparte/plugin-shiki @aparte/core shiki
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { setupShikiProvider } from '@aparte/plugin-shiki';
|
|
13
|
+
|
|
14
|
+
await setupShikiProvider({ theme: 'github-dark' });
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`@aparte/core` and `shiki` are **peer dependencies**. `setupShikiProvider` is async — `await` it once at
|
|
18
|
+
startup before rendering highlighted messages.
|
|
19
|
+
|
|
20
|
+
**Bundle**: nothing is eagerly bundled. Each language grammar is dynamically imported from shiki's bundle
|
|
21
|
+
the first time it appears, so you pay only for the languages you actually render. An unknown language
|
|
22
|
+
degrades to plain text instead of throwing.
|
|
23
|
+
|
|
24
|
+
> ESM-only. Part of the aparté monorepo.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type BundledTheme } from 'shiki';
|
|
2
|
+
export interface ShikiProviderOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Theme to render with — a bundled theme name (e.g. `'github-dark'`) or a
|
|
5
|
+
* loaded custom theme name. Default `'github-dark'`.
|
|
6
|
+
*/
|
|
7
|
+
theme?: BundledTheme | string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Register [shiki](https://shiki.style) as aparté's syntax-highlight provider.
|
|
11
|
+
*
|
|
12
|
+
* Backed by a **single, lazily-created** `createHighlighter` instance: the
|
|
13
|
+
* highlighter is built once on first use with only the requested theme, and each
|
|
14
|
+
* language grammar is loaded on demand and cached. This is deliberately NOT the
|
|
15
|
+
* `codeToHtml` shorthand, which can re-initialise a highlighter per call — the
|
|
16
|
+
* cost we control here is why the plugin exists.
|
|
17
|
+
*
|
|
18
|
+
* Bundle note: nothing is eagerly bundled. Grammars/themes are dynamically
|
|
19
|
+
* imported from shiki's bundle the first time they are seen, so the highlighted
|
|
20
|
+
* languages a consumer actually renders are what they pay for.
|
|
21
|
+
*
|
|
22
|
+
* Framework-agnostic — vanilla, no framework imports. Call once at startup.
|
|
23
|
+
*/
|
|
24
|
+
export declare function setupShikiProvider(options?: ShikiProviderOptions): Promise<void>;
|
|
25
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAIH,KAAK,YAAY,EACpB,MAAM,OAAO,CAAC;AAEf,MAAM,WAAW,oBAAoB;IACjC;;;OAGG;IACH,KAAK,CAAC,EAAE,YAAY,GAAG,MAAM,CAAC;CACjC;AAID;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,kBAAkB,CAAC,OAAO,GAAE,oBAAyB,GAAG,OAAO,CAAC,IAAI,CAAC,CA4C1F"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { AparteConfig } from "@aparte/core";
|
|
2
|
+
import { createHighlighter } from "shiki";
|
|
3
|
+
const PLAINTEXT = /* @__PURE__ */ new Set(["text", "plaintext", "txt", "ansi", ""]);
|
|
4
|
+
async function setupShikiProvider(options = {}) {
|
|
5
|
+
const theme = options.theme ?? "github-dark";
|
|
6
|
+
let highlighter = null;
|
|
7
|
+
let creating = null;
|
|
8
|
+
const loadedLangs = /* @__PURE__ */ new Set();
|
|
9
|
+
const getHighlighter = () => {
|
|
10
|
+
if (highlighter) return Promise.resolve(highlighter);
|
|
11
|
+
if (!creating) {
|
|
12
|
+
creating = createHighlighter({ themes: [theme], langs: [] }).then((h) => {
|
|
13
|
+
highlighter = h;
|
|
14
|
+
return h;
|
|
15
|
+
}).catch((err) => {
|
|
16
|
+
creating = null;
|
|
17
|
+
throw err;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return creating;
|
|
21
|
+
};
|
|
22
|
+
AparteConfig.setHighlightProvider(async (code, lang) => {
|
|
23
|
+
const hl = await getHighlighter();
|
|
24
|
+
let language = (lang || "text").toLowerCase();
|
|
25
|
+
if (!PLAINTEXT.has(language) && !loadedLangs.has(language)) {
|
|
26
|
+
try {
|
|
27
|
+
await hl.loadLanguage(language);
|
|
28
|
+
loadedLangs.add(language);
|
|
29
|
+
} catch {
|
|
30
|
+
language = "text";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return hl.codeToHtml(code, { lang: language, theme });
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
export {
|
|
37
|
+
setupShikiProvider
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { AparteConfig } from '@aparte/core';\nimport {\n createHighlighter,\n type Highlighter,\n type BundledLanguage,\n type BundledTheme,\n} from 'shiki';\n\nexport interface ShikiProviderOptions {\n /**\n * Theme to render with — a bundled theme name (e.g. `'github-dark'`) or a\n * loaded custom theme name. Default `'github-dark'`.\n */\n theme?: BundledTheme | string;\n}\n\nconst PLAINTEXT = new Set(['text', 'plaintext', 'txt', 'ansi', '']);\n\n/**\n * Register [shiki](https://shiki.style) as aparté's syntax-highlight provider.\n *\n * Backed by a **single, lazily-created** `createHighlighter` instance: the\n * highlighter is built once on first use with only the requested theme, and each\n * language grammar is loaded on demand and cached. This is deliberately NOT the\n * `codeToHtml` shorthand, which can re-initialise a highlighter per call — the\n * cost we control here is why the plugin exists.\n *\n * Bundle note: nothing is eagerly bundled. Grammars/themes are dynamically\n * imported from shiki's bundle the first time they are seen, so the highlighted\n * languages a consumer actually renders are what they pay for.\n *\n * Framework-agnostic — vanilla, no framework imports. Call once at startup.\n */\nexport async function setupShikiProvider(options: ShikiProviderOptions = {}): Promise<void> {\n const theme = (options.theme ?? 'github-dark') as BundledTheme;\n\n let highlighter: Highlighter | null = null;\n let creating: Promise<Highlighter> | null = null;\n const loadedLangs = new Set<string>();\n\n const getHighlighter = (): Promise<Highlighter> => {\n if (highlighter) return Promise.resolve(highlighter);\n // De-dupe concurrent first calls so we only ever create one instance.\n if (!creating) {\n creating = createHighlighter({ themes: [theme], langs: [] })\n .then((h) => {\n highlighter = h;\n return h;\n })\n .catch((err) => {\n // A transient failure (network/CSP on the dynamic theme import,\n // wasm engine hiccup) must NOT poison the singleton: clear the\n // cached promise so the next code block retries creation instead\n // of re-returning a permanently-rejected promise. Re-throw so this\n // block still degrades to plaintext via the provider's caller.\n creating = null;\n throw err;\n });\n }\n return creating;\n };\n\n AparteConfig.setHighlightProvider(async (code, lang) => {\n const hl = await getHighlighter();\n let language = (lang || 'text').toLowerCase();\n\n if (!PLAINTEXT.has(language) && !loadedLangs.has(language)) {\n try {\n await hl.loadLanguage(language as BundledLanguage);\n loadedLangs.add(language);\n } catch {\n language = 'text'; // unknown grammar → render as plain text\n }\n }\n\n return hl.codeToHtml(code, { lang: language, theme });\n });\n}\n"],"names":[],"mappings":";;AAgBA,MAAM,gCAAgB,IAAI,CAAC,QAAQ,aAAa,OAAO,QAAQ,EAAE,CAAC;AAiBlE,eAAsB,mBAAmB,UAAgC,IAAmB;AACxF,QAAM,QAAS,QAAQ,SAAS;AAEhC,MAAI,cAAkC;AACtC,MAAI,WAAwC;AAC5C,QAAM,kCAAkB,IAAA;AAExB,QAAM,iBAAiB,MAA4B;AAC/C,QAAI,YAAa,QAAO,QAAQ,QAAQ,WAAW;AAEnD,QAAI,CAAC,UAAU;AACX,iBAAW,kBAAkB,EAAE,QAAQ,CAAC,KAAK,GAAG,OAAO,GAAC,CAAG,EACtD,KAAK,CAAC,MAAM;AACT,sBAAc;AACd,eAAO;AAAA,MACX,CAAC,EACA,MAAM,CAAC,QAAQ;AAMZ,mBAAW;AACX,cAAM;AAAA,MACV,CAAC;AAAA,IACT;AACA,WAAO;AAAA,EACX;AAEA,eAAa,qBAAqB,OAAO,MAAM,SAAS;AACpD,UAAM,KAAK,MAAM,eAAA;AACjB,QAAI,YAAY,QAAQ,QAAQ,YAAA;AAEhC,QAAI,CAAC,UAAU,IAAI,QAAQ,KAAK,CAAC,YAAY,IAAI,QAAQ,GAAG;AACxD,UAAI;AACA,cAAM,GAAG,aAAa,QAA2B;AACjD,oBAAY,IAAI,QAAQ;AAAA,MAC5B,QAAQ;AACJ,mBAAW;AAAA,MACf;AAAA,IACJ;AAEA,WAAO,GAAG,WAAW,MAAM,EAAE,MAAM,UAAU,OAAO;AAAA,EACxD,CAAC;AACL;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aparte/plugin-shiki",
|
|
3
|
+
"version": "0.2.0-alpha.0",
|
|
4
|
+
"description": "Syntax highlighting for aparté via shiki — register it as the highlight provider (single lazily-created highlighter).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"@aparte-workspace/source": "./src/index.ts",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE"
|
|
22
|
+
],
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=18"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"shiki": ">=1.0.0",
|
|
28
|
+
"@aparte/core": "0.2.0-alpha.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"shiki": "^4.0.0",
|
|
32
|
+
"typescript": "^5.4.0",
|
|
33
|
+
"vite": "^6.0.0",
|
|
34
|
+
"vite-plugin-dts": "^4.5.4",
|
|
35
|
+
"@aparte/core": "0.2.0-alpha.0"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"aparte",
|
|
39
|
+
"plugin",
|
|
40
|
+
"highlight",
|
|
41
|
+
"syntax",
|
|
42
|
+
"shiki"
|
|
43
|
+
],
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "git+https://github.com/apartejs/aparte.git",
|
|
48
|
+
"directory": "packages/plugins/shiki"
|
|
49
|
+
},
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/apartejs/aparte/issues"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"dev": "vite",
|
|
55
|
+
"build": "vite build && tsc -b --emitDeclarationOnly --force",
|
|
56
|
+
"preview": "vite preview",
|
|
57
|
+
"test": "vitest",
|
|
58
|
+
"test:run": "vitest run",
|
|
59
|
+
"test:coverage": "vitest run --coverage"
|
|
60
|
+
}
|
|
61
|
+
}
|