@markdy/astro 0.1.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 +77 -0
- package/package.json +45 -0
- package/src/Markdy.astro +134 -0
- package/src/index.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hoang Yell (https://hoangyell.com)
|
|
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,77 @@
|
|
|
1
|
+
# @markdy/astro
|
|
2
|
+
|
|
3
|
+
[Astro](https://astro.build/) island component for [MarkdyScript](../../docs/SYNTAX.md) animated scenes.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **SSR placeholder** — correctly-sized `<div>` prevents layout shift before hydration
|
|
8
|
+
- **Viewport-triggered hydration** — `IntersectionObserver` with 100px root margin
|
|
9
|
+
- **View Transition compatible** — re-observes elements on `astro:page-load`
|
|
10
|
+
- **Zero config** — pass your MarkdyScript code as a prop
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
pnpm add @markdy/astro
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```astro
|
|
21
|
+
---
|
|
22
|
+
import { Markdy } from "@markdy/astro";
|
|
23
|
+
|
|
24
|
+
const code = `
|
|
25
|
+
scene width=800 height=400 bg=#fff5f9
|
|
26
|
+
actor hero = figure(#c68642, m, 😎) at (400, 200)
|
|
27
|
+
@0.0: hero.enter(from=left, dur=0.8)
|
|
28
|
+
@1.5: hero.say("Hello!", dur=1.2)
|
|
29
|
+
`;
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
<Markdy code={code} width={800} height={400} bg="#fff5f9" autoplay />
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### In MDX
|
|
36
|
+
|
|
37
|
+
```mdx
|
|
38
|
+
import { Markdy } from "@markdy/astro";
|
|
39
|
+
|
|
40
|
+
export const code = `
|
|
41
|
+
scene width=600 height=300 bg=white
|
|
42
|
+
actor label = text("Hello") at (200, 130) size 40 opacity 0
|
|
43
|
+
@0.3: label.fade_in(dur=0.6)
|
|
44
|
+
`;
|
|
45
|
+
|
|
46
|
+
<Markdy code={code} width={600} height={300} autoplay />
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Props
|
|
50
|
+
|
|
51
|
+
| Prop | Type | Default | Description |
|
|
52
|
+
|---|---|---|---|
|
|
53
|
+
| `code` | `string` | *(required)* | MarkdyScript source code |
|
|
54
|
+
| `width` | `number` | `800` | Placeholder width in pixels |
|
|
55
|
+
| `height` | `number` | `400` | Placeholder height in pixels |
|
|
56
|
+
| `bg` | `string` | `"white"` | Placeholder background colour |
|
|
57
|
+
| `assets` | `Record<string, string>` | `{}` | Asset URL overrides |
|
|
58
|
+
| `autoplay` | `boolean` | `true` | Start playing on hydration |
|
|
59
|
+
| `class` | `string` | — | CSS class for the outer wrapper |
|
|
60
|
+
|
|
61
|
+
> **Tip:** Match `width`, `height`, and `bg` props to your `scene` declaration values to avoid a visual flash on hydration.
|
|
62
|
+
|
|
63
|
+
## How It Works
|
|
64
|
+
|
|
65
|
+
1. **Server:** renders a sized placeholder `<div>` with a `▶ markdy` label
|
|
66
|
+
2. **Client:** an `IntersectionObserver` watches all `.markdy-root` elements
|
|
67
|
+
3. **On viewport entry:** the observer fires, clears the placeholder, and calls `createPlayer()` from `@markdy/renderer-dom`
|
|
68
|
+
4. **View Transitions:** listens for `astro:page-load` to re-observe newly added elements
|
|
69
|
+
|
|
70
|
+
## Documentation
|
|
71
|
+
|
|
72
|
+
- **[Tutorial](../../docs/TUTORIAL.md)** — step-by-step guide from zero to animated scenes
|
|
73
|
+
- **[Syntax Reference](../../docs/SYNTAX.md)** — complete DSL language spec
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
[MIT](../../LICENSE)
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@markdy/astro",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Astro island component for MarkdyScript animations.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"files": [
|
|
8
|
+
"src",
|
|
9
|
+
"README.md",
|
|
10
|
+
"LICENSE"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./src/index.ts"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"markdy",
|
|
17
|
+
"astro",
|
|
18
|
+
"animation",
|
|
19
|
+
"island",
|
|
20
|
+
"component"
|
|
21
|
+
],
|
|
22
|
+
"author": "Hoang Yell <hoangyell@gmail.com> (https://hoangyell.com)",
|
|
23
|
+
"homepage": "https://markdy.com",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/HoangYell/markdy-com.git",
|
|
27
|
+
"directory": "packages/astro"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/HoangYell/markdy-com/issues"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@markdy/renderer-dom": "0.1.0"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"astro": ">=4.0.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"astro": "^5.0.0",
|
|
43
|
+
"typescript": "^5.4.0"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/Markdy.astro
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* @markdy/astro — <Markdy /> island component
|
|
4
|
+
*
|
|
5
|
+
* SSR: renders a correctly-sized placeholder div so layout is stable.
|
|
6
|
+
* Client: hydrates via IntersectionObserver when the element enters the
|
|
7
|
+
* viewport (equivalent to client:visible behaviour).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export interface Props {
|
|
11
|
+
/** MarkdyScript source code. */
|
|
12
|
+
code: string;
|
|
13
|
+
/**
|
|
14
|
+
* Placeholder width in pixels (should match the `width` in the scene
|
|
15
|
+
* declaration). Used only for the SSR placeholder — the renderer reads
|
|
16
|
+
* the actual value from the parsed AST.
|
|
17
|
+
*/
|
|
18
|
+
width?: number;
|
|
19
|
+
/** Placeholder height in pixels. */
|
|
20
|
+
height?: number;
|
|
21
|
+
/**
|
|
22
|
+
* Placeholder background colour. Pass the same value as `bg` in the
|
|
23
|
+
* scene declaration so there is no flash on hydration.
|
|
24
|
+
*/
|
|
25
|
+
bg?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Asset URL overrides. Keys are the asset names used in the DSL.
|
|
28
|
+
* Values are the URLs the renderer should use instead of the parsed path.
|
|
29
|
+
* Useful for CDN assets or data URIs in demos.
|
|
30
|
+
*/
|
|
31
|
+
assets?: Record<string, string>;
|
|
32
|
+
/** Start playing as soon as the scene is hydrated. Defaults to true. */
|
|
33
|
+
autoplay?: boolean;
|
|
34
|
+
class?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const {
|
|
38
|
+
code,
|
|
39
|
+
width = 800,
|
|
40
|
+
height = 400,
|
|
41
|
+
bg = "white",
|
|
42
|
+
assets = {},
|
|
43
|
+
autoplay = true,
|
|
44
|
+
class: className,
|
|
45
|
+
} = Astro.props;
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
<div
|
|
49
|
+
class:list={["markdy-root", className]}
|
|
50
|
+
data-markdy-code={code}
|
|
51
|
+
data-markdy-assets={JSON.stringify(assets)}
|
|
52
|
+
data-markdy-autoplay={String(autoplay)}
|
|
53
|
+
style={`width:${width}px;height:${height}px;overflow:hidden`}
|
|
54
|
+
>
|
|
55
|
+
<!--
|
|
56
|
+
SSR placeholder: keeps layout stable before the island hydrates.
|
|
57
|
+
Background matches the scene bg prop to avoid a visible flash.
|
|
58
|
+
-->
|
|
59
|
+
<div
|
|
60
|
+
class="markdy-placeholder"
|
|
61
|
+
aria-label="Markdy animation loading"
|
|
62
|
+
style={`width:${width}px;height:${height}px;background:${bg};display:flex;align-items:center;justify-content:center`}
|
|
63
|
+
>
|
|
64
|
+
<span
|
|
65
|
+
style="font-family:sans-serif;font-size:12px;color:#bbb;letter-spacing:0.05em"
|
|
66
|
+
aria-hidden="true"
|
|
67
|
+
>
|
|
68
|
+
▶ markdy
|
|
69
|
+
</span>
|
|
70
|
+
</div>
|
|
71
|
+
</div>
|
|
72
|
+
|
|
73
|
+
<script>
|
|
74
|
+
import { createPlayer } from "@markdy/renderer-dom";
|
|
75
|
+
|
|
76
|
+
function hydrate(el: HTMLElement): void {
|
|
77
|
+
const code = el.dataset.markdyCode;
|
|
78
|
+
if (!code) return;
|
|
79
|
+
|
|
80
|
+
let assets: Record<string, string> = {};
|
|
81
|
+
try {
|
|
82
|
+
assets = JSON.parse(el.dataset.markdyAssets ?? "{}") as Record<
|
|
83
|
+
string,
|
|
84
|
+
string
|
|
85
|
+
>;
|
|
86
|
+
} catch {
|
|
87
|
+
// Leave assets empty on parse failure — renderer falls back to DSL paths.
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const autoplay = el.dataset.markdyAutoplay !== "false";
|
|
91
|
+
|
|
92
|
+
// Clear the SSR placeholder before mounting the canvas.
|
|
93
|
+
el.innerHTML = "";
|
|
94
|
+
el.style.overflow = "visible";
|
|
95
|
+
|
|
96
|
+
createPlayer({ container: el, code, assets, autoplay });
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Observe every unhydrated .markdy-root element and hydrate it when it
|
|
100
|
+
// enters the viewport (100 px root margin so the scene is ready before
|
|
101
|
+
// the user actually sees it).
|
|
102
|
+
const observer = new IntersectionObserver(
|
|
103
|
+
(entries) => {
|
|
104
|
+
for (const entry of entries) {
|
|
105
|
+
if (entry.isIntersecting) {
|
|
106
|
+
const el = entry.target as HTMLElement;
|
|
107
|
+
observer.unobserve(el);
|
|
108
|
+
hydrate(el);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
{ rootMargin: "100px" },
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
function initAll(): void {
|
|
116
|
+
document
|
|
117
|
+
.querySelectorAll<HTMLElement>(".markdy-root:not([data-markdy-init])")
|
|
118
|
+
.forEach((el) => {
|
|
119
|
+
// Flag the element to prevent double-registration across re-runs.
|
|
120
|
+
el.dataset.markdyInit = "pending";
|
|
121
|
+
observer.observe(el);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Initial page load.
|
|
126
|
+
if (document.readyState === "loading") {
|
|
127
|
+
document.addEventListener("DOMContentLoaded", initAll);
|
|
128
|
+
} else {
|
|
129
|
+
initAll();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Re-run after Astro View Transitions / client-side navigation.
|
|
133
|
+
document.addEventListener("astro:page-load", initAll);
|
|
134
|
+
</script>
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as Markdy } from "./Markdy.astro";
|