@ugoku/astro 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 +10 -0
- package/LICENSE +21 -0
- package/README.md +41 -2
- package/package.json +23 -3
- package/src/client.test.ts +87 -0
- package/src/client.ts +23 -0
- package/src/index.ts +1 -0
- package/src/integration.test.ts +42 -0
- package/src/integration.ts +20 -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,42 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ugoku/astro
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Astro adapter for [ugoku](https://github.com/ugokujs/ugoku): runnable, editable code snippets for Shiki-highlighted fences, with no server and no DOM/iframe — console output only.
|
|
4
|
+
|
|
5
|
+
A single integration auto-wires both the `@ugoku/shiki` transformer and the page script, for plain Astro's MPA navigation and `<ClientRouter />` transitions alike. (This is for plain Astro — Astro Starlight replaces Shiki with its own highlighter and needs a separate adapter.)
|
|
6
|
+
|
|
7
|
+
## Setup
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
// astro.config.mjs
|
|
11
|
+
import { defineConfig } from 'astro/config';
|
|
12
|
+
import { ugoku } from '@ugoku/astro';
|
|
13
|
+
|
|
14
|
+
export default defineConfig({
|
|
15
|
+
integrations: [ugoku()],
|
|
16
|
+
});
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
That's it — no second manual step, no theme file to edit.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Add `run` or `run edit` to a fence's info string:
|
|
24
|
+
|
|
25
|
+
````md
|
|
26
|
+
```js run
|
|
27
|
+
console.log('hello');
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```ts run edit
|
|
31
|
+
function add(a: number, b: number): number {
|
|
32
|
+
return a + b;
|
|
33
|
+
}
|
|
34
|
+
console.log(add(2, 3));
|
|
35
|
+
```
|
|
36
|
+
````
|
|
37
|
+
|
|
38
|
+
`run` is run-only; `run edit` additionally shows an editable overlay over the highlighted block, with edits persisted to `localStorage` per snippet.
|
|
39
|
+
|
|
40
|
+
TypeScript blocks (`ts`) have their type annotations stripped in-browser (no type-checking) before execution.
|
|
41
|
+
|
|
42
|
+
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/astro",
|
|
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/astro"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "src/index.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./src/index.ts",
|
|
17
|
+
"./client": "./src/client.ts"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@ugoku/core": "0.1.1",
|
|
21
|
+
"@ugoku/shiki": "0.1.1"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"astro": "^5.0.0"
|
|
25
|
+
}
|
|
6
26
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
// no static top-level import of '@ugoku/core' — must be dynamic, see beforeEach
|
|
4
|
+
|
|
5
|
+
interface FakeLink {
|
|
6
|
+
rel: string;
|
|
7
|
+
href: string;
|
|
8
|
+
dataset: Record<string, string>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface FakeDocument {
|
|
12
|
+
body: object;
|
|
13
|
+
head: {
|
|
14
|
+
querySelector: (selector: string) => FakeLink | null;
|
|
15
|
+
appendChild: (node: FakeLink) => void;
|
|
16
|
+
};
|
|
17
|
+
createElement: (tag: string) => FakeLink;
|
|
18
|
+
addEventListener: (type: string, listener: () => void) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function stubDocument(): { fakeDocument: FakeDocument; listeners: Record<string, () => void> } {
|
|
22
|
+
const listeners: Record<string, () => void> = {};
|
|
23
|
+
let existingLink: FakeLink | null = null;
|
|
24
|
+
const fakeDocument: FakeDocument = {
|
|
25
|
+
body: {},
|
|
26
|
+
head: {
|
|
27
|
+
querySelector: (selector) => (selector === 'link[data-ugoku-styles]' ? existingLink : null),
|
|
28
|
+
appendChild: (node) => {
|
|
29
|
+
existingLink = node;
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
createElement: () => ({ rel: '', href: '', dataset: {} }),
|
|
33
|
+
addEventListener: (type, listener) => {
|
|
34
|
+
listeners[type] = listener;
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
(globalThis as { document?: FakeDocument }).document = fakeDocument;
|
|
38
|
+
return { fakeDocument, listeners };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
describe('@ugoku/astro client', () => {
|
|
42
|
+
let mountSpy: ReturnType<typeof vi.spyOn>;
|
|
43
|
+
let unmountSpy: ReturnType<typeof vi.spyOn>;
|
|
44
|
+
|
|
45
|
+
beforeEach(async () => {
|
|
46
|
+
vi.resetModules();
|
|
47
|
+
const core = await import('@ugoku/core');
|
|
48
|
+
mountSpy = vi.spyOn(core, 'mount').mockImplementation(() => {});
|
|
49
|
+
unmountSpy = vi.spyOn(core, 'unmount').mockImplementation(() => {});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
afterEach(() => {
|
|
53
|
+
mountSpy.mockRestore();
|
|
54
|
+
unmountSpy.mockRestore();
|
|
55
|
+
delete (globalThis as { document?: unknown }).document;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('mounts synchronously when the module is imported', async () => {
|
|
59
|
+
const { fakeDocument } = stubDocument();
|
|
60
|
+
|
|
61
|
+
await import('./client');
|
|
62
|
+
|
|
63
|
+
expect(mountSpy).toHaveBeenCalledTimes(1);
|
|
64
|
+
expect(mountSpy).toHaveBeenCalledWith(fakeDocument.body);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('mounts again when astro:page-load fires', async () => {
|
|
68
|
+
const { fakeDocument, listeners } = stubDocument();
|
|
69
|
+
await import('./client');
|
|
70
|
+
mountSpy.mockClear();
|
|
71
|
+
|
|
72
|
+
listeners['astro:page-load']?.();
|
|
73
|
+
|
|
74
|
+
expect(mountSpy).toHaveBeenCalledTimes(1);
|
|
75
|
+
expect(mountSpy).toHaveBeenCalledWith(fakeDocument.body);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('unmounts when astro:before-swap fires', async () => {
|
|
79
|
+
const { fakeDocument, listeners } = stubDocument();
|
|
80
|
+
await import('./client');
|
|
81
|
+
|
|
82
|
+
listeners['astro:before-swap']?.();
|
|
83
|
+
|
|
84
|
+
expect(unmountSpy).toHaveBeenCalledTimes(1);
|
|
85
|
+
expect(unmountSpy).toHaveBeenCalledWith(fakeDocument.body);
|
|
86
|
+
});
|
|
87
|
+
});
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { mount, unmount } from '@ugoku/core';
|
|
2
|
+
import stylesUrl from '@ugoku/core/styles.css?url';
|
|
3
|
+
|
|
4
|
+
function ensureStyles() {
|
|
5
|
+
if (document.head.querySelector('link[data-ugoku-styles]')) return;
|
|
6
|
+
const link = document.createElement('link');
|
|
7
|
+
link.rel = 'stylesheet';
|
|
8
|
+
link.href = stylesUrl;
|
|
9
|
+
link.dataset.ugokuStyles = 'true';
|
|
10
|
+
document.head.appendChild(link);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
ensureStyles();
|
|
14
|
+
mount(document.body);
|
|
15
|
+
|
|
16
|
+
document.addEventListener('astro:page-load', () => {
|
|
17
|
+
ensureStyles();
|
|
18
|
+
mount(document.body);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
document.addEventListener('astro:before-swap', () => {
|
|
22
|
+
unmount(document.body);
|
|
23
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ugoku } from './integration.ts';
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import type { AstroIntegration } from 'astro';
|
|
3
|
+
import { ugoku } from './integration';
|
|
4
|
+
|
|
5
|
+
type SetupHookArgs = Pick<
|
|
6
|
+
Parameters<NonNullable<AstroIntegration['hooks']['astro:config:setup']>>[0],
|
|
7
|
+
'updateConfig' | 'injectScript'
|
|
8
|
+
>;
|
|
9
|
+
|
|
10
|
+
function runSetupHook(integration: AstroIntegration, args: SetupHookArgs): void {
|
|
11
|
+
const setup = integration.hooks['astro:config:setup'];
|
|
12
|
+
if (!setup) throw new Error('astro:config:setup hook is missing');
|
|
13
|
+
setup(args as Parameters<NonNullable<AstroIntegration['hooks']['astro:config:setup']>>[0]);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
describe('ugoku (Astro integration)', () => {
|
|
17
|
+
it('has the expected integration name', () => {
|
|
18
|
+
expect(ugoku().name).toBe('@ugoku/astro');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('registers the ugoku Shiki transformer in markdown.shikiConfig.transformers', () => {
|
|
22
|
+
const updateConfig = vi.fn();
|
|
23
|
+
const injectScript = vi.fn();
|
|
24
|
+
|
|
25
|
+
runSetupHook(ugoku(), { updateConfig, injectScript });
|
|
26
|
+
|
|
27
|
+
expect(updateConfig).toHaveBeenCalledTimes(1);
|
|
28
|
+
const [config] = updateConfig.mock.calls[0];
|
|
29
|
+
const transformers = config.markdown.shikiConfig.transformers;
|
|
30
|
+
expect(transformers).toHaveLength(1);
|
|
31
|
+
expect(transformers[0].name).toBe('ugoku');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('injects the client mount script at the page stage', () => {
|
|
35
|
+
const updateConfig = vi.fn();
|
|
36
|
+
const injectScript = vi.fn();
|
|
37
|
+
|
|
38
|
+
runSetupHook(ugoku(), { updateConfig, injectScript });
|
|
39
|
+
|
|
40
|
+
expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('@ugoku/astro/client'));
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AstroIntegration } from 'astro';
|
|
2
|
+
import { ugokuTransformer } from '@ugoku/shiki';
|
|
3
|
+
|
|
4
|
+
export function ugoku(): AstroIntegration {
|
|
5
|
+
return {
|
|
6
|
+
name: '@ugoku/astro',
|
|
7
|
+
hooks: {
|
|
8
|
+
'astro:config:setup': ({ updateConfig, injectScript }) => {
|
|
9
|
+
updateConfig({
|
|
10
|
+
markdown: {
|
|
11
|
+
shikiConfig: {
|
|
12
|
+
transformers: [ugokuTransformer()],
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
injectScript('page', "import '@ugoku/astro/client';");
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
}
|