@terrafirmagreg-team/mdx 1.2.5 → 1.2.7

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.5",
3
+ "version": "1.2.7",
4
4
  "type": "module",
5
5
  "description": "MDX/markdown pipeline",
6
6
  "license": "LGPL-3.0-only",
@@ -16,14 +16,14 @@
16
16
  "./src/components/*/index.ts"
17
17
  ],
18
18
  "./types/*": "./src/types/*.ts",
19
- "./types": "./src/types/index.ts"
19
+ "./types": "./src/types/index.ts",
20
+ "./lib/markdown": "./src/lib/markdown/index.ts"
20
21
  },
21
22
  "dependencies": {
22
23
  "@fumadocs/story": "^1.5.3",
23
24
  "@fumari/stf": "^1.1.2",
24
- "@terrafirmagreg-team/translate": "^1.2.6",
25
- "@terrafirmagreg-team/ui": "^1.2.7",
26
- "@types/turndown": "^5.0.6",
25
+ "@terrafirmagreg-team/translate": "^1.2.8",
26
+ "@terrafirmagreg-team/ui": "^1.2.9",
27
27
  "fumadocs-core": "16.17.10",
28
28
  "fumadocs-docgen": "^3.2.0",
29
29
  "fumadocs-twoslash": "^4.0.4",
@@ -37,8 +37,6 @@
37
37
  "remark-gfm": "^4.0.1",
38
38
  "remark-math": "^6.0.0",
39
39
  "remark-parse": "^11.0.0",
40
- "turndown": "^7.2.5",
41
- "turndown-plugin-gfm": "^1.0.2",
42
40
  "unified": "^11.0.5",
43
41
  "unist-util-visit": "^5.2.0"
44
42
  },
@@ -38,6 +38,7 @@ import { Mermaid } from '@terrafirmagreg-team/ui/components/mdx/mermaid';
38
38
  import { Callout, CalloutContainer } from '@terrafirmagreg-team/ui/components/mdx/callout';
39
39
  import { Input, Output, Preview } from '@terrafirmagreg-team/ui/components/mdx/preview';
40
40
  import { DocsImage } from './docs-image';
41
+ import { MdxStory } from './mdx-story';
41
42
  import { Multiblock } from '@terrafirmagreg-team/ui/components/mdx/multiblock';
42
43
  import { Icon } from '@terrafirmagreg-team/ui/components/mdx/icon';
43
44
  import { Video } from '@terrafirmagreg-team/ui/components/mdx/video';
