@terrafirmagreg-team/mdx 1.2.9 → 1.2.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terrafirmagreg-team/mdx",
3
- "version": "1.2.9",
3
+ "version": "1.2.11",
4
4
  "type": "module",
5
5
  "description": "MDX/markdown pipeline",
6
6
  "license": "LGPL-3.0-only",
@@ -20,8 +20,8 @@
20
20
  "./lib/markdown": "./src/lib/markdown/index.ts"
21
21
  },
22
22
  "dependencies": {
23
- "@terrafirmagreg-team/translate": "^1.2.9",
24
- "@terrafirmagreg-team/ui": "^1.2.12",
23
+ "@terrafirmagreg-team/translate": "^1.2.11",
24
+ "@terrafirmagreg-team/ui": "^1.2.15",
25
25
  "fumadocs-docgen": "^3.2.0",
26
26
  "fumadocs-twoslash": "^4.0.4",
27
27
  "fumadocs-typescript": "5.6.0",
@@ -1,8 +1,22 @@
1
1
  'use client';
2
2
 
3
- import { storyFor } from '../lib/story';
3
+ import { useEffect, useState } from 'react';
4
+ import { ensureStories, storyFor } from '../lib/story-registry';
5
+ import type { Story } from '@fumadocs/story/next/client';
4
6
 
5
7
  export function MdxStory({ name }: { name?: string }) {
6
- const entry = name ? storyFor(name) : undefined;
7
- return entry ? <entry.WithControl /> : null;
8
+ const [story, setStory] = useState<Story | undefined>(() =>
9
+ name ? storyFor(name) : undefined,
10
+ );
11
+ useEffect(() => {
12
+ if (story || !name) return;
13
+ let live = true;
14
+ void ensureStories().then(() => {
15
+ if (live) setStory(storyFor(name));
16
+ });
17
+ return () => {
18
+ live = false;
19
+ };
20
+ }, [name, story]);
21
+ return story ? <story.WithControl /> : null;
8
22
  }
@@ -54,7 +54,7 @@ const rehypeCodeOptions = {
54
54
  };
55
55
 
56
56
  export const mdOptions = (
57
- datasetConfig: Record<string, { repo?: string }> = {},
57
+ datasetConfig: Record<string, { github?: { repo?: string } }> = {},
58
58
  ) => ({
59
59
  mdOptions: {
60
60
  remarkImageOptions: { onError: 'ignore' },
@@ -25,7 +25,7 @@ const DEFAULT_PATHS = [
25
25
  ];
26
26
 
27
27
  export function remarkGithubAutolinks(options: {
28
- datasetConfig?: Record<string, { repo?: string }>;
28
+ datasetConfig?: Record<string, { github?: { repo?: string } }>;
29
29
  paths?: RegExp[];
30
30
  } = {}) {
31
31
  const { datasetConfig = {}, paths = DEFAULT_PATHS } = options;
@@ -34,7 +34,7 @@ export function remarkGithubAutolinks(options: {
34
34
  const pack = paths
35
35
  .map((pattern) => pattern.exec(filePath)?.[1])
36
36
  .find(Boolean);
37
- const repo = pack ? datasetConfig[pack]?.repo : undefined;
37
+ const repo = pack ? datasetConfig[pack]?.github?.repo : undefined;
38
38
  if (!repo) return;
39
39
  const repoUrl = `https://github.com/${repo}`;
40
40
  const visit = (node: MdastNode) => {
@@ -0,0 +1,28 @@
1
+ import type { Story } from '@fumadocs/story/next/client';
2
+
3
+ const registry = ((globalThis as Record<symbol, unknown>)[
4
+ Symbol.for('terrafirmagreg.mdx.stories')
5
+ ] ??= {}) as Record<string, Story>;
6
+
7
+ const state = ((globalThis as Record<symbol, unknown>)[
8
+ Symbol.for('terrafirmagreg.mdx.storyLoader')
9
+ ] ??= {}) as {
10
+ load?: () => Promise<unknown>;
11
+ loaded?: Promise<unknown>;
12
+ };
13
+
14
+ export function registerStory(name: string, story: Story): void {
15
+ registry[name] = story;
16
+ }
17
+
18
+ export function storyFor(name: string): Story | undefined {
19
+ return registry[name];
20
+ }
21
+
22
+ export function registerStoryLoader(load: () => Promise<unknown>): void {
23
+ state.load = load;
24
+ }
25
+
26
+ export function ensureStories(): Promise<unknown> {
27
+ return (state.loaded ??= state.load?.() ?? Promise.resolve());
28
+ }
package/src/lib/story.ts CHANGED
@@ -6,6 +6,7 @@ import type {
6
6
  StoryOptions,
7
7
  } from '@fumadocs/story/next/client';
8
8
  import { DocsWithControl } from '../components/story-panel';
9
+ import { registerStory } from './story-registry';
9
10
  import type { ComponentType } from 'react';
10
11
 
11
12
  export { useStory } from '@fumadocs/story';
@@ -16,19 +17,10 @@ const factory = defineStoryFactory({
16
17
  WithControl: DocsWithControl,
17
18
  });
18
19
 
19
- const registry = ((globalThis as Record<symbol, unknown>)[
20
- Symbol.for('terrafirmagreg.mdx.stories')
21
- ] ??= {}) as Record<string, Story>;
22
-
23
20
  export function defineStory<C extends ComponentType<any>>(
24
21
  options: StoryOptions<C>,
25
22
  ): Story<C> {
26
23
  const story = factory.defineStory(options);
27
- if (options.displayName)
28
- registry[options.displayName] = story as Story;
24
+ if (options.displayName) registerStory(options.displayName, story as Story);
29
25
  return story;
30
26
  }
31
-
32
- export function storyFor(name: string): Story | undefined {
33
- return registry[name];
34
- }