@ugoku/shiki 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/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +26 -2
- package/package.json +23 -3
- package/src/hash.test.ts +14 -0
- package/src/hash.ts +7 -0
- package/src/index.ts +1 -0
- package/src/mode.test.ts +32 -0
- package/src/mode.ts +8 -0
- package/src/transformer.test.ts +113 -0
- package/src/transformer.ts +28 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 David Skewis
|
|
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
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ugoku/shiki
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The build-time [Shiki](https://shiki.style/) transformer for [ugoku](https://github.com/ugokujs/ugoku). Reads a code fence's meta string and, for `run`/`run edit` blocks, stamps the rendered `<pre>` with data attributes `@ugoku/core` reads at runtime (mode, language, theme(s), source hash). Blocks without `run` are left untouched.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { ugokuTransformer } from '@ugoku/shiki';
|
|
9
|
+
|
|
10
|
+
const transformer = ugokuTransformer();
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Pass it wherever your Shiki setup accepts transformers — e.g. VitePress's `markdown.codeTransformers`, or Shiki's own `codeToHtml({ transformers: [...] })`.
|
|
14
|
+
|
|
15
|
+
In markdown, add `run` (run-only) or `run edit` (editable) to the fence info string, alongside whatever else your pipeline already parses there (line numbers, filenames, etc.):
|
|
16
|
+
|
|
17
|
+
````md
|
|
18
|
+
```js run
|
|
19
|
+
console.log('hello');
|
|
20
|
+
```
|
|
21
|
+
````
|
|
22
|
+
|
|
23
|
+
## Who should use this directly
|
|
24
|
+
|
|
25
|
+
If you're using the VitePress or Astro adapter, it already wires this in for you — see [`@ugoku/vitepress`](https://www.npmjs.com/package/@ugoku/vitepress) or [`@ugoku/astro`](https://www.npmjs.com/package/@ugoku/astro). Use this package directly only if you're building a custom Shiki integration.
|
|
26
|
+
|
|
27
|
+
See the [main README](https://github.com/ugokujs/ugoku#readme) for the full picture.
|
package/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ugoku/shiki",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/ugokujs/ugoku.git",
|
|
8
|
+
"directory": "packages/shiki"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "src/index.ts",
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"shiki": "^4.0.0"
|
|
17
|
+
},
|
|
18
|
+
"peerDependenciesMeta": {
|
|
19
|
+
"shiki": {
|
|
20
|
+
"optional": true
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"shiki": "^4.5.0"
|
|
25
|
+
}
|
|
6
26
|
}
|
package/src/hash.test.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { expect, test } from 'vitest';
|
|
2
|
+
import { hashSource } from './hash';
|
|
3
|
+
|
|
4
|
+
test('produces a 10-character lowercase hex string', () => {
|
|
5
|
+
expect(hashSource('console.log(1)')).toMatch(/^[0-9a-f]{10}$/);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
test('is deterministic for the same source', () => {
|
|
9
|
+
expect(hashSource('const x = 1;')).toBe(hashSource('const x = 1;'));
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test('changes when the source changes', () => {
|
|
13
|
+
expect(hashSource('const x = 1;')).not.toBe(hashSource('const x = 2;'));
|
|
14
|
+
});
|
package/src/hash.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ugokuTransformer } from './transformer.ts';
|
package/src/mode.test.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { expect, test } from 'vitest';
|
|
2
|
+
import { resolveMode } from './mode';
|
|
3
|
+
|
|
4
|
+
test('returns null when run is absent', () => {
|
|
5
|
+
expect(resolveMode(undefined)).toBeNull();
|
|
6
|
+
expect(resolveMode('')).toBeNull();
|
|
7
|
+
expect(resolveMode('{1,3}')).toBeNull();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test('returns run when run is present without edit', () => {
|
|
11
|
+
expect(resolveMode('run')).toBe('run');
|
|
12
|
+
expect(resolveMode('run {1,3}')).toBe('run');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('returns run-edit when both run and edit are present', () => {
|
|
16
|
+
expect(resolveMode('run edit')).toBe('run-edit');
|
|
17
|
+
expect(resolveMode('edit run')).toBe('run-edit');
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('tokens coexist with other meta flags regardless of position', () => {
|
|
21
|
+
expect(resolveMode('{1,3} run :line-numbers edit [file.ts]')).toBe('run-edit');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('downgrades to run when twoslash is also present', () => {
|
|
25
|
+
expect(resolveMode('run edit twoslash')).toBe('run');
|
|
26
|
+
expect(resolveMode('twoslash run edit')).toBe('run');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('does not match run as a substring of another token', () => {
|
|
30
|
+
expect(resolveMode('run-something')).toBeNull();
|
|
31
|
+
expect(resolveMode('rerun')).toBeNull();
|
|
32
|
+
});
|
package/src/mode.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type UgokuMode = 'run' | 'run-edit';
|
|
2
|
+
|
|
3
|
+
export function resolveMode(rawMeta: string | undefined): UgokuMode | null {
|
|
4
|
+
const tokens = new Set((rawMeta ?? '').split(/\s+/).filter(Boolean));
|
|
5
|
+
if (!tokens.has('run')) return null;
|
|
6
|
+
if (tokens.has('edit') && !tokens.has('twoslash')) return 'run-edit';
|
|
7
|
+
return 'run';
|
|
8
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { expect, test } from 'vitest';
|
|
2
|
+
import { codeToHtml } from 'shiki';
|
|
3
|
+
import type { ShikiTransformer } from 'shiki';
|
|
4
|
+
import { ugokuTransformer } from './transformer';
|
|
5
|
+
|
|
6
|
+
const theme = 'github-light';
|
|
7
|
+
|
|
8
|
+
test('plain fence without run is left byte-identical', async () => {
|
|
9
|
+
const options = { lang: 'js' as const, theme, meta: { __raw: '{1,3}' } };
|
|
10
|
+
const withTransformer = await codeToHtml('const x = 1;', { ...options, transformers: [ugokuTransformer()] });
|
|
11
|
+
const withoutTransformer = await codeToHtml('const x = 1;', options);
|
|
12
|
+
expect(withTransformer).toBe(withoutTransformer);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('run stamps data-ugoku-mode, lang, theme, and hash', async () => {
|
|
16
|
+
const html = await codeToHtml('const x = 1;', {
|
|
17
|
+
lang: 'js',
|
|
18
|
+
theme,
|
|
19
|
+
meta: { __raw: 'run' },
|
|
20
|
+
transformers: [ugokuTransformer()],
|
|
21
|
+
});
|
|
22
|
+
expect(html).toContain('data-ugoku-mode="run"');
|
|
23
|
+
expect(html).toContain('data-ugoku-lang="js"');
|
|
24
|
+
expect(html).toContain('data-ugoku-theme="github-light"');
|
|
25
|
+
expect(html).toMatch(/data-ugoku-hash="[0-9a-f]{10}"/);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('run edit stamps run-edit', async () => {
|
|
29
|
+
const html = await codeToHtml('const x = 1;', {
|
|
30
|
+
lang: 'ts',
|
|
31
|
+
theme,
|
|
32
|
+
meta: { __raw: 'run edit' },
|
|
33
|
+
transformers: [ugokuTransformer()],
|
|
34
|
+
});
|
|
35
|
+
expect(html).toContain('data-ugoku-mode="run-edit"');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('run and edit coexist with other meta tokens regardless of order', async () => {
|
|
39
|
+
const html = await codeToHtml('const x = 1;', {
|
|
40
|
+
lang: 'ts',
|
|
41
|
+
theme,
|
|
42
|
+
meta: { __raw: '{1,3} run :line-numbers edit [file.ts]' },
|
|
43
|
+
transformers: [ugokuTransformer()],
|
|
44
|
+
});
|
|
45
|
+
expect(html).toContain('data-ugoku-mode="run-edit"');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('does not interfere with another transformer reading its own meta token', async () => {
|
|
49
|
+
const stub: ShikiTransformer = {
|
|
50
|
+
name: 'stub',
|
|
51
|
+
pre(hast) {
|
|
52
|
+
if (this.options.meta?.__raw?.includes(':line-numbers')) {
|
|
53
|
+
hast.properties['data-stub-line-numbers'] = 'true';
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
const html = await codeToHtml('const x = 1;', {
|
|
58
|
+
lang: 'ts',
|
|
59
|
+
theme,
|
|
60
|
+
meta: { __raw: 'run :line-numbers' },
|
|
61
|
+
transformers: [ugokuTransformer(), stub],
|
|
62
|
+
});
|
|
63
|
+
expect(html).toContain('data-ugoku-mode="run"');
|
|
64
|
+
expect(html).toContain('data-stub-line-numbers="true"');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('run edit downgrades to run when twoslash is present', async () => {
|
|
68
|
+
const html = await codeToHtml('const x = 1;', {
|
|
69
|
+
lang: 'ts',
|
|
70
|
+
theme,
|
|
71
|
+
meta: { __raw: 'run edit twoslash' },
|
|
72
|
+
transformers: [ugokuTransformer()],
|
|
73
|
+
});
|
|
74
|
+
expect(html).toContain('data-ugoku-mode="run"');
|
|
75
|
+
expect(html).not.toContain('data-ugoku-mode="run-edit"');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('dual-theme config stamps light and dark theme attributes', async () => {
|
|
79
|
+
const html = await codeToHtml('const x = 1;', {
|
|
80
|
+
lang: 'js',
|
|
81
|
+
themes: { light: 'github-light', dark: 'github-dark' },
|
|
82
|
+
meta: { __raw: 'run' },
|
|
83
|
+
transformers: [ugokuTransformer()],
|
|
84
|
+
});
|
|
85
|
+
expect(html).toContain('data-ugoku-theme-light="github-light"');
|
|
86
|
+
expect(html).toContain('data-ugoku-theme-dark="github-dark"');
|
|
87
|
+
expect(html).not.toContain('data-ugoku-theme=');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('single-theme config stamps one theme attribute', async () => {
|
|
91
|
+
const html = await codeToHtml('const x = 1;', {
|
|
92
|
+
lang: 'js',
|
|
93
|
+
theme,
|
|
94
|
+
meta: { __raw: 'run' },
|
|
95
|
+
transformers: [ugokuTransformer()],
|
|
96
|
+
});
|
|
97
|
+
expect(html).toContain('data-ugoku-theme="github-light"');
|
|
98
|
+
expect(html).not.toContain('data-ugoku-theme-light=');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('hash is deterministic for identical source and differs when source changes', async () => {
|
|
102
|
+
const makeHtml = (code: string) =>
|
|
103
|
+
codeToHtml(code, { lang: 'js', theme, meta: { __raw: 'run' }, transformers: [ugokuTransformer()] });
|
|
104
|
+
|
|
105
|
+
const first = await makeHtml('const x = 1;');
|
|
106
|
+
const same = await makeHtml('const x = 1;');
|
|
107
|
+
const different = await makeHtml('const x = 2;');
|
|
108
|
+
|
|
109
|
+
const extractHash = (html: string) => html.match(/data-ugoku-hash="([0-9a-f]{10})"/)?.[1];
|
|
110
|
+
|
|
111
|
+
expect(extractHash(first)).toBe(extractHash(same));
|
|
112
|
+
expect(extractHash(first)).not.toBe(extractHash(different));
|
|
113
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ShikiTransformer } from 'shiki';
|
|
2
|
+
import { resolveMode } from './mode.ts';
|
|
3
|
+
import { hashSource } from './hash.ts';
|
|
4
|
+
|
|
5
|
+
export function ugokuTransformer(): ShikiTransformer {
|
|
6
|
+
return {
|
|
7
|
+
name: 'ugoku',
|
|
8
|
+
pre(hast) {
|
|
9
|
+
const mode = resolveMode(this.options.meta?.__raw);
|
|
10
|
+
if (!mode) return;
|
|
11
|
+
|
|
12
|
+
hast.properties['data-ugoku-mode'] = mode;
|
|
13
|
+
hast.properties['data-ugoku-lang'] = this.options.lang;
|
|
14
|
+
hast.properties['data-ugoku-hash'] = hashSource(this.source);
|
|
15
|
+
|
|
16
|
+
// theme/themes can technically hold a full registration object, not just
|
|
17
|
+
// a name — but every real config (VitePress included) references themes
|
|
18
|
+
// by their string name.
|
|
19
|
+
if ('theme' in this.options && this.options.theme) {
|
|
20
|
+
hast.properties['data-ugoku-theme'] = this.options.theme as string;
|
|
21
|
+
} else if ('themes' in this.options && this.options.themes) {
|
|
22
|
+
const { light, dark } = this.options.themes as Record<string, string>;
|
|
23
|
+
if (light) hast.properties['data-ugoku-theme-light'] = light;
|
|
24
|
+
if (dark) hast.properties['data-ugoku-theme-dark'] = dark;
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|