@@ -86,6 +87,7 @@ export function getMDXComponents(components?: MDXComponents) {
86
87
  Input,
87
88
  Output,
88
89
  DocsImage,
90
+ Story: MdxStory,
89
91
  Multiblock,
90
92
  Icon,
91
93
  Video,
@@ -0,0 +1,8 @@
1
+ 'use client';
2
+
3
+ import { storyFor } from '../lib/story';
4
+
5
+ export function MdxStory({ name }: { name?: string }) {
6
+ const entry = name ? storyFor(name) : undefined;
7
+ return entry ? <entry.WithControl /> : null;
8
+ }
@@ -1,4 +1,3 @@
1
- export * from './dom';
2
1
  export * from './providers';
3
2
 
4
3
  export async function renderSectionMarkdown(page: {
@@ -3,6 +3,7 @@ type MdastNode = {
3
3
  value?: string;
4
4
  url?: string;
5
5
  children?: MdastNode[];
6
+ data?: { hProperties?: Record<string, unknown> };
6
7
  };
7
8
 
8
9
  const ISSUE_REF = /^#\d+$/;
@@ -18,15 +19,21 @@ const SKIP = new Set([
18
19
  'html',
19
20
  ]);
20
21
 
21
- const DEFAULT_PATH = /[/\\]changelog[/\\]([^/\\]+)[/\\]/;
22
+ const DEFAULT_PATHS = [
23
+ /[/\\]changelog[/\\]([^/\\]+)[/\\]/,
24
+ /[/\\]docs[/\\]([^/\\]+)[/\\][^/\\]+[/\\]major-changelogs[/\\]/,
25
+ ];
22
26
 
23
27
  export function remarkGithubAutolinks(options: {
24
28
  datasetConfig?: Record<string, { repo?: string }>;
25
- path?: RegExp;
29
+ paths?: RegExp[];
26
30
  } = {}) {
27
- const { datasetConfig = {}, path = DEFAULT_PATH } = options;
31
+ const { datasetConfig = {}, paths = DEFAULT_PATHS } = options;
28
32
  return (tree: MdastNode, file: { path?: string }) => {
29
- const pack = path.exec(file.path ?? '')?.[1];
33
+ const filePath = file.path ?? '';
34
+ const pack = paths
35
+ .map((pattern) => pattern.exec(filePath)?.[1])
36
+ .find(Boolean);
30
37
  const repo = pack ? datasetConfig[pack]?.repo : undefined;
31
38
  if (!repo) return;
32
39
  const repoUrl = `https://github.com/${repo}`;
@@ -43,12 +50,14 @@ export function remarkGithubAutolinks(options: {
43
50
  type: 'link',
44
51
  url: `${repoUrl}/issues/${part.slice(1)}`,
45
52
  children: [{ type: 'text', value: part }],
53
+ data: { hProperties: { className: 'gh-issue' } },
46
54
  }
47
55
  : USER_REF.test(part)
48
56
  ? {
49
57
  type: 'link',
50
58
  url: `https://github.com/${part.slice(1)}`,
51
59
  children: [{ type: 'text', value: part }],
60
+ data: { hProperties: { className: 'gh-mention' } },
52
61
  }
53
62
  : { type: 'text', value: part },
54
63
  );
package/src/lib/story.ts CHANGED
@@ -1,11 +1,32 @@
1
1
  'use client';
2
2
 
3
3
  import { defineStoryFactory } from '@fumadocs/story/next/client';
4
+ import type {
5
+ Story,
6
+ StoryOptions,
7
+ } from '@fumadocs/story/next/client';
4
8
  import { DocsWithControl } from '../components/story-panel';
9
+ import type { ComponentType } from 'react';
5
10
 
6
11
  export { md } from './md';
7
12
 
8
- export const { defineStory } = defineStoryFactory({
13
+ const factory = defineStoryFactory({
9
14
  WithControl: DocsWithControl,
10
15
  });
11
16
 
17
+ const registry = ((globalThis as Record<symbol, unknown>)[
18
+ Symbol.for('terrafirmagreg.mdx.stories')
19
+ ] ??= {}) as Record<string, Story>;
20
+
21
+ export function defineStory<C extends ComponentType<any>>(
22
+ options: StoryOptions<C>,
23
+ ): Story<C> {
24
+ const story = factory.defineStory(options);
25
+ if (options.displayName)
26
+ registry[options.displayName] = story as Story;
27
+ return story;
28
+ }
29
+
30
+ export function storyFor(name: string): Story | undefined {
31
+ return registry[name];
32
+ }
@@ -1,104 +0,0 @@
1
- 'use client';
2
- import type TurndownService from 'turndown';
3
-
4
- function decodedUrl(raw: string): string {
5
- const url = raw.replace(/(?:%[0-9A-Fa-f]{2})+/g, (run) => {
6
- let decoded: string;
7
- try {
8
- decoded = decodeURIComponent(run);
9
- } catch {
10
- return run;
11
- }
12
- return /[/?#&=%+]/.test(decoded) ? run : decoded;
13
- });
14
- return /[\s<>()]/.test(url) ? `<${url}>` : url;
15
- }
16
-
17
- let servicePromise: Promise<TurndownService> | undefined;
18
-
19
- function turndownService(): Promise<TurndownService> {
20
- servicePromise ??= (async () => {
21
- const [{ default: TurndownServiceImpl }, { gfm }] = await Promise.all([
22
- import('turndown'),
23
- // @ts-expect-error -- no published type declarations
24
- import('turndown-plugin-gfm'),
25
- ]);
26
- const service = new TurndownServiceImpl({
27
- headingStyle: 'atx',
28
- codeBlockStyle: 'fenced',
29
- bulletListMarker: '-',
30
- });
31
- service.use(gfm);
32
- service.remove([
33
- 'script',
34
- 'style',
35
- 'noscript',
36
- 'template',
37
- 'button',
38
- 'select',
39
- 'nav',
40
- 'aside',
41
- 'footer',
42
- ]);
43
- service.remove(
44
- (node) =>
45
- node.nodeName === 'SVG' ||
46
- (node instanceof HTMLImageElement &&
47
- (node.src.startsWith('data:') ||
48
- (node.width > 0 && node.width <= 48))) ||
49
- (node instanceof HTMLElement && node.dataset.mdSkip !== undefined),
50
- );
51
- service.addRule('decodedLink', {
52
- filter: 'a',
53
- replacement(content, node) {
54
- const anchor = node as HTMLAnchorElement;
55
- const href = anchor.getAttribute('href');
56
- if (!href) return content;
57
- const title = anchor.getAttribute('title');
58
- const titlePart = title ? ` "${title.replace(/"/g, '\\"')}"` : '';
59
- return `[${content}](${decodedUrl(href)}${titlePart})`;
60
- },
61
- });
62
- service.addRule('decodedImage', {
63
- filter: 'img',
64
- replacement(_content, node) {
65
- const image = node as HTMLImageElement;
66
- const src = image.getAttribute('src');
67
- if (!src) return '';
68
- const alt = image.getAttribute('alt') ?? '';
69
- const title = image.getAttribute('title');
70
- const titlePart = title ? ` "${title.replace(/"/g, '\\"')}"` : '';
71
- return `![${alt}](${decodedUrl(src)}${titlePart})`;
72
- },
73
- });
74
- return service;
75
- })();
76
- return servicePromise;
77
- }
78
-
79
- export async function domToMarkdown(
80
- root: Element | null | undefined,
81
- ): Promise<string> {
82
- if (!root) return '';
83
- return (await turndownService()).turndown(root as HTMLElement);
84
- }
85
-
86
- export function pageMarkdown(
87
- root: Element | null | undefined,
88
- title: string,
89
- ): Promise<string> {
90
- return domToMarkdown(root).then((body) => `# ${title}\n\n${body}`);
91
- }
92
-
93
- export async function openPageMarkdown(
94
- root: Element | null | undefined,
95
- title: string,
96
- ): Promise<void> {
97
- const markdown = await pageMarkdown(root, title);
98
- const url = URL.createObjectURL(
99
- new Blob([markdown], { type: 'text/markdown;charset=utf-8' }),
100
- );
101
- window.open(url, '_blank', 'noopener');
102
- setTimeout(() => URL.revokeObjectURL(url), 60_000);
103
- }
104
-