histoire 0.9.4 → 0.10.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/dist/node/build.js +6 -0
- package/dist/node/builtin-plugins/vanilla-support/plugin.js +2 -2
- package/dist/node/collect/worker.js +8 -0
- package/dist/node/commands/dev.js +1 -1
- package/dist/node/config.d.ts +1 -0
- package/dist/node/config.js +20 -3
- package/dist/node/context.d.ts +2 -1
- package/dist/node/context.js +1 -0
- package/dist/node/markdown.d.ts +7 -0
- package/dist/node/markdown.js +129 -1
- package/dist/node/plugin.d.ts +9 -0
- package/dist/node/preview.js +1 -1
- package/dist/node/server.js +3 -0
- package/dist/node/stories.d.ts +1 -1
- package/dist/node/stories.js +5 -2
- package/dist/node/vite.d.ts +2 -0
- package/dist/node/vite.js +30 -29
- package/package.json +9 -8
- package/src/node/build.ts +7 -0
- package/src/node/builtin-plugins/vanilla-support/plugin.ts +2 -2
- package/src/node/collect/worker.ts +9 -0
- package/src/node/commands/dev.ts +1 -1
- package/src/node/config.ts +23 -3
- package/src/node/context.ts +3 -1
- package/src/node/markdown.ts +148 -1
- package/src/node/plugin.ts +9 -0
- package/src/node/preview.ts +1 -1
- package/src/node/server.ts +4 -0
- package/src/node/stories.ts +5 -2
- package/src/node/vite.ts +32 -30
package/dist/node/build.js
CHANGED
|
@@ -11,6 +11,7 @@ import { useCollectStories } from './collect/index.js';
|
|
|
11
11
|
import { BuildPluginApi } from './plugin.js';
|
|
12
12
|
import { useModuleLoader } from './load.js';
|
|
13
13
|
import { startPreview } from './preview.js';
|
|
14
|
+
import { createMarkdownFilesWatcher } from './markdown.js';
|
|
14
15
|
const PRELOAD_MODULES = [
|
|
15
16
|
'vendor',
|
|
16
17
|
];
|
|
@@ -22,6 +23,11 @@ const PREFETCHED_MODULES = [
|
|
|
22
23
|
export async function build(ctx) {
|
|
23
24
|
const startTime = performance.now();
|
|
24
25
|
await findAllStories(ctx);
|
|
26
|
+
// Scan for markdown files
|
|
27
|
+
{
|
|
28
|
+
const { stop } = await createMarkdownFilesWatcher(ctx);
|
|
29
|
+
await stop();
|
|
30
|
+
}
|
|
25
31
|
const resolvedViteConfig = await resolveViteConfig(ctx);
|
|
26
32
|
const server = await createViteServer(await getViteConfigWithPlugins(true, ctx));
|
|
27
33
|
await server.pluginContainer.buildStart({});
|
|
@@ -4,7 +4,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
4
4
|
export function vanillaSupport() {
|
|
5
5
|
return {
|
|
6
6
|
name: 'builtin:vanilla-support',
|
|
7
|
-
|
|
7
|
+
defaultConfig() {
|
|
8
8
|
return {
|
|
9
9
|
supportMatch: [
|
|
10
10
|
{
|
|
@@ -19,7 +19,7 @@ export function vanillaSupport() {
|
|
|
19
19
|
id: 'vanilla',
|
|
20
20
|
moduleName: __dirname,
|
|
21
21
|
setupFn: 'setupVanilla',
|
|
22
|
-
importStoryComponent: (file, index) => `import Comp${index} from
|
|
22
|
+
importStoryComponent: (file, index) => `import Comp${index} from ${JSON.stringify(file.moduleId)}`, // @TODO code-splitting
|
|
23
23
|
},
|
|
24
24
|
// onDev (api) {
|
|
25
25
|
// // Test vanilla story
|
|
@@ -32,6 +32,14 @@ export default async (payload) => {
|
|
|
32
32
|
storyData,
|
|
33
33
|
el,
|
|
34
34
|
});
|
|
35
|
+
if (payload.storyFile.markdownFile) {
|
|
36
|
+
const el = document.createElement('div');
|
|
37
|
+
el.innerHTML = payload.storyFile.markdownFile.html;
|
|
38
|
+
const text = el.textContent;
|
|
39
|
+
storyData.forEach(s => {
|
|
40
|
+
s.docsText = text;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
35
43
|
destroyDomEnv();
|
|
36
44
|
return {
|
|
37
45
|
storyData,
|
|
@@ -9,7 +9,7 @@ export async function devCommand(options) {
|
|
|
9
9
|
const ctx = await createContext({
|
|
10
10
|
mode: 'dev',
|
|
11
11
|
});
|
|
12
|
-
const { server, close } = await createServer(ctx, options.port);
|
|
12
|
+
const { server, close } = await createServer(ctx, options.port ?? 6006);
|
|
13
13
|
server.printUrls();
|
|
14
14
|
// Histoire config watcher
|
|
15
15
|
let watcher;
|
package/dist/node/config.d.ts
CHANGED
|
@@ -274,6 +274,7 @@ export declare const mergeConfig: <Source extends {
|
|
|
274
274
|
} ? any : T_62 | T_60 : T_60 | T_62 : never : never : never : never; } : never : never : T_56 | T_54 : T_54 | T_56 : never : never : never : never; } : never : never : T_50 | T_48 : T_48 | T_50 : never : never : never : never; } : never : never : T_44 | T_42 : T_42 | T_44 : never : never : never : never; } : never : never : T_38 | T_36 : T_36 | T_38 : never : never : never : never; } : never : never : T_32 | T_30 : T_30 | T_32 : never : never : never : never; } : never : never : T_26 | T_24 : T_24 | T_26 : never : never : never : never; } : never : never : T_20 | T_18 : T_18 | T_20 : never : never : never : never; } : never : never : T_14 | T_12 : T_12 | T_14 : never : never : never : never; } : never : never : T_8 | T_6 : T_6 | T_8 : never : never : never : never; } : never : never : T_2 | T : T | T_2 : never : never : never : never; };
|
|
275
275
|
export declare function resolveConfig(cwd: string, mode: ConfigMode): Promise<HistoireConfig>;
|
|
276
276
|
export declare function processConfig(config: HistoireConfig, mode: ConfigMode): Promise<HistoireConfig>;
|
|
277
|
+
export declare function processDefaultConfig(defaultConfig: HistoireConfig, preUserConfig: HistoireConfig, mode: ConfigMode): Promise<HistoireConfig>;
|
|
277
278
|
export declare function defineConfig(config: Partial<HistoireConfig>): Partial<HistoireConfig>;
|
|
278
279
|
declare module 'vite' {
|
|
279
280
|
interface UserConfig {
|
package/dist/node/config.js
CHANGED
|
@@ -15,7 +15,10 @@ export function getDefaultConfig() {
|
|
|
15
15
|
tailwindTokens(),
|
|
16
16
|
],
|
|
17
17
|
outDir: '.histoire/dist',
|
|
18
|
-
storyMatch: [
|
|
18
|
+
storyMatch: [
|
|
19
|
+
'**/*.story.vue',
|
|
20
|
+
'**/*.story.svelte',
|
|
21
|
+
],
|
|
19
22
|
storyIgnored: [
|
|
20
23
|
'**/node_modules/**',
|
|
21
24
|
'**/dist/**',
|
|
@@ -103,7 +106,8 @@ export function getDefaultConfig() {
|
|
|
103
106
|
const index = config.plugins?.findIndex(plugin => Array.isArray(plugin) &&
|
|
104
107
|
typeof plugin[0] === 'object' &&
|
|
105
108
|
!Array.isArray(plugin[0]) &&
|
|
106
|
-
|
|
109
|
+
// @ts-expect-error could have no property 'name'
|
|
110
|
+
plugin[0].name?.startsWith('vite:legacy'));
|
|
107
111
|
if (index !== -1) {
|
|
108
112
|
config.plugins?.splice(index, 1);
|
|
109
113
|
}
|
|
@@ -215,7 +219,9 @@ export async function resolveConfig(cwd = process.cwd(), mode) {
|
|
|
215
219
|
}
|
|
216
220
|
const viteConfig = await resolveViteConfig({}, 'serve');
|
|
217
221
|
const viteHistoireConfig = (viteConfig.histoire ?? {});
|
|
218
|
-
|
|
222
|
+
const preUserConfig = mergeConfig(result, viteHistoireConfig);
|
|
223
|
+
const processedDefaultConfig = await processDefaultConfig(getDefaultConfig(), preUserConfig, mode);
|
|
224
|
+
return processConfig(mergeConfig(preUserConfig, processedDefaultConfig), mode);
|
|
219
225
|
}
|
|
220
226
|
export async function processConfig(config, mode) {
|
|
221
227
|
for (const plugin of config.plugins) {
|
|
@@ -229,6 +235,17 @@ export async function processConfig(config, mode) {
|
|
|
229
235
|
config.outDir = path.resolve(process.cwd(), config.outDir);
|
|
230
236
|
return config;
|
|
231
237
|
}
|
|
238
|
+
export async function processDefaultConfig(defaultConfig, preUserConfig, mode) {
|
|
239
|
+
for (const plugin of [...defaultConfig.plugins, ...preUserConfig.plugins]) {
|
|
240
|
+
if (plugin.defaultConfig) {
|
|
241
|
+
const result = await plugin.defaultConfig(defaultConfig, mode);
|
|
242
|
+
if (result) {
|
|
243
|
+
defaultConfig = mergeConfig(result, defaultConfig);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return defaultConfig;
|
|
248
|
+
}
|
|
232
249
|
export function defineConfig(config) {
|
|
233
250
|
return config;
|
|
234
251
|
}
|
package/dist/node/context.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ServerStoryFile, FinalSupportPlugin } from '@histoire/shared';
|
|
1
|
+
import type { ServerStoryFile, FinalSupportPlugin, ServerMarkdownFile } from '@histoire/shared';
|
|
2
2
|
import { HistoireConfig, ConfigMode } from './config.js';
|
|
3
3
|
export interface Context {
|
|
4
4
|
root: string;
|
|
@@ -6,6 +6,7 @@ export interface Context {
|
|
|
6
6
|
mode: ConfigMode;
|
|
7
7
|
storyFiles: ServerStoryFile[];
|
|
8
8
|
supportPlugins: FinalSupportPlugin[];
|
|
9
|
+
markdownFiles: ServerMarkdownFile[];
|
|
9
10
|
}
|
|
10
11
|
export interface CreateContextOptions {
|
|
11
12
|
mode: Context['mode'];
|
package/dist/node/context.js
CHANGED
package/dist/node/markdown.d.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
import MarkdownIt from 'markdown-it';
|
|
2
|
+
import type { Plugin as VitePlugin } from 'vite';
|
|
3
|
+
import type { Context } from './context.js';
|
|
2
4
|
export declare function createMarkdownRenderer(): Promise<MarkdownIt>;
|
|
5
|
+
export declare function createMarkdownPlugins(ctx: Context): Promise<VitePlugin[]>;
|
|
6
|
+
export declare function createMarkdownFilesWatcher(ctx: Context): Promise<{
|
|
7
|
+
stop: () => Promise<void>;
|
|
8
|
+
}>;
|
|
9
|
+
export declare type MarkdownFilesWatcher = ReturnType<typeof createMarkdownFilesWatcher>;
|
package/dist/node/markdown.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import MarkdownIt from 'markdown-it';
|
|
2
|
+
import matter from 'gray-matter';
|
|
2
3
|
import shiki from 'shiki';
|
|
3
4
|
import anchor from 'markdown-it-anchor';
|
|
4
5
|
import attrs from 'markdown-it-attrs';
|
|
5
6
|
import emoji from 'markdown-it-emoji';
|
|
7
|
+
import chokidar from 'chokidar';
|
|
8
|
+
import fs from 'fs-extra';
|
|
9
|
+
import path from 'pathe';
|
|
6
10
|
import { slugify } from './util/slugify.js';
|
|
11
|
+
import { addStory, notifyStoryChange, removeStory } from './stories.js';
|
|
7
12
|
export async function createMarkdownRenderer() {
|
|
8
13
|
const highlighter = await shiki.getHighlighter({
|
|
9
14
|
theme: 'github-dark',
|
|
@@ -30,7 +35,7 @@ export async function createMarkdownRenderer() {
|
|
|
30
35
|
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
|
|
31
36
|
const hrefIndex = tokens[idx].attrIndex('href');
|
|
32
37
|
const classIndex = tokens[idx].attrIndex('class');
|
|
33
|
-
if (hrefIndex >= 0 && !tokens[idx].attrs[hrefIndex][1].startsWith('/') && (classIndex < 0 || !tokens[idx].attrs[classIndex][1].includes('header-anchor'))) {
|
|
38
|
+
if (hrefIndex >= 0 && !tokens[idx].attrs[hrefIndex][1].startsWith('/') && !tokens[idx].attrs[hrefIndex][1].startsWith('#') && (classIndex < 0 || !tokens[idx].attrs[classIndex][1].includes('header-anchor'))) {
|
|
34
39
|
// If you are sure other plugins can't add `target` - drop check below
|
|
35
40
|
const aIndex = tokens[idx].attrIndex('target');
|
|
36
41
|
if (aIndex < 0) {
|
|
@@ -46,3 +51,126 @@ export async function createMarkdownRenderer() {
|
|
|
46
51
|
}
|
|
47
52
|
return md;
|
|
48
53
|
}
|
|
54
|
+
async function createMarkdownRendererWithPlugins(ctx) {
|
|
55
|
+
let md = await createMarkdownRenderer();
|
|
56
|
+
if (ctx.config.markdown) {
|
|
57
|
+
const result = await ctx.config.markdown(md);
|
|
58
|
+
if (result) {
|
|
59
|
+
md = result;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return md;
|
|
63
|
+
}
|
|
64
|
+
export async function createMarkdownPlugins(ctx) {
|
|
65
|
+
const plugins = [];
|
|
66
|
+
const md = await createMarkdownRendererWithPlugins(ctx);
|
|
67
|
+
// @TODO extract
|
|
68
|
+
plugins.push({
|
|
69
|
+
name: 'histoire-vue-docs-block',
|
|
70
|
+
transform(code, id) {
|
|
71
|
+
if (!id.includes('?vue&type=docs'))
|
|
72
|
+
return;
|
|
73
|
+
if (!id.includes('lang.md'))
|
|
74
|
+
return;
|
|
75
|
+
const html = md.render(code);
|
|
76
|
+
return `export default Comp => {
|
|
77
|
+
Comp.doc = ${JSON.stringify(html)}
|
|
78
|
+
}`;
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
plugins.push({
|
|
82
|
+
name: 'histoire-markdown-files',
|
|
83
|
+
transform(code, id) {
|
|
84
|
+
if (id.endsWith('.story.md')) {
|
|
85
|
+
const relativePath = path.relative(ctx.root, id);
|
|
86
|
+
const { content, data: frontmatter } = matter(code);
|
|
87
|
+
const html = md.render(content);
|
|
88
|
+
return `export const html = ${JSON.stringify(html)}
|
|
89
|
+
export const frontmatter = ${JSON.stringify(frontmatter)}
|
|
90
|
+
export const relativePath = ${JSON.stringify(relativePath)}
|
|
91
|
+
|
|
92
|
+
if (import.meta.hot) {
|
|
93
|
+
import.meta.hot.accept(newModule => {
|
|
94
|
+
window.__hst_md_hmr(newModule)
|
|
95
|
+
})
|
|
96
|
+
}`;
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
return plugins;
|
|
101
|
+
}
|
|
102
|
+
export async function createMarkdownFilesWatcher(ctx) {
|
|
103
|
+
const md = await createMarkdownRendererWithPlugins(ctx);
|
|
104
|
+
const watcher = chokidar.watch(['**/*.story.md'], {
|
|
105
|
+
cwd: ctx.root,
|
|
106
|
+
ignored: ctx.config.storyIgnored,
|
|
107
|
+
});
|
|
108
|
+
async function addFile(relativePath) {
|
|
109
|
+
const absolutePath = path.resolve(ctx.root, relativePath);
|
|
110
|
+
const dirFiles = await fs.readdir(path.dirname(absolutePath));
|
|
111
|
+
const truncatedName = path.basename(absolutePath, '.md');
|
|
112
|
+
const isRelatedToStory = dirFiles.some((file) => !file.endsWith('.md') && file.startsWith(truncatedName));
|
|
113
|
+
const { data: frontmatter, content } = matter(await fs.readFile(absolutePath, 'utf8'));
|
|
114
|
+
const html = md.render(content);
|
|
115
|
+
const file = {
|
|
116
|
+
relativePath,
|
|
117
|
+
absolutePath,
|
|
118
|
+
isRelatedToStory,
|
|
119
|
+
frontmatter,
|
|
120
|
+
html,
|
|
121
|
+
};
|
|
122
|
+
ctx.markdownFiles.push(file);
|
|
123
|
+
if (!isRelatedToStory) {
|
|
124
|
+
const storyRelativePath = relativePath.replace(/\.md$/, '.js');
|
|
125
|
+
const storyFile = addStory(storyRelativePath, `export default ${JSON.stringify({
|
|
126
|
+
id: frontmatter.id,
|
|
127
|
+
title: frontmatter.title,
|
|
128
|
+
icon: frontmatter.icon,
|
|
129
|
+
iconColor: frontmatter.iconColor,
|
|
130
|
+
group: frontmatter.group,
|
|
131
|
+
docsOnly: true,
|
|
132
|
+
variants: [],
|
|
133
|
+
})}`);
|
|
134
|
+
file.storyFile = storyFile;
|
|
135
|
+
storyFile.markdownFile = file;
|
|
136
|
+
notifyStoryChange(storyFile);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
const searchPath = path.join(path.dirname(relativePath), truncatedName);
|
|
140
|
+
const storyFile = ctx.storyFiles.find(f => f.relativePath.startsWith(searchPath));
|
|
141
|
+
if (storyFile) {
|
|
142
|
+
file.storyFile = storyFile;
|
|
143
|
+
storyFile.markdownFile = file;
|
|
144
|
+
notifyStoryChange(storyFile);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return file;
|
|
148
|
+
}
|
|
149
|
+
function removeFile(relativePath) {
|
|
150
|
+
const index = ctx.markdownFiles.findIndex((file) => file.relativePath === relativePath);
|
|
151
|
+
if (index !== -1) {
|
|
152
|
+
const file = ctx.markdownFiles[index];
|
|
153
|
+
if (!file.isRelatedToStory) {
|
|
154
|
+
removeStory(file.storyFile.relativePath);
|
|
155
|
+
notifyStoryChange();
|
|
156
|
+
}
|
|
157
|
+
ctx.markdownFiles.splice(index, 1);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async function stop() {
|
|
161
|
+
await watcher.close();
|
|
162
|
+
}
|
|
163
|
+
watcher
|
|
164
|
+
.on('add', async (relativePath) => {
|
|
165
|
+
await addFile(relativePath);
|
|
166
|
+
})
|
|
167
|
+
.on('unlink', (relativePath) => {
|
|
168
|
+
removeFile(relativePath);
|
|
169
|
+
});
|
|
170
|
+
await new Promise(resolve => {
|
|
171
|
+
watcher.once('ready', resolve);
|
|
172
|
+
});
|
|
173
|
+
return {
|
|
174
|
+
stop,
|
|
175
|
+
};
|
|
176
|
+
}
|
package/dist/node/plugin.d.ts
CHANGED
|
@@ -10,6 +10,15 @@ export interface Plugin {
|
|
|
10
10
|
* Name of the plugin
|
|
11
11
|
*/
|
|
12
12
|
name: string;
|
|
13
|
+
/**
|
|
14
|
+
* Modify histoire default config. The hook can either mutate the passed config or
|
|
15
|
+
* return a partial config object that will be deeply merged into the existing
|
|
16
|
+
* config. User config will have higher priority than default config.
|
|
17
|
+
*
|
|
18
|
+
* Note: User plugins are resolved before running this hook so injecting other
|
|
19
|
+
* plugins inside the `config` hook will have no effect.
|
|
20
|
+
*/
|
|
21
|
+
defaultConfig?: (defaultConfig: HistoireConfig, mode: ConfigMode) => Partial<HistoireConfig> | null | void | Promise<Partial<HistoireConfig> | null | void>;
|
|
13
22
|
/**
|
|
14
23
|
* Modify histoire config. The hook can either mutate the passed config or
|
|
15
24
|
* return a partial config object that will be deeply merged into the existing
|
package/dist/node/preview.js
CHANGED
|
@@ -8,7 +8,7 @@ export async function startPreview(viteConfig, port, ctx) {
|
|
|
8
8
|
etag: true,
|
|
9
9
|
single: true,
|
|
10
10
|
}));
|
|
11
|
-
let finalPort = port ??
|
|
11
|
+
let finalPort = port ?? 6006;
|
|
12
12
|
const httpServer = http.createServer(app);
|
|
13
13
|
return new Promise((resolve, reject) => {
|
|
14
14
|
function onError(e) {
|
package/dist/node/server.js
CHANGED
|
@@ -5,6 +5,7 @@ import { useCollectStories } from './collect/index.js';
|
|
|
5
5
|
import { DevPluginApi } from './plugin.js';
|
|
6
6
|
import { useModuleLoader } from './load.js';
|
|
7
7
|
import { wrapLogError } from './util/log.js';
|
|
8
|
+
import { createMarkdownFilesWatcher } from './markdown.js';
|
|
8
9
|
export async function createServer(ctx, port) {
|
|
9
10
|
const server = await createViteServer(await getViteConfigWithPlugins(false, ctx));
|
|
10
11
|
await server.pluginContainer.buildStart({});
|
|
@@ -14,6 +15,7 @@ export async function createServer(ctx, port) {
|
|
|
14
15
|
server: nodeServer,
|
|
15
16
|
});
|
|
16
17
|
await watchStories(ctx);
|
|
18
|
+
const { stop: stopMdFileWatcher } = await createMarkdownFilesWatcher(ctx);
|
|
17
19
|
const pluginOnCleanups = [];
|
|
18
20
|
for (const plugin of ctx.config.plugins) {
|
|
19
21
|
if (plugin.onDev) {
|
|
@@ -125,6 +127,7 @@ export async function createServer(ctx, port) {
|
|
|
125
127
|
await wrapLogError('server.close', () => server.close());
|
|
126
128
|
await wrapLogError('nodeServer', () => nodeServer.close());
|
|
127
129
|
await wrapLogError('destroyCollectStories', () => destroyCollectStories());
|
|
130
|
+
await wrapLogError('stopMdFileWatcher', () => stopMdFileWatcher());
|
|
128
131
|
}
|
|
129
132
|
return {
|
|
130
133
|
server,
|
package/dist/node/stories.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ declare type StoryChangeHandler = (file?: ServerStoryFile) => unknown;
|
|
|
5
5
|
export declare function onStoryChange(handler: StoryChangeHandler): void;
|
|
6
6
|
export declare function watchStories(newContext: Context): Promise<chokidar.FSWatcher>;
|
|
7
7
|
export declare function notifyStoryChange(file?: ServerStoryFile): void;
|
|
8
|
-
export declare function addStory(relativeFilePath: string): ServerStoryFile;
|
|
8
|
+
export declare function addStory(relativeFilePath: string, virtualModuleCode?: string): ServerStoryFile;
|
|
9
9
|
export declare function removeStory(relativeFilePath: string): void;
|
|
10
10
|
export declare function findAllStories(newContext: Context): Promise<void>;
|
|
11
11
|
export {};
|
package/dist/node/stories.js
CHANGED
|
@@ -33,7 +33,7 @@ export function notifyStoryChange(file) {
|
|
|
33
33
|
function getAbsoluteFilePath(relativeFilePath) {
|
|
34
34
|
return resolve(context.root, relativeFilePath);
|
|
35
35
|
}
|
|
36
|
-
export function addStory(relativeFilePath) {
|
|
36
|
+
export function addStory(relativeFilePath, virtualModuleCode) {
|
|
37
37
|
const absoluteFilePath = getAbsoluteFilePath(relativeFilePath);
|
|
38
38
|
const fileId = Case.kebab(relativeFilePath);
|
|
39
39
|
let fileName = basename(relativeFilePath);
|
|
@@ -55,9 +55,12 @@ export function addStory(relativeFilePath) {
|
|
|
55
55
|
const file = {
|
|
56
56
|
id: fileId,
|
|
57
57
|
path: absoluteFilePath,
|
|
58
|
+
relativePath: relativeFilePath,
|
|
58
59
|
fileName,
|
|
59
|
-
moduleId: absoluteFilePath,
|
|
60
|
+
moduleId: virtualModuleCode ? `virtual:story:${absoluteFilePath}` : absoluteFilePath,
|
|
60
61
|
supportPluginId,
|
|
62
|
+
virtual: !!virtualModuleCode,
|
|
63
|
+
moduleCode: virtualModuleCode,
|
|
61
64
|
};
|
|
62
65
|
context.storyFiles.push(file);
|
|
63
66
|
return file;
|
package/dist/node/vite.d.ts
CHANGED
|
@@ -20,5 +20,7 @@ export declare const SUPPORT_PLUGINS_CLIENT = "virtual:$histoire-support-plugins
|
|
|
20
20
|
export declare const RESOLVED_SUPPORT_PLUGINS_CLIENT: string;
|
|
21
21
|
export declare const SUPPORT_PLUGINS_COLLECT = "virtual:$histoire-support-plugins-collect";
|
|
22
22
|
export declare const RESOLVED_SUPPORT_PLUGINS_COLLECT: string;
|
|
23
|
+
export declare const MARKDOWN_FILES = "virtual:$histoire-markdown-files";
|
|
24
|
+
export declare const RESOLVED_MARKDOWN_FILES: string;
|
|
23
25
|
export declare function resolveViteConfig(ctx: Context): Promise<ResolvedConfig>;
|
|
24
26
|
export declare function getViteConfigWithPlugins(isServer: boolean, ctx: Context): Promise<InlineConfig>;
|
package/dist/node/vite.js
CHANGED
|
@@ -6,7 +6,7 @@ import { APP_PATH, TEMP_PATH } from './alias.js';
|
|
|
6
6
|
import { notifyStoryChange } from './stories.js';
|
|
7
7
|
import { makeTree } from './tree.js';
|
|
8
8
|
import { parseColor } from './colors.js';
|
|
9
|
-
import {
|
|
9
|
+
import { createMarkdownPlugins } from './markdown.js';
|
|
10
10
|
import { getSearchDataJS, generateDocSearchData, generateTitleSearchData } from './search.js';
|
|
11
11
|
const require = createRequire(import.meta.url);
|
|
12
12
|
export const STORIES_ID = '$histoire-stories';
|
|
@@ -29,6 +29,8 @@ export const SUPPORT_PLUGINS_CLIENT = 'virtual:$histoire-support-plugins-client'
|
|
|
29
29
|
export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `\0${SUPPORT_PLUGINS_CLIENT}`;
|
|
30
30
|
export const SUPPORT_PLUGINS_COLLECT = 'virtual:$histoire-support-plugins-collect';
|
|
31
31
|
export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `\0${SUPPORT_PLUGINS_COLLECT}`;
|
|
32
|
+
export const MARKDOWN_FILES = 'virtual:$histoire-markdown-files';
|
|
33
|
+
export const RESOLVED_MARKDOWN_FILES = `\0${MARKDOWN_FILES}`;
|
|
32
34
|
export async function resolveViteConfig(ctx) {
|
|
33
35
|
const command = ctx.mode === 'dev' ? 'serve' : 'build';
|
|
34
36
|
let viteConfig = (await resolveViteConfigInternal({}, command));
|
|
@@ -80,14 +82,13 @@ export async function getViteConfigWithPlugins(isServer, ctx) {
|
|
|
80
82
|
},
|
|
81
83
|
},
|
|
82
84
|
optimizeDeps: {
|
|
83
|
-
disabled: isServer,
|
|
84
85
|
entries: [
|
|
85
86
|
`${APP_PATH}/bundle-main.js`,
|
|
86
87
|
`${APP_PATH}/bundle-sandbox.js`,
|
|
87
|
-
// `${APP_PATH}/server/index.js`,
|
|
88
88
|
],
|
|
89
89
|
include: optimizeDeps([
|
|
90
90
|
'case',
|
|
91
|
+
'flexsearch',
|
|
91
92
|
'shiki',
|
|
92
93
|
// Shiki dependencies
|
|
93
94
|
'vscode-oniguruma',
|
|
@@ -158,6 +159,12 @@ export async function getViteConfigWithPlugins(isServer, ctx) {
|
|
|
158
159
|
if (id.startsWith(SUPPORT_PLUGINS_COLLECT)) {
|
|
159
160
|
return RESOLVED_SUPPORT_PLUGINS_COLLECT;
|
|
160
161
|
}
|
|
162
|
+
if (id.startsWith(MARKDOWN_FILES)) {
|
|
163
|
+
return RESOLVED_MARKDOWN_FILES;
|
|
164
|
+
}
|
|
165
|
+
if (id.startsWith('virtual:story:')) {
|
|
166
|
+
return `\0${id}`;
|
|
167
|
+
}
|
|
161
168
|
},
|
|
162
169
|
async load(id) {
|
|
163
170
|
if (id === RESOLVED_STORIES_ID) {
|
|
@@ -166,6 +173,7 @@ export async function getViteConfigWithPlugins(isServer, ctx) {
|
|
|
166
173
|
return {
|
|
167
174
|
id: file.id,
|
|
168
175
|
path: file.treePath,
|
|
176
|
+
filePath: file.relativePath,
|
|
169
177
|
story: {
|
|
170
178
|
...file.story,
|
|
171
179
|
docsText: undefined,
|
|
@@ -270,6 +278,23 @@ if (import.meta.hot) {
|
|
|
270
278
|
${plugins.join(',\n ')}
|
|
271
279
|
}`;
|
|
272
280
|
}
|
|
281
|
+
if (id === RESOLVED_MARKDOWN_FILES) {
|
|
282
|
+
const filesJs = ctx.markdownFiles.map(f => `${JSON.stringify(f.relativePath)}: () => import(${JSON.stringify(`./${f.relativePath}`)})`).join(',');
|
|
283
|
+
return `import { reactive } from ${JSON.stringify(require.resolve('@histoire/vendors/vue'))}
|
|
284
|
+
export const markdownFiles = reactive({${filesJs}})
|
|
285
|
+
if (import.meta.hot) {
|
|
286
|
+
window.__hst_md_hmr = (newModule) => {
|
|
287
|
+
markdownFiles[newModule.relativePath] = () => newModule
|
|
288
|
+
}
|
|
289
|
+
}`;
|
|
290
|
+
}
|
|
291
|
+
if (id.startsWith('\0virtual:story:')) {
|
|
292
|
+
const moduleId = id.replace('\0', '');
|
|
293
|
+
const storyFile = ctx.storyFiles.find(f => f.moduleId === moduleId && f.virtual);
|
|
294
|
+
if (storyFile) {
|
|
295
|
+
return storyFile.moduleCode;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
273
298
|
},
|
|
274
299
|
handleHotUpdate(updateContext) {
|
|
275
300
|
const story = ctx.storyFiles.find(file => file.path === updateContext.file);
|
|
@@ -349,8 +374,8 @@ if (import.meta.hot) {
|
|
|
349
374
|
};
|
|
350
375
|
},
|
|
351
376
|
});
|
|
352
|
-
//
|
|
353
|
-
plugins.push(...await
|
|
377
|
+
// Markdown
|
|
378
|
+
plugins.push(...await createMarkdownPlugins(ctx));
|
|
354
379
|
if (ctx.mode === 'build') {
|
|
355
380
|
// Add file name in build mode to have components names instead of <Anonymous>
|
|
356
381
|
const include = [/\.vue$/];
|
|
@@ -375,27 +400,3 @@ if (import.meta.hot) {
|
|
|
375
400
|
plugins,
|
|
376
401
|
});
|
|
377
402
|
}
|
|
378
|
-
async function createCustomBlocksPlugins(ctx) {
|
|
379
|
-
const plugins = [];
|
|
380
|
-
let md = await createMarkdownRenderer();
|
|
381
|
-
if (ctx.config.markdown) {
|
|
382
|
-
const result = await ctx.config.markdown(md);
|
|
383
|
-
if (result) {
|
|
384
|
-
md = result;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
plugins.push({
|
|
388
|
-
name: 'histoire-vue-docs-block',
|
|
389
|
-
transform(code, id) {
|
|
390
|
-
if (!id.includes('?vue&type=docs'))
|
|
391
|
-
return;
|
|
392
|
-
if (!id.includes('lang.md'))
|
|
393
|
-
return;
|
|
394
|
-
const html = md.render(code);
|
|
395
|
-
return `export default Comp => {
|
|
396
|
-
Comp.doc = ${JSON.stringify(html)}
|
|
397
|
-
}`;
|
|
398
|
-
},
|
|
399
|
-
});
|
|
400
|
-
return plugins;
|
|
401
|
-
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "histoire",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "A fast story book powered by Vite",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -29,10 +29,10 @@
|
|
|
29
29
|
"histoire": "./bin.mjs"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@histoire/app": "^0.
|
|
33
|
-
"@histoire/controls": "^0.
|
|
34
|
-
"@histoire/shared": "^0.
|
|
35
|
-
"@histoire/vendors": "^0.
|
|
32
|
+
"@histoire/app": "^0.10.0",
|
|
33
|
+
"@histoire/controls": "^0.10.0",
|
|
34
|
+
"@histoire/shared": "^0.10.0",
|
|
35
|
+
"@histoire/vendors": "^0.10.0",
|
|
36
36
|
"@types/flexsearch": "^0.7.3",
|
|
37
37
|
"@types/markdown-it": "^12.2.3",
|
|
38
38
|
"birpc": "^0.1.1",
|
|
@@ -44,6 +44,7 @@
|
|
|
44
44
|
"flexsearch": "^0.7.21",
|
|
45
45
|
"fs-extra": "^10.0.1",
|
|
46
46
|
"globby": "^13.1.1",
|
|
47
|
+
"gray-matter": "^4.0.3",
|
|
47
48
|
"happy-dom": "^2.55.0",
|
|
48
49
|
"markdown-it": "^12.3.2",
|
|
49
50
|
"markdown-it-anchor": "^8.6.2",
|
|
@@ -57,10 +58,10 @@
|
|
|
57
58
|
"shiki": "0.10.1",
|
|
58
59
|
"sirv": "^2.0.2",
|
|
59
60
|
"tinypool": "^0.1.2",
|
|
60
|
-
"vite-node": "0.
|
|
61
|
+
"vite-node": "0.21.1"
|
|
61
62
|
},
|
|
62
63
|
"peerDependencies": {
|
|
63
|
-
"vite": "^2.9.0"
|
|
64
|
+
"vite": "^2.9.0 || ^3.0.0"
|
|
64
65
|
},
|
|
65
66
|
"devDependencies": {
|
|
66
67
|
"@peeky/test": "^0.13.5",
|
|
@@ -70,7 +71,7 @@
|
|
|
70
71
|
"@types/node": "^17.0.32",
|
|
71
72
|
"rollup": "^2.59.0",
|
|
72
73
|
"typescript": "^4.7.4",
|
|
73
|
-
"vite": "^
|
|
74
|
+
"vite": "^3.0.5"
|
|
74
75
|
},
|
|
75
76
|
"scripts": {
|
|
76
77
|
"build": "rimraf dist && tsc -d",
|
package/src/node/build.ts
CHANGED
|
@@ -20,6 +20,7 @@ import { useCollectStories } from './collect/index.js'
|
|
|
20
20
|
import { BuildEndCallback, BuildPluginApi, PreviewStoryCallback } from './plugin.js'
|
|
21
21
|
import { useModuleLoader } from './load.js'
|
|
22
22
|
import { startPreview } from './preview.js'
|
|
23
|
+
import { createMarkdownFilesWatcher } from './markdown.js'
|
|
23
24
|
|
|
24
25
|
const PRELOAD_MODULES = [
|
|
25
26
|
'vendor',
|
|
@@ -35,6 +36,12 @@ export async function build (ctx: Context) {
|
|
|
35
36
|
const startTime = performance.now()
|
|
36
37
|
await findAllStories(ctx)
|
|
37
38
|
|
|
39
|
+
// Scan for markdown files
|
|
40
|
+
{
|
|
41
|
+
const { stop } = await createMarkdownFilesWatcher(ctx)
|
|
42
|
+
await stop()
|
|
43
|
+
}
|
|
44
|
+
|
|
38
45
|
const resolvedViteConfig = await resolveViteConfig(ctx)
|
|
39
46
|
|
|
40
47
|
const server = await createViteServer(await getViteConfigWithPlugins(true, ctx))
|
|
@@ -8,7 +8,7 @@ export function vanillaSupport (): Plugin {
|
|
|
8
8
|
return {
|
|
9
9
|
name: 'builtin:vanilla-support',
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
defaultConfig () {
|
|
12
12
|
return {
|
|
13
13
|
supportMatch: [
|
|
14
14
|
{
|
|
@@ -24,7 +24,7 @@ export function vanillaSupport (): Plugin {
|
|
|
24
24
|
id: 'vanilla',
|
|
25
25
|
moduleName: __dirname,
|
|
26
26
|
setupFn: 'setupVanilla',
|
|
27
|
-
importStoryComponent: (file, index) => `import Comp${index} from
|
|
27
|
+
importStoryComponent: (file, index) => `import Comp${index} from ${JSON.stringify(file.moduleId)}`, // @TODO code-splitting
|
|
28
28
|
},
|
|
29
29
|
|
|
30
30
|
// onDev (api) {
|
|
@@ -56,6 +56,15 @@ export default async (payload: Payload): Promise<ReturnData> => {
|
|
|
56
56
|
el,
|
|
57
57
|
})
|
|
58
58
|
|
|
59
|
+
if (payload.storyFile.markdownFile) {
|
|
60
|
+
const el = document.createElement('div')
|
|
61
|
+
el.innerHTML = payload.storyFile.markdownFile.html
|
|
62
|
+
const text = el.textContent
|
|
63
|
+
storyData.forEach(s => {
|
|
64
|
+
s.docsText = text
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
59
68
|
destroyDomEnv()
|
|
60
69
|
|
|
61
70
|
return {
|
package/src/node/commands/dev.ts
CHANGED
|
@@ -15,7 +15,7 @@ export async function devCommand (options: DevOptions) {
|
|
|
15
15
|
const ctx = await createContext({
|
|
16
16
|
mode: 'dev',
|
|
17
17
|
})
|
|
18
|
-
const { server, close } = await createServer(ctx, options.port)
|
|
18
|
+
const { server, close } = await createServer(ctx, options.port ?? 6006)
|
|
19
19
|
server.printUrls()
|
|
20
20
|
|
|
21
21
|
// Histoire config watcher
|
package/src/node/config.ts
CHANGED
|
@@ -201,7 +201,10 @@ export function getDefaultConfig (): HistoireConfig {
|
|
|
201
201
|
tailwindTokens(),
|
|
202
202
|
],
|
|
203
203
|
outDir: '.histoire/dist',
|
|
204
|
-
storyMatch: [
|
|
204
|
+
storyMatch: [
|
|
205
|
+
'**/*.story.vue',
|
|
206
|
+
'**/*.story.svelte',
|
|
207
|
+
],
|
|
205
208
|
storyIgnored: [
|
|
206
209
|
'**/node_modules/**',
|
|
207
210
|
'**/dist/**',
|
|
@@ -289,7 +292,8 @@ export function getDefaultConfig (): HistoireConfig {
|
|
|
289
292
|
const index = config.plugins?.findIndex(plugin => Array.isArray(plugin) &&
|
|
290
293
|
typeof plugin[0] === 'object' &&
|
|
291
294
|
!Array.isArray(plugin[0]) &&
|
|
292
|
-
|
|
295
|
+
// @ts-expect-error could have no property 'name'
|
|
296
|
+
plugin[0].name?.startsWith('vite:legacy'))
|
|
293
297
|
if (index !== -1) {
|
|
294
298
|
config.plugins?.splice(index, 1)
|
|
295
299
|
}
|
|
@@ -408,7 +412,11 @@ export async function resolveConfig (cwd: string = process.cwd(), mode: ConfigMo
|
|
|
408
412
|
}
|
|
409
413
|
const viteConfig = await resolveViteConfig({}, 'serve')
|
|
410
414
|
const viteHistoireConfig = (viteConfig.histoire ?? {}) as HistoireConfig
|
|
411
|
-
|
|
415
|
+
|
|
416
|
+
const preUserConfig = mergeConfig(result, viteHistoireConfig)
|
|
417
|
+
const processedDefaultConfig = await processDefaultConfig(getDefaultConfig(), preUserConfig, mode)
|
|
418
|
+
|
|
419
|
+
return processConfig(mergeConfig(preUserConfig, processedDefaultConfig), mode)
|
|
412
420
|
}
|
|
413
421
|
|
|
414
422
|
export async function processConfig (config: HistoireConfig, mode: ConfigMode): Promise<HistoireConfig> {
|
|
@@ -424,6 +432,18 @@ export async function processConfig (config: HistoireConfig, mode: ConfigMode):
|
|
|
424
432
|
return config
|
|
425
433
|
}
|
|
426
434
|
|
|
435
|
+
export async function processDefaultConfig (defaultConfig: HistoireConfig, preUserConfig: HistoireConfig, mode: ConfigMode): Promise<HistoireConfig> {
|
|
436
|
+
for (const plugin of [...defaultConfig.plugins, ...preUserConfig.plugins]) {
|
|
437
|
+
if (plugin.defaultConfig) {
|
|
438
|
+
const result = await plugin.defaultConfig(defaultConfig, mode)
|
|
439
|
+
if (result) {
|
|
440
|
+
defaultConfig = mergeConfig(result, defaultConfig)
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return defaultConfig
|
|
445
|
+
}
|
|
446
|
+
|
|
427
447
|
export function defineConfig (config: Partial<HistoireConfig>) {
|
|
428
448
|
return config
|
|
429
449
|
}
|
package/src/node/context.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { resolveConfig as resolveViteConfig } from 'vite'
|
|
2
|
-
import type { ServerStoryFile, FinalSupportPlugin } from '@histoire/shared'
|
|
2
|
+
import type { ServerStoryFile, FinalSupportPlugin, ServerMarkdownFile } from '@histoire/shared'
|
|
3
3
|
import { resolveConfig, HistoireConfig, ConfigMode } from './config.js'
|
|
4
4
|
|
|
5
5
|
export interface Context {
|
|
@@ -8,6 +8,7 @@ export interface Context {
|
|
|
8
8
|
mode: ConfigMode
|
|
9
9
|
storyFiles: ServerStoryFile[]
|
|
10
10
|
supportPlugins: FinalSupportPlugin[]
|
|
11
|
+
markdownFiles: ServerMarkdownFile[]
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
export interface CreateContextOptions {
|
|
@@ -26,5 +27,6 @@ export async function createContext (options: CreateContextOptions): Promise<Con
|
|
|
26
27
|
mode: options.mode,
|
|
27
28
|
storyFiles: [],
|
|
28
29
|
supportPlugins,
|
|
30
|
+
markdownFiles: [],
|
|
29
31
|
}
|
|
30
32
|
}
|
package/src/node/markdown.ts
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import MarkdownIt from 'markdown-it'
|
|
2
|
+
import matter from 'gray-matter'
|
|
2
3
|
import shiki from 'shiki'
|
|
3
4
|
import anchor from 'markdown-it-anchor'
|
|
4
5
|
import attrs from 'markdown-it-attrs'
|
|
5
6
|
import emoji from 'markdown-it-emoji'
|
|
7
|
+
import type { Plugin as VitePlugin } from 'vite'
|
|
8
|
+
import chokidar from 'chokidar'
|
|
9
|
+
import fs from 'fs-extra'
|
|
10
|
+
import path from 'pathe'
|
|
11
|
+
import type { ServerMarkdownFile } from '@histoire/shared'
|
|
6
12
|
import { slugify } from './util/slugify.js'
|
|
13
|
+
import type { Context } from './context.js'
|
|
14
|
+
import { addStory, notifyStoryChange, removeStory } from './stories.js'
|
|
7
15
|
|
|
8
16
|
export async function createMarkdownRenderer () {
|
|
9
17
|
const highlighter = await shiki.getHighlighter({
|
|
@@ -36,7 +44,7 @@ export async function createMarkdownRenderer () {
|
|
|
36
44
|
const hrefIndex = tokens[idx].attrIndex('href')
|
|
37
45
|
const classIndex = tokens[idx].attrIndex('class')
|
|
38
46
|
|
|
39
|
-
if (hrefIndex >= 0 && !tokens[idx].attrs[hrefIndex][1].startsWith('/') && (classIndex < 0 || !tokens[idx].attrs[classIndex][1].includes('header-anchor'))) {
|
|
47
|
+
if (hrefIndex >= 0 && !tokens[idx].attrs[hrefIndex][1].startsWith('/') && !tokens[idx].attrs[hrefIndex][1].startsWith('#') && (classIndex < 0 || !tokens[idx].attrs[classIndex][1].includes('header-anchor'))) {
|
|
40
48
|
// If you are sure other plugins can't add `target` - drop check below
|
|
41
49
|
const aIndex = tokens[idx].attrIndex('target')
|
|
42
50
|
|
|
@@ -54,3 +62,142 @@ export async function createMarkdownRenderer () {
|
|
|
54
62
|
|
|
55
63
|
return md
|
|
56
64
|
}
|
|
65
|
+
|
|
66
|
+
async function createMarkdownRendererWithPlugins (ctx: Context) {
|
|
67
|
+
let md = await createMarkdownRenderer()
|
|
68
|
+
if (ctx.config.markdown) {
|
|
69
|
+
const result = await ctx.config.markdown(md)
|
|
70
|
+
if (result) {
|
|
71
|
+
md = result
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return md
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function createMarkdownPlugins (ctx: Context) {
|
|
78
|
+
const plugins: VitePlugin[] = []
|
|
79
|
+
const md = await createMarkdownRendererWithPlugins(ctx)
|
|
80
|
+
|
|
81
|
+
// @TODO extract
|
|
82
|
+
plugins.push({
|
|
83
|
+
name: 'histoire-vue-docs-block',
|
|
84
|
+
transform (code, id) {
|
|
85
|
+
if (!id.includes('?vue&type=docs')) return
|
|
86
|
+
if (!id.includes('lang.md')) return
|
|
87
|
+
const html = md.render(code)
|
|
88
|
+
return `export default Comp => {
|
|
89
|
+
Comp.doc = ${JSON.stringify(html)}
|
|
90
|
+
}`
|
|
91
|
+
},
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
plugins.push({
|
|
95
|
+
name: 'histoire-markdown-files',
|
|
96
|
+
transform (code, id) {
|
|
97
|
+
if (id.endsWith('.story.md')) {
|
|
98
|
+
const relativePath = path.relative(ctx.root, id)
|
|
99
|
+
const { content, data: frontmatter } = matter(code)
|
|
100
|
+
const html = md.render(content)
|
|
101
|
+
return `export const html = ${JSON.stringify(html)}
|
|
102
|
+
export const frontmatter = ${JSON.stringify(frontmatter)}
|
|
103
|
+
export const relativePath = ${JSON.stringify(relativePath)}
|
|
104
|
+
|
|
105
|
+
if (import.meta.hot) {
|
|
106
|
+
import.meta.hot.accept(newModule => {
|
|
107
|
+
window.__hst_md_hmr(newModule)
|
|
108
|
+
})
|
|
109
|
+
}`
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
return plugins
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export async function createMarkdownFilesWatcher (ctx: Context) {
|
|
118
|
+
const md = await createMarkdownRendererWithPlugins(ctx)
|
|
119
|
+
|
|
120
|
+
const watcher = chokidar.watch(['**/*.story.md'], {
|
|
121
|
+
cwd: ctx.root,
|
|
122
|
+
ignored: ctx.config.storyIgnored,
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
async function addFile (relativePath: string) {
|
|
126
|
+
const absolutePath = path.resolve(ctx.root, relativePath)
|
|
127
|
+
const dirFiles = await fs.readdir(path.dirname(absolutePath))
|
|
128
|
+
const truncatedName = path.basename(absolutePath, '.md')
|
|
129
|
+
const isRelatedToStory = dirFiles.some((file) => !file.endsWith('.md') && file.startsWith(truncatedName))
|
|
130
|
+
|
|
131
|
+
const { data: frontmatter, content } = matter(await fs.readFile(absolutePath, 'utf8'))
|
|
132
|
+
const html = md.render(content)
|
|
133
|
+
|
|
134
|
+
const file: ServerMarkdownFile = {
|
|
135
|
+
relativePath,
|
|
136
|
+
absolutePath,
|
|
137
|
+
isRelatedToStory,
|
|
138
|
+
frontmatter,
|
|
139
|
+
html,
|
|
140
|
+
}
|
|
141
|
+
ctx.markdownFiles.push(file)
|
|
142
|
+
|
|
143
|
+
if (!isRelatedToStory) {
|
|
144
|
+
const storyRelativePath = relativePath.replace(/\.md$/, '.js')
|
|
145
|
+
const storyFile = addStory(storyRelativePath, `export default ${JSON.stringify({
|
|
146
|
+
id: frontmatter.id,
|
|
147
|
+
title: frontmatter.title,
|
|
148
|
+
icon: frontmatter.icon,
|
|
149
|
+
iconColor: frontmatter.iconColor,
|
|
150
|
+
group: frontmatter.group,
|
|
151
|
+
docsOnly: true,
|
|
152
|
+
variants: [],
|
|
153
|
+
})}`)
|
|
154
|
+
file.storyFile = storyFile
|
|
155
|
+
storyFile.markdownFile = file
|
|
156
|
+
notifyStoryChange(storyFile)
|
|
157
|
+
} else {
|
|
158
|
+
const searchPath = path.join(path.dirname(relativePath), truncatedName)
|
|
159
|
+
const storyFile = ctx.storyFiles.find(f => f.relativePath.startsWith(searchPath))
|
|
160
|
+
if (storyFile) {
|
|
161
|
+
file.storyFile = storyFile
|
|
162
|
+
storyFile.markdownFile = file
|
|
163
|
+
notifyStoryChange(storyFile)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return file
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function removeFile (relativePath: string) {
|
|
171
|
+
const index = ctx.markdownFiles.findIndex((file) => file.relativePath === relativePath)
|
|
172
|
+
if (index !== -1) {
|
|
173
|
+
const file = ctx.markdownFiles[index]
|
|
174
|
+
if (!file.isRelatedToStory) {
|
|
175
|
+
removeStory(file.storyFile.relativePath)
|
|
176
|
+
notifyStoryChange()
|
|
177
|
+
}
|
|
178
|
+
ctx.markdownFiles.splice(index, 1)
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async function stop () {
|
|
183
|
+
await watcher.close()
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
watcher
|
|
187
|
+
.on('add', async (relativePath) => {
|
|
188
|
+
await addFile(relativePath)
|
|
189
|
+
})
|
|
190
|
+
.on('unlink', (relativePath) => {
|
|
191
|
+
removeFile(relativePath)
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
await new Promise(resolve => {
|
|
195
|
+
watcher.once('ready', resolve)
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
return {
|
|
199
|
+
stop,
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export type MarkdownFilesWatcher = ReturnType<typeof createMarkdownFilesWatcher>
|
package/src/node/plugin.ts
CHANGED
|
@@ -14,6 +14,15 @@ export interface Plugin {
|
|
|
14
14
|
* Name of the plugin
|
|
15
15
|
*/
|
|
16
16
|
name: string
|
|
17
|
+
/**
|
|
18
|
+
* Modify histoire default config. The hook can either mutate the passed config or
|
|
19
|
+
* return a partial config object that will be deeply merged into the existing
|
|
20
|
+
* config. User config will have higher priority than default config.
|
|
21
|
+
*
|
|
22
|
+
* Note: User plugins are resolved before running this hook so injecting other
|
|
23
|
+
* plugins inside the `config` hook will have no effect.
|
|
24
|
+
*/
|
|
25
|
+
defaultConfig?: (defaultConfig: HistoireConfig, mode: ConfigMode) => Partial<HistoireConfig> | null | void | Promise<Partial<HistoireConfig> | null | void>
|
|
17
26
|
/**
|
|
18
27
|
* Modify histoire config. The hook can either mutate the passed config or
|
|
19
28
|
* return a partial config object that will be deeply merged into the existing
|
package/src/node/preview.ts
CHANGED
package/src/node/server.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { useCollectStories } from './collect/index.js'
|
|
|
7
7
|
import { DevPluginApi } from './plugin.js'
|
|
8
8
|
import { useModuleLoader } from './load.js'
|
|
9
9
|
import { wrapLogError } from './util/log.js'
|
|
10
|
+
import { createMarkdownFilesWatcher } from './markdown.js'
|
|
10
11
|
|
|
11
12
|
export async function createServer (ctx: Context, port: number) {
|
|
12
13
|
const server = await createViteServer(await getViteConfigWithPlugins(false, ctx))
|
|
@@ -21,6 +22,8 @@ export async function createServer (ctx: Context, port: number) {
|
|
|
21
22
|
|
|
22
23
|
await watchStories(ctx)
|
|
23
24
|
|
|
25
|
+
const { stop: stopMdFileWatcher } = await createMarkdownFilesWatcher(ctx)
|
|
26
|
+
|
|
24
27
|
const pluginOnCleanups: (() => void | Promise<void>)[] = []
|
|
25
28
|
for (const plugin of ctx.config.plugins) {
|
|
26
29
|
if (plugin.onDev) {
|
|
@@ -149,6 +152,7 @@ export async function createServer (ctx: Context, port: number) {
|
|
|
149
152
|
await wrapLogError('server.close', () => server.close())
|
|
150
153
|
await wrapLogError('nodeServer', () => nodeServer.close())
|
|
151
154
|
await wrapLogError('destroyCollectStories', () => destroyCollectStories())
|
|
155
|
+
await wrapLogError('stopMdFileWatcher', () => stopMdFileWatcher())
|
|
152
156
|
}
|
|
153
157
|
|
|
154
158
|
return {
|
package/src/node/stories.ts
CHANGED
|
@@ -45,7 +45,7 @@ function getAbsoluteFilePath (relativeFilePath: string) {
|
|
|
45
45
|
return resolve(context.root, relativeFilePath)
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export function addStory (relativeFilePath: string) {
|
|
48
|
+
export function addStory (relativeFilePath: string, virtualModuleCode?: string) {
|
|
49
49
|
const absoluteFilePath = getAbsoluteFilePath(relativeFilePath)
|
|
50
50
|
const fileId = Case.kebab(relativeFilePath)
|
|
51
51
|
let fileName = basename(relativeFilePath)
|
|
@@ -71,9 +71,12 @@ export function addStory (relativeFilePath: string) {
|
|
|
71
71
|
const file: ServerStoryFile = {
|
|
72
72
|
id: fileId, // The file id will be changed by the story id after it is collected
|
|
73
73
|
path: absoluteFilePath,
|
|
74
|
+
relativePath: relativeFilePath,
|
|
74
75
|
fileName,
|
|
75
|
-
moduleId: absoluteFilePath,
|
|
76
|
+
moduleId: virtualModuleCode ? `virtual:story:${absoluteFilePath}` : absoluteFilePath,
|
|
76
77
|
supportPluginId,
|
|
78
|
+
virtual: !!virtualModuleCode,
|
|
79
|
+
moduleCode: virtualModuleCode,
|
|
77
80
|
}
|
|
78
81
|
context.storyFiles.push(file)
|
|
79
82
|
return file
|
package/src/node/vite.ts
CHANGED
|
@@ -15,7 +15,7 @@ import { Context } from './context.js'
|
|
|
15
15
|
import { notifyStoryChange } from './stories.js'
|
|
16
16
|
import { makeTree } from './tree.js'
|
|
17
17
|
import { parseColor } from './colors.js'
|
|
18
|
-
import {
|
|
18
|
+
import { createMarkdownPlugins } from './markdown.js'
|
|
19
19
|
import { getSearchDataJS, generateDocSearchData, generateTitleSearchData } from './search.js'
|
|
20
20
|
|
|
21
21
|
const require = createRequire(import.meta.url)
|
|
@@ -40,6 +40,8 @@ export const SUPPORT_PLUGINS_CLIENT = 'virtual:$histoire-support-plugins-client'
|
|
|
40
40
|
export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `\0${SUPPORT_PLUGINS_CLIENT}`
|
|
41
41
|
export const SUPPORT_PLUGINS_COLLECT = 'virtual:$histoire-support-plugins-collect'
|
|
42
42
|
export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `\0${SUPPORT_PLUGINS_COLLECT}`
|
|
43
|
+
export const MARKDOWN_FILES = 'virtual:$histoire-markdown-files'
|
|
44
|
+
export const RESOLVED_MARKDOWN_FILES = `\0${MARKDOWN_FILES}`
|
|
43
45
|
|
|
44
46
|
export async function resolveViteConfig (ctx: Context): Promise<ResolvedConfig> {
|
|
45
47
|
const command = ctx.mode === 'dev' ? 'serve' : 'build'
|
|
@@ -98,14 +100,13 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
|
|
|
98
100
|
},
|
|
99
101
|
},
|
|
100
102
|
optimizeDeps: {
|
|
101
|
-
disabled: isServer,
|
|
102
103
|
entries: [
|
|
103
104
|
`${APP_PATH}/bundle-main.js`,
|
|
104
105
|
`${APP_PATH}/bundle-sandbox.js`,
|
|
105
|
-
// `${APP_PATH}/server/index.js`,
|
|
106
106
|
],
|
|
107
107
|
include: optimizeDeps([
|
|
108
108
|
'case',
|
|
109
|
+
'flexsearch',
|
|
109
110
|
'shiki',
|
|
110
111
|
// Shiki dependencies
|
|
111
112
|
'vscode-oniguruma',
|
|
@@ -176,6 +177,12 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
|
|
|
176
177
|
if (id.startsWith(SUPPORT_PLUGINS_COLLECT)) {
|
|
177
178
|
return RESOLVED_SUPPORT_PLUGINS_COLLECT
|
|
178
179
|
}
|
|
180
|
+
if (id.startsWith(MARKDOWN_FILES)) {
|
|
181
|
+
return RESOLVED_MARKDOWN_FILES
|
|
182
|
+
}
|
|
183
|
+
if (id.startsWith('virtual:story:')) {
|
|
184
|
+
return `\0${id}`
|
|
185
|
+
}
|
|
179
186
|
},
|
|
180
187
|
|
|
181
188
|
async load (id) {
|
|
@@ -185,6 +192,7 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
|
|
|
185
192
|
return {
|
|
186
193
|
id: file.id,
|
|
187
194
|
path: file.treePath,
|
|
195
|
+
filePath: file.relativePath,
|
|
188
196
|
story: {
|
|
189
197
|
...file.story,
|
|
190
198
|
docsText: undefined,
|
|
@@ -297,6 +305,25 @@ if (import.meta.hot) {
|
|
|
297
305
|
${plugins.join(',\n ')}
|
|
298
306
|
}`
|
|
299
307
|
}
|
|
308
|
+
|
|
309
|
+
if (id === RESOLVED_MARKDOWN_FILES) {
|
|
310
|
+
const filesJs = ctx.markdownFiles.map(f => `${JSON.stringify(f.relativePath)}: () => import(${JSON.stringify(`./${f.relativePath}`)})`).join(',')
|
|
311
|
+
return `import { reactive } from ${JSON.stringify(require.resolve('@histoire/vendors/vue'))}
|
|
312
|
+
export const markdownFiles = reactive({${filesJs}})
|
|
313
|
+
if (import.meta.hot) {
|
|
314
|
+
window.__hst_md_hmr = (newModule) => {
|
|
315
|
+
markdownFiles[newModule.relativePath] = () => newModule
|
|
316
|
+
}
|
|
317
|
+
}`
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (id.startsWith('\0virtual:story:')) {
|
|
321
|
+
const moduleId = id.replace('\0', '')
|
|
322
|
+
const storyFile = ctx.storyFiles.find(f => f.moduleId === moduleId && f.virtual)
|
|
323
|
+
if (storyFile) {
|
|
324
|
+
return storyFile.moduleCode
|
|
325
|
+
}
|
|
326
|
+
}
|
|
300
327
|
},
|
|
301
328
|
|
|
302
329
|
handleHotUpdate (updateContext) {
|
|
@@ -382,8 +409,8 @@ if (import.meta.hot) {
|
|
|
382
409
|
},
|
|
383
410
|
})
|
|
384
411
|
|
|
385
|
-
//
|
|
386
|
-
plugins.push(...await
|
|
412
|
+
// Markdown
|
|
413
|
+
plugins.push(...await createMarkdownPlugins(ctx))
|
|
387
414
|
|
|
388
415
|
if (ctx.mode === 'build') {
|
|
389
416
|
// Add file name in build mode to have components names instead of <Anonymous>
|
|
@@ -410,28 +437,3 @@ if (import.meta.hot) {
|
|
|
410
437
|
plugins,
|
|
411
438
|
}) as InlineConfig
|
|
412
439
|
}
|
|
413
|
-
|
|
414
|
-
async function createCustomBlocksPlugins (ctx) {
|
|
415
|
-
const plugins: VitePlugin[] = []
|
|
416
|
-
|
|
417
|
-
let md = await createMarkdownRenderer()
|
|
418
|
-
if (ctx.config.markdown) {
|
|
419
|
-
const result = await ctx.config.markdown(md)
|
|
420
|
-
if (result) {
|
|
421
|
-
md = result
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
plugins.push({
|
|
425
|
-
name: 'histoire-vue-docs-block',
|
|
426
|
-
transform (code, id) {
|
|
427
|
-
if (!id.includes('?vue&type=docs')) return
|
|
428
|
-
if (!id.includes('lang.md')) return
|
|
429
|
-
const html = md.render(code)
|
|
430
|
-
return `export default Comp => {
|
|
431
|
-
Comp.doc = ${JSON.stringify(html)}
|
|
432
|
-
}`
|
|
433
|
-
},
|
|
434
|
-
})
|
|
435
|
-
|
|
436
|
-
return plugins
|
|
437
|
-
}
|