@rokkit/stories 1.0.0-next.133 → 1.0.0-next.134
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 +169 -0
- package/package.json +3 -1
- package/src/lib/shiki.js +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# @rokkit/stories
|
|
2
|
+
|
|
3
|
+
Utilities for building interactive code stories and tutorials in SvelteKit — file fetching, metadata parsing, section navigation, and syntax-highlighted code display.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @rokkit/stories
|
|
9
|
+
# or
|
|
10
|
+
npm install @rokkit/stories
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Overview
|
|
14
|
+
|
|
15
|
+
`@rokkit/stories` is used to build interactive documentation pages where each "story" pairs a live Svelte component preview with its source files. It handles:
|
|
16
|
+
|
|
17
|
+
- Loading and grouping source files via Vite's `import.meta.glob`
|
|
18
|
+
- Parsing metadata (title, description, category, order) from story modules
|
|
19
|
+
- Navigating stories by section and slug
|
|
20
|
+
- Rendering syntax-highlighted code via Shiki
|
|
21
|
+
- Displaying a live preview alongside the highlighted source
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Loading stories in a SvelteKit route
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
// src/routes/stories/[slug]/+page.js
|
|
29
|
+
import { fetchStories, getAllSections, findSection } from '@rokkit/stories'
|
|
30
|
+
|
|
31
|
+
const sources = import.meta.glob('/src/stories/**/+page.svelte', { as: 'raw', eager: false })
|
|
32
|
+
const modules = import.meta.glob('/src/stories/**/meta.js')
|
|
33
|
+
|
|
34
|
+
export async function load({ params }) {
|
|
35
|
+
const stories = await fetchStories(sources, modules)
|
|
36
|
+
const sections = getAllSections()
|
|
37
|
+
const section = findSection(sections, params.slug)
|
|
38
|
+
|
|
39
|
+
return { stories, sections, section }
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Rendering a story
|
|
44
|
+
|
|
45
|
+
```svelte
|
|
46
|
+
<!-- src/routes/stories/[slug]/+page.svelte -->
|
|
47
|
+
<script>
|
|
48
|
+
import { StoryViewer } from '@rokkit/stories'
|
|
49
|
+
|
|
50
|
+
let { data } = $props()
|
|
51
|
+
</script>
|
|
52
|
+
|
|
53
|
+
<StoryViewer App={data.section.App} files={data.section.files} />
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`StoryViewer` renders a split view: the live component preview on the left, syntax-highlighted source files on the right.
|
|
57
|
+
|
|
58
|
+
### Working with sections and metadata
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
import {
|
|
62
|
+
fetchImports,
|
|
63
|
+
getSlug,
|
|
64
|
+
getSections,
|
|
65
|
+
groupFiles,
|
|
66
|
+
findSection,
|
|
67
|
+
findGroupForSection
|
|
68
|
+
} from '@rokkit/stories'
|
|
69
|
+
|
|
70
|
+
// Fetch raw source file contents
|
|
71
|
+
const files = await fetchImports(import.meta.glob('./examples/**', { as: 'raw' }))
|
|
72
|
+
|
|
73
|
+
// Group files by their parent folder
|
|
74
|
+
const grouped = groupFiles(files)
|
|
75
|
+
|
|
76
|
+
// Build a navigation tree from story metadata
|
|
77
|
+
const sections = getSections(metadata)
|
|
78
|
+
|
|
79
|
+
// Look up a section by URL slug
|
|
80
|
+
const current = findSection(sections, 'getting-started')
|
|
81
|
+
|
|
82
|
+
// Find which nav group a section belongs to
|
|
83
|
+
const group = findGroupForSection(sections, current.id)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Syntax highlighting
|
|
87
|
+
|
|
88
|
+
```js
|
|
89
|
+
import { highlightCode, preloadHighlighter } from '@rokkit/stories'
|
|
90
|
+
|
|
91
|
+
// Warm the highlighter (call once on app start to avoid first-render delay)
|
|
92
|
+
await preloadHighlighter()
|
|
93
|
+
|
|
94
|
+
// Highlight a code string
|
|
95
|
+
const html = await highlightCode('const x = 1', {
|
|
96
|
+
lang: 'javascript',
|
|
97
|
+
theme: 'github-light' // or 'github-dark'
|
|
98
|
+
})
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Supported languages: `svelte`, `javascript`, `typescript`, `css`, `html`, `json`, `bash`, `shell`.
|
|
102
|
+
|
|
103
|
+
## API
|
|
104
|
+
|
|
105
|
+
### Components
|
|
106
|
+
|
|
107
|
+
| Export | Description |
|
|
108
|
+
| ------------- | -------------------------------------------------------------------------------------------- |
|
|
109
|
+
| `StoryViewer` | Renders a live preview (`App` prop) alongside syntax-highlighted source files (`files` prop) |
|
|
110
|
+
| `CodeViewer` | Tabbed code viewer with syntax highlighting |
|
|
111
|
+
| `Preview` | Isolated preview container for a live component |
|
|
112
|
+
| `Notes` | Prose content area for story narrative text |
|
|
113
|
+
|
|
114
|
+
### Story utilities (`@rokkit/stories`)
|
|
115
|
+
|
|
116
|
+
| Export | Description |
|
|
117
|
+
| ----------------------------------- | ------------------------------------------------------------- |
|
|
118
|
+
| `fetchImports(sources)` | Resolve `import.meta.glob` entries into `File[]` objects |
|
|
119
|
+
| `fetchStories(sources, modules)` | Combine source files and metadata modules into stories |
|
|
120
|
+
| `getSlug(file)` | Derive a URL slug from a file path |
|
|
121
|
+
| `getSections(metadata)` | Build a hierarchical section list from metadata objects |
|
|
122
|
+
| `groupFiles(files)` | Group `File[]` by parent folder name |
|
|
123
|
+
| `getAllSections()` | Return the full section list (populated after `fetchStories`) |
|
|
124
|
+
| `findSection(sections, slug)` | Look up a section by slug |
|
|
125
|
+
| `findGroupForSection(sections, id)` | Find the nav group that contains a section |
|
|
126
|
+
|
|
127
|
+
### Shiki utilities
|
|
128
|
+
|
|
129
|
+
| Export | Description |
|
|
130
|
+
| ------------------------------ | ------------------------------------------------------ |
|
|
131
|
+
| `highlightCode(code, options)` | Highlight a code string; returns HTML string |
|
|
132
|
+
| `preloadHighlighter()` | Warm the Shiki highlighter to avoid cold-start latency |
|
|
133
|
+
|
|
134
|
+
### Types
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
type File = {
|
|
138
|
+
file: string
|
|
139
|
+
group?: string
|
|
140
|
+
name: string
|
|
141
|
+
language: string
|
|
142
|
+
content: string | object
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
type Metadata = {
|
|
146
|
+
title: string
|
|
147
|
+
description: string
|
|
148
|
+
category: string
|
|
149
|
+
tags: string[]
|
|
150
|
+
depth: number
|
|
151
|
+
order: number
|
|
152
|
+
children?: Metadata[]
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
type Story = {
|
|
156
|
+
files: File[]
|
|
157
|
+
App?: SvelteComponent
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Dependencies
|
|
162
|
+
|
|
163
|
+
- `shiki` — syntax highlighting
|
|
164
|
+
- `frontmatter` — metadata parsing
|
|
165
|
+
- `@rokkit/core` — shared utilities
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
Part of [Rokkit](https://github.com/jerrythomas/rokkit) — a Svelte 5 component library and design system.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rokkit/stories",
|
|
3
|
-
"version": "1.0.0-next.
|
|
3
|
+
"version": "1.0.0-next.134",
|
|
4
4
|
"description": "Utilities for building tutorials.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -25,10 +25,12 @@
|
|
|
25
25
|
"files": [
|
|
26
26
|
"src/**/*.js",
|
|
27
27
|
"dist/**/*.d.ts",
|
|
28
|
+
"README.md",
|
|
28
29
|
"LICENSE"
|
|
29
30
|
],
|
|
30
31
|
"scripts": {
|
|
31
32
|
"prepublishOnly": "cp ../../LICENSE . && bun clean && bun tsc --project tsconfig.build.json",
|
|
33
|
+
"postpublish": "rm -f LICENSE",
|
|
32
34
|
"clean": "rm -rf dist",
|
|
33
35
|
"build": "bun prepublishOnly"
|
|
34
36
|
},
|
package/src/lib/shiki.js
CHANGED
|
@@ -70,7 +70,7 @@ export async function preloadHighlighter() {
|
|
|
70
70
|
try {
|
|
71
71
|
await initializeHighlighter()
|
|
72
72
|
} catch (error) {
|
|
73
|
-
console.warn('Failed to preload syntax highlighter:', error.message)
|
|
73
|
+
console.warn('Failed to preload syntax highlighter:', error.message)
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
|