@terrafirmagreg-team/mdx 0.0.0-stage → 1.2.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/package.json +61 -3
- package/src/components/docs-image.tsx +62 -0
- package/src/components/mdx-components.tsx +158 -0
- package/src/components/story-fields.tsx +318 -0
- package/src/components/story-panel.tsx +181 -0
- package/src/lib/markdown/dom.ts +104 -0
- package/src/lib/markdown/index.ts +18 -0
- package/src/lib/markdown/providers.ts +26 -0
- package/src/lib/md-plugins.ts +113 -0
- package/src/lib/md.ts +20 -0
- package/src/lib/mdx/accordions.ts +102 -0
- package/src/lib/mdx/banner.ts +57 -0
- package/src/lib/mdx/directive-utils.ts +118 -0
- package/src/lib/mdx/directives.ts +261 -0
- package/src/lib/mdx/game-export.ts +110 -0
- package/src/lib/mdx/html-style.ts +136 -0
- package/src/lib/mdx/preview.ts +253 -0
- package/src/lib/mdx/references.ts +57 -0
- package/src/lib/mdx/tabs.ts +76 -0
- package/src/lib/pack-assets.ts +46 -0
- package/src/lib/remark-highlight.ts +82 -0
- package/src/lib/story-remark.ts +468 -0
- package/src/lib/story.ts +11 -0
- package/src/types/mdx.ts +68 -0
- package/README.md +0 -3
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DirectiveNode,
|
|
3
|
+
JsxAttribute,
|
|
4
|
+
JsxAttributeValue,
|
|
5
|
+
MdastNode,
|
|
6
|
+
} from '../../types/mdx';
|
|
7
|
+
|
|
8
|
+
export type {
|
|
9
|
+
DirectiveNode,
|
|
10
|
+
JsxAttribute,
|
|
11
|
+
JsxAttributeValue,
|
|
12
|
+
JsxElement,
|
|
13
|
+
MdastNode,
|
|
14
|
+
} from '../../types/mdx';
|
|
15
|
+
|
|
16
|
+
import { visit, SKIP, CONTINUE } from 'unist-util-visit';
|
|
17
|
+
|
|
18
|
+
export function walkContainers(
|
|
19
|
+
tree: MdastNode,
|
|
20
|
+
fn: (node: DirectiveNode) => boolean | void,
|
|
21
|
+
): void {
|
|
22
|
+
visit(tree as never, 'containerDirective', (node) =>
|
|
23
|
+
fn(node as DirectiveNode) === false ? SKIP : CONTINUE,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const DIRECTIVE_TYPES = [
|
|
28
|
+
'containerDirective',
|
|
29
|
+
'leafDirective',
|
|
30
|
+
'textDirective',
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
export function walkDirectives(
|
|
34
|
+
tree: MdastNode,
|
|
35
|
+
fn: (node: DirectiveNode) => boolean | void,
|
|
36
|
+
): void {
|
|
37
|
+
visit(tree as never, DIRECTIVE_TYPES, (node) =>
|
|
38
|
+
fn(node as DirectiveNode) === false ? SKIP : CONTINUE,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function toJsxAttributes(node: DirectiveNode): JsxAttribute[] {
|
|
43
|
+
return Object.entries(node.attributes ?? {}).map(([name, value]) => ({
|
|
44
|
+
type: 'mdxJsxAttribute',
|
|
45
|
+
name,
|
|
46
|
+
value: value == null || value === '' ? jsxExpression(estreeBoolean(true)) : value,
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function labelText(node: MdastNode): string {
|
|
51
|
+
const t = node.type;
|
|
52
|
+
const v = (node as { value?: unknown }).value;
|
|
53
|
+
if (typeof v === 'string') return v;
|
|
54
|
+
if (t === 'textDirective' || t === 'leafDirective') {
|
|
55
|
+
const d = node as DirectiveNode;
|
|
56
|
+
const inner = (d.children ?? []).map(labelText).join('');
|
|
57
|
+
const attrs = Object.entries(d.attributes ?? {})
|
|
58
|
+
.map(([k, v]) => (v == null ? k : v === true ? k : `${k}="${v}"`))
|
|
59
|
+
.join(' ');
|
|
60
|
+
const prefix = t === 'leafDirective' ? '::' : ':';
|
|
61
|
+
return `${prefix}${d.name ?? ''}${inner ? `[${inner}]` : ''}${attrs ? `{${attrs}}` : ''}`;
|
|
62
|
+
}
|
|
63
|
+
return (node.children ?? []).map(labelText).join('');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function extractDirectiveLabel(node: DirectiveNode): string {
|
|
67
|
+
for (const child of node.children ?? []) {
|
|
68
|
+
if (child.type === 'paragraph' && child.data?.directiveLabel) {
|
|
69
|
+
return (child.children ?? []).map(labelText).join('');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return '';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function extractLeafLabel(node: MdastNode): string {
|
|
76
|
+
return (node.children ?? []).map(labelText).join('');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function contentChildren(node: DirectiveNode): MdastNode[] {
|
|
80
|
+
return (node.children ?? []).filter(
|
|
81
|
+
(child) =>
|
|
82
|
+
!(child.type === 'paragraph' && child.data?.directiveLabel),
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function jsxAttr(name: string, value: string | boolean): JsxAttribute {
|
|
87
|
+
return { type: 'mdxJsxAttribute', name, value };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function jsxExpression(expression: unknown): JsxAttributeValue {
|
|
91
|
+
return {
|
|
92
|
+
type: 'mdxJsxAttributeValueExpression',
|
|
93
|
+
value: '',
|
|
94
|
+
data: {
|
|
95
|
+
estree: {
|
|
96
|
+
type: 'Program',
|
|
97
|
+
body: [{ type: 'ExpressionStatement', expression }],
|
|
98
|
+
sourceType: 'script',
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function estreeString(value: string): unknown {
|
|
105
|
+
return { type: 'Literal', value, raw: JSON.stringify(value) };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function estreeStringArray(labels: string[]): unknown {
|
|
109
|
+
return {
|
|
110
|
+
type: 'ArrayExpression',
|
|
111
|
+
elements: labels.map(estreeString),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function estreeBoolean(value: boolean): unknown {
|
|
116
|
+
return { type: 'Literal', value, raw: String(value) };
|
|
117
|
+
}
|
|
118
|
+
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import {
|
|
2
|
+
contentChildren,
|
|
3
|
+
extractDirectiveLabel,
|
|
4
|
+
extractLeafLabel,
|
|
5
|
+
jsxAttr,
|
|
6
|
+
jsxExpression,
|
|
7
|
+
toJsxAttributes,
|
|
8
|
+
walkDirectives,
|
|
9
|
+
type DirectiveNode,
|
|
10
|
+
type MdastNode,
|
|
11
|
+
} from './directive-utils.ts';
|
|
12
|
+
import { gradientImage } from '@terrafirmagreg-team/ui/lib/mdx/gradient';
|
|
13
|
+
import { createRequire } from 'node:module';
|
|
14
|
+
import { join, relative } from 'node:path';
|
|
15
|
+
|
|
16
|
+
const require = createRequire(join(process.cwd(), '_'));
|
|
17
|
+
|
|
18
|
+
function resolvePkgPath(spec: string) {
|
|
19
|
+
const resolved = require.resolve(`@terrafirmagreg-team/${spec}`);
|
|
20
|
+
return relative(process.cwd(), resolved).replaceAll('\\', '/');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function remarkDirectives() {
|
|
24
|
+
return (tree: MdastNode, file?: { value?: unknown }) => {
|
|
25
|
+
walkDirectives(tree, (node) => {
|
|
26
|
+
const { type, name } = node;
|
|
27
|
+
if (
|
|
28
|
+
name === 'math' &&
|
|
29
|
+
(type === 'textDirective' ||
|
|
30
|
+
type === 'leafDirective' ||
|
|
31
|
+
type === 'containerDirective')
|
|
32
|
+
) {
|
|
33
|
+
const block = type !== 'textDirective';
|
|
34
|
+
const tex =
|
|
35
|
+
type === 'containerDirective'
|
|
36
|
+
? containerBody(node, file)
|
|
37
|
+
: extractLeafLabel(node).trim();
|
|
38
|
+
Object.assign(node, mathNode(tex, block));
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
if (type === 'textDirective' && name === 'gradient') {
|
|
42
|
+
Object.assign(node, {
|
|
43
|
+
type: 'mdxJsxTextElement',
|
|
44
|
+
name: 'span',
|
|
45
|
+
attributes: [
|
|
46
|
+
styleAttr(gradientStyle(node)),
|
|
47
|
+
{ type: 'mdxJsxAttribute', name: 'data-md', value: 'gradient' },
|
|
48
|
+
],
|
|
49
|
+
});
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (
|
|
53
|
+
name === 'glow' &&
|
|
54
|
+
(type === 'textDirective' ||
|
|
55
|
+
type === 'leafDirective' ||
|
|
56
|
+
type === 'containerDirective')
|
|
57
|
+
) {
|
|
58
|
+
Object.assign(node, {
|
|
59
|
+
type:
|
|
60
|
+
type === 'textDirective'
|
|
61
|
+
? 'mdxJsxTextElement'
|
|
62
|
+
: 'mdxJsxFlowElement',
|
|
63
|
+
name: 'span',
|
|
64
|
+
attributes: [
|
|
65
|
+
styleAttr(glowStyle(node)),
|
|
66
|
+
{ type: 'mdxJsxAttribute', name: 'data-md', value: 'glow' },
|
|
67
|
+
],
|
|
68
|
+
});
|
|
69
|
+
return type === 'containerDirective' ? undefined : false;
|
|
70
|
+
}
|
|
71
|
+
if (type === 'textDirective' && name === 'icon') {
|
|
72
|
+
Object.assign(node, {
|
|
73
|
+
type: 'mdxJsxTextElement',
|
|
74
|
+
name: 'Icon',
|
|
75
|
+
attributes: [jsxAttr('name', extractLeafLabel(node).trim())],
|
|
76
|
+
children: [],
|
|
77
|
+
});
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (
|
|
81
|
+
name === 'card' &&
|
|
82
|
+
(type === 'leafDirective' || type === 'containerDirective')
|
|
83
|
+
) {
|
|
84
|
+
const title =
|
|
85
|
+
type === 'leafDirective'
|
|
86
|
+
? extractLeafLabel(node)
|
|
87
|
+
: extractDirectiveLabel(node);
|
|
88
|
+
Object.assign(node, {
|
|
89
|
+
type: 'mdxJsxFlowElement',
|
|
90
|
+
name: 'Card',
|
|
91
|
+
attributes: [
|
|
92
|
+
jsxAttr('title', title.trim()),
|
|
93
|
+
...toJsxAttributes(node),
|
|
94
|
+
],
|
|
95
|
+
children:
|
|
96
|
+
type === 'containerDirective' ? contentChildren(node) : [],
|
|
97
|
+
});
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if (type === 'containerDirective' && name === 'cards') {
|
|
101
|
+
Object.assign(node, {
|
|
102
|
+
type: 'mdxJsxFlowElement',
|
|
103
|
+
name: 'Cards',
|
|
104
|
+
attributes: toJsxAttributes(node),
|
|
105
|
+
children: contentChildren(node),
|
|
106
|
+
});
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (type === 'leafDirective' && name === 'type-table') {
|
|
110
|
+
const attrs = node.attributes;
|
|
111
|
+
if (typeof attrs?.path === 'string' && attrs.path.startsWith('pkg:'))
|
|
112
|
+
attrs.path = resolvePkgPath(attrs.path.slice(4));
|
|
113
|
+
Object.assign(node, {
|
|
114
|
+
type: 'mdxJsxFlowElement',
|
|
115
|
+
name: 'auto-type-table',
|
|
116
|
+
attributes: toJsxAttributes(node),
|
|
117
|
+
children: [],
|
|
118
|
+
});
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
if (type === 'leafDirective' && name === 'recipe') {
|
|
122
|
+
Object.assign(node, {
|
|
123
|
+
type: 'mdxJsxFlowElement',
|
|
124
|
+
name: 'Recipe',
|
|
125
|
+
attributes: [
|
|
126
|
+
{
|
|
127
|
+
type: 'mdxJsxAttribute',
|
|
128
|
+
name: 'id',
|
|
129
|
+
value: extractLeafLabel(node).trim(),
|
|
130
|
+
},
|
|
131
|
+
...toJsxAttributes(node),
|
|
132
|
+
],
|
|
133
|
+
children: [],
|
|
134
|
+
});
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
return undefined;
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function mathNode(tex: string, block: boolean): MdastNode {
|
|
143
|
+
const code = {
|
|
144
|
+
type: 'element',
|
|
145
|
+
tagName: 'code',
|
|
146
|
+
properties: { className: ['language-math', 'math-display'] },
|
|
147
|
+
children: [{ type: 'text', value: tex }],
|
|
148
|
+
};
|
|
149
|
+
return block
|
|
150
|
+
? ({
|
|
151
|
+
type: 'math',
|
|
152
|
+
value: tex,
|
|
153
|
+
data: { hName: 'pre', hChildren: [code] },
|
|
154
|
+
} as unknown as MdastNode)
|
|
155
|
+
: ({
|
|
156
|
+
type: 'inlineMath',
|
|
157
|
+
value: tex,
|
|
158
|
+
data: {
|
|
159
|
+
hName: 'code',
|
|
160
|
+
hProperties: { className: ['language-math', 'math-inline'] },
|
|
161
|
+
hChildren: [{ type: 'text', value: tex }],
|
|
162
|
+
},
|
|
163
|
+
} as unknown as MdastNode);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function containerBody(
|
|
167
|
+
node: DirectiveNode,
|
|
168
|
+
file?: { value?: unknown },
|
|
169
|
+
): string {
|
|
170
|
+
const source = typeof file?.value === 'string' ? file.value : '';
|
|
171
|
+
const pos = node.position as
|
|
172
|
+
| { start?: { offset?: number }; end?: { offset?: number } }
|
|
173
|
+
| undefined;
|
|
174
|
+
const start = pos?.start?.offset;
|
|
175
|
+
const end = pos?.end?.offset;
|
|
176
|
+
if (typeof start !== 'number' || typeof end !== 'number' || !source)
|
|
177
|
+
return '';
|
|
178
|
+
const lines = source.slice(start, end).split('\n');
|
|
179
|
+
return lines
|
|
180
|
+
.slice(1, lines[lines.length - 1]?.trim() === ':::' ? -1 : undefined)
|
|
181
|
+
.join('\n')
|
|
182
|
+
.trim();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function estreeObject(entries: [string, string][]): unknown {
|
|
186
|
+
return {
|
|
187
|
+
type: 'ObjectExpression',
|
|
188
|
+
properties: entries.map(([key, value]) => ({
|
|
189
|
+
type: 'Property',
|
|
190
|
+
key: { type: 'Literal', value: key },
|
|
191
|
+
value: { type: 'Literal', value, raw: JSON.stringify(value) },
|
|
192
|
+
kind: 'init',
|
|
193
|
+
computed: false,
|
|
194
|
+
shorthand: false,
|
|
195
|
+
})),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function styleAttr(entries: [string, string][]) {
|
|
200
|
+
return {
|
|
201
|
+
type: 'mdxJsxAttribute' as const,
|
|
202
|
+
name: 'style',
|
|
203
|
+
value: jsxExpression(estreeObject(entries)),
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function attr(node: DirectiveNode, name: string) {
|
|
208
|
+
const value = node.attributes?.[name];
|
|
209
|
+
return typeof value === 'string' && value ? value : undefined;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function hasFlag(node: DirectiveNode, name: string) {
|
|
213
|
+
return node.attributes?.[name] !== undefined;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function gradientStyle(node: DirectiveNode): [string, string][] {
|
|
217
|
+
return [
|
|
218
|
+
['backgroundImage', gradientImage(attrObject(node))],
|
|
219
|
+
['WebkitBackgroundClip', 'text'],
|
|
220
|
+
['backgroundClip', 'text'],
|
|
221
|
+
['WebkitTextFillColor', 'transparent'],
|
|
222
|
+
['color', 'transparent'],
|
|
223
|
+
];
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function glowStyle(node: DirectiveNode): [string, string][] {
|
|
227
|
+
const from = attr(node, 'from') ?? '#fa18cf';
|
|
228
|
+
const to = attr(node, 'to') ?? '#ff7967';
|
|
229
|
+
const gradient = gradientImage(attrObject(node));
|
|
230
|
+
const boxBg = `radial-gradient(color-mix(in srgb, ${from} 18%, transparent), color-mix(in srgb, ${to} 8%, transparent))`;
|
|
231
|
+
const style: [string, string][] = [
|
|
232
|
+
['display', 'block'],
|
|
233
|
+
['borderRadius', '20px'],
|
|
234
|
+
['border', '1px solid transparent'],
|
|
235
|
+
['padding', '0.625rem 1rem'],
|
|
236
|
+
['marginBottom', '1.5rem'],
|
|
237
|
+
['textAlign', 'center'],
|
|
238
|
+
['backgroundImage', hasFlag(node, 'gradient') ? `${gradient}, ${boxBg}` : boxBg],
|
|
239
|
+
['boxShadow', `0 10px 20px color-mix(in srgb, ${from} 25%, transparent)`],
|
|
240
|
+
];
|
|
241
|
+
if (hasFlag(node, 'gradient')) {
|
|
242
|
+
style.push(
|
|
243
|
+
['WebkitBackgroundClip', 'text, border-box'],
|
|
244
|
+
['backgroundClip', 'text, border-box'],
|
|
245
|
+
['WebkitTextFillColor', 'transparent'],
|
|
246
|
+
['color', 'transparent'],
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
if (hasFlag(node, 'fade')) style.push(['borderImage', `${gradient} 1`]);
|
|
250
|
+
return style;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function attrObject(node: DirectiveNode) {
|
|
254
|
+
return {
|
|
255
|
+
from: attr(node, 'from'),
|
|
256
|
+
to: attr(node, 'to'),
|
|
257
|
+
dir: attr(node, 'dir'),
|
|
258
|
+
image: attr(node, 'image'),
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import {
|
|
2
|
+
contentChildren,
|
|
3
|
+
extractDirectiveLabel,
|
|
4
|
+
extractLeafLabel,
|
|
5
|
+
estreeBoolean,
|
|
6
|
+
estreeString,
|
|
7
|
+
jsxAttr,
|
|
8
|
+
jsxExpression,
|
|
9
|
+
walkDirectives,
|
|
10
|
+
type DirectiveNode,
|
|
11
|
+
type JsxAttribute,
|
|
12
|
+
type MdastNode,
|
|
13
|
+
} from './directive-utils.ts';
|
|
14
|
+
|
|
15
|
+
const COMPONENTS: Record<string, string> = {
|
|
16
|
+
item: 'ItemEntry',
|
|
17
|
+
block: 'BlockEntry',
|
|
18
|
+
fluid: 'FluidEntry',
|
|
19
|
+
entity: 'EntityEntry',
|
|
20
|
+
tag: 'TagEntry',
|
|
21
|
+
quest: 'QuestEntry',
|
|
22
|
+
guide: 'GuideEntry',
|
|
23
|
+
multiblock: 'Multiblock',
|
|
24
|
+
image: 'DocsImage',
|
|
25
|
+
story: 'Story',
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const LEAF_KEY: Record<string, string> = {
|
|
29
|
+
image: 'src',
|
|
30
|
+
story: 'name',
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const LABEL_ATTR: Record<string, string> = {
|
|
34
|
+
image: 'alt',
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
function pageDataset(file: { path?: string }): string | undefined {
|
|
38
|
+
return file.path
|
|
39
|
+
?.replaceAll('\\', '/')
|
|
40
|
+
.match(/content\/docs\/([^/]+)\/[^/]+\//)?.[1];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function remarkGameExport() {
|
|
44
|
+
return (tree: MdastNode, file: { path?: string }) => {
|
|
45
|
+
const pack = pageDataset(file);
|
|
46
|
+
walkDirectives(tree, (directive) => {
|
|
47
|
+
const name = directive.name ?? '';
|
|
48
|
+
const component = COMPONENTS[name];
|
|
49
|
+
if (!component) return;
|
|
50
|
+
if (
|
|
51
|
+
directive.type !== 'leafDirective' &&
|
|
52
|
+
directive.type !== 'containerDirective'
|
|
53
|
+
)
|
|
54
|
+
return;
|
|
55
|
+
|
|
56
|
+
const attrs = toAttributes(directive);
|
|
57
|
+
if (
|
|
58
|
+
name === 'image' &&
|
|
59
|
+
pack &&
|
|
60
|
+
directive.attributes?.pack == null &&
|
|
61
|
+
directive.attributes?.defaultPack == null
|
|
62
|
+
) {
|
|
63
|
+
attrs.push(jsxAttr('defaultPack', pack));
|
|
64
|
+
}
|
|
65
|
+
const isLeaf = directive.type === 'leafDirective';
|
|
66
|
+
const label = (
|
|
67
|
+
isLeaf
|
|
68
|
+
? extractLeafLabel(directive)
|
|
69
|
+
: extractDirectiveLabel(directive)
|
|
70
|
+
).trim();
|
|
71
|
+
const labelAttr = isLeaf
|
|
72
|
+
? (LEAF_KEY[name] ?? 'id')
|
|
73
|
+
: (LABEL_ATTR[name] ?? 'label');
|
|
74
|
+
if (label && labelAttr) attrs.unshift(jsxAttr(labelAttr, label));
|
|
75
|
+
|
|
76
|
+
let children = isLeaf ? [] : contentChildren(directive);
|
|
77
|
+
if (name === 'multiblock' && !isLeaf) {
|
|
78
|
+
const code = directive.children?.find(
|
|
79
|
+
(child) => child.type === 'code',
|
|
80
|
+
) as { value?: string } | undefined;
|
|
81
|
+
if (code?.value) {
|
|
82
|
+
attrs.unshift(jsxAttr('spec', code.value));
|
|
83
|
+
children = [];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
Object.assign(directive, {
|
|
88
|
+
type: 'mdxJsxFlowElement',
|
|
89
|
+
name: component,
|
|
90
|
+
attributes: attrs,
|
|
91
|
+
children,
|
|
92
|
+
});
|
|
93
|
+
return false;
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function toAttributes(directive: DirectiveNode): JsxAttribute[] {
|
|
99
|
+
return Object.entries(directive.attributes ?? {}).map(([name, value]) => ({
|
|
100
|
+
type: 'mdxJsxAttribute' as const,
|
|
101
|
+
name,
|
|
102
|
+
value:
|
|
103
|
+
value == null || value === ''
|
|
104
|
+
? jsxExpression(estreeBoolean(true))
|
|
105
|
+
: typeof value === 'string'
|
|
106
|
+
? value
|
|
107
|
+
: jsxExpression(estreeString(String(value))),
|
|
108
|
+
}));
|
|
109
|
+
}
|
|
110
|
+
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import type { MdastNode } from './directive-utils.ts';
|
|
2
|
+
|
|
3
|
+
type NodeWithValue = MdastNode & { value: string };
|
|
4
|
+
type NodeWithChildren = MdastNode & { children: MdastNode[] };
|
|
5
|
+
|
|
6
|
+
function objectToCss(body: string): string | undefined {
|
|
7
|
+
const entries: string[] = [];
|
|
8
|
+
const re = /(\w+)\s*:\s*['"]([^'"]*)['"]/g;
|
|
9
|
+
let m: RegExpExecArray | null;
|
|
10
|
+
while ((m = re.exec(body)) !== null) entries.push(`${m[1]}: ${m[2]}`);
|
|
11
|
+
return entries.length ? entries.join('; ') : undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const JSX_ATTR = /(\w[\w-]*)=\{\{([\s\S]*?)\}\}/g;
|
|
15
|
+
|
|
16
|
+
function rewriteAttrs(html: string): string {
|
|
17
|
+
return html.replace(JSX_ATTR, (all, attr: string, body: string) => {
|
|
18
|
+
if (attr !== 'style') return all;
|
|
19
|
+
const css = objectToCss(body);
|
|
20
|
+
return css ? `style="${css}"` : all;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const OPEN_TAG = /<([a-zA-Z][\w-]*)((?:[^>"']|"[^"]*"|'[^']*'|\{\{[\s\S]*?\}\})*?)>/;
|
|
25
|
+
|
|
26
|
+
function stylesFromTag(tag: string): string | undefined {
|
|
27
|
+
const m = /style=\{\{([\s\S]*?)\}\}/.exec(tag);
|
|
28
|
+
return m ? objectToCss(m[1]!) : undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function elementNode(
|
|
32
|
+
tag: string,
|
|
33
|
+
hName: string,
|
|
34
|
+
children: MdastNode[],
|
|
35
|
+
): MdastNode {
|
|
36
|
+
const style = stylesFromTag(tag);
|
|
37
|
+
return {
|
|
38
|
+
type: 'emphasis',
|
|
39
|
+
children,
|
|
40
|
+
data: {
|
|
41
|
+
hName,
|
|
42
|
+
...(style ? { hProperties: { style } } : {}),
|
|
43
|
+
},
|
|
44
|
+
} as MdastNode;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function textNode(value: string): MdastNode {
|
|
48
|
+
return { type: 'text', value } as MdastNode;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function remarkHtmlStyle() {
|
|
52
|
+
return (tree: MdastNode) => {
|
|
53
|
+
const visitChildren = (parent: NodeWithChildren) => {
|
|
54
|
+
const kids = parent.children;
|
|
55
|
+
for (let i = 0; i < kids.length; i++) {
|
|
56
|
+
const child = kids[i]!;
|
|
57
|
+
if (child.type === 'html' && typeof child.value === 'string') {
|
|
58
|
+
(child as NodeWithValue).value = rewriteAttrs(child.value);
|
|
59
|
+
} else if (
|
|
60
|
+
child.type === 'text' &&
|
|
61
|
+
typeof child.value === 'string' &&
|
|
62
|
+
child.value.includes('={{')
|
|
63
|
+
) {
|
|
64
|
+
if (fuseJsxStyleTags(kids, i)) i--;
|
|
65
|
+
} else if (child.children) {
|
|
66
|
+
visitChildren(child as NodeWithChildren);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
visitChildren(tree as NodeWithChildren);
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function fuseJsxStyleTags(kids: MdastNode[], i: number): boolean {
|
|
75
|
+
const text = kids[i] as NodeWithValue;
|
|
76
|
+
let open: RegExpExecArray | null;
|
|
77
|
+
const re = new RegExp(OPEN_TAG.source, 'g');
|
|
78
|
+
do {
|
|
79
|
+
open = re.exec(text.value);
|
|
80
|
+
} while (open && !open[2]?.includes('={{'));
|
|
81
|
+
if (!open) return false;
|
|
82
|
+
|
|
83
|
+
const [openTag, tagName] = open;
|
|
84
|
+
const prefix = text.value.slice(0, open.index);
|
|
85
|
+
const rest = text.value.slice(open.index + openTag.length);
|
|
86
|
+
|
|
87
|
+
const closeSelf = new RegExp(`</${tagName}\\s*>`).exec(rest);
|
|
88
|
+
if (closeSelf) {
|
|
89
|
+
const end = closeSelf.index + closeSelf[0].length;
|
|
90
|
+
kids.splice(
|
|
91
|
+
i,
|
|
92
|
+
1,
|
|
93
|
+
...compact([
|
|
94
|
+
prefix ? textNode(prefix) : undefined,
|
|
95
|
+
elementNode(openTag, tagName, [textNode(rest.slice(0, closeSelf.index))]),
|
|
96
|
+
rest.slice(end) ? textNode(rest.slice(end)) : undefined,
|
|
97
|
+
]),
|
|
98
|
+
);
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
for (let j = i + 1; j < kids.length; j++) {
|
|
103
|
+
const sib = kids[j]!;
|
|
104
|
+
if (
|
|
105
|
+
sib.type === 'html' &&
|
|
106
|
+
typeof sib.value === 'string' &&
|
|
107
|
+
new RegExp(`^</${tagName}\\s*>$`).test(sib.value.trim())
|
|
108
|
+
) {
|
|
109
|
+
const inner = rest ? [textNode(rest)] : [];
|
|
110
|
+
kids.splice(
|
|
111
|
+
i,
|
|
112
|
+
j - i + 1,
|
|
113
|
+
...compact([
|
|
114
|
+
prefix ? textNode(prefix) : undefined,
|
|
115
|
+
elementNode(openTag, tagName, [...inner, ...kids.slice(i + 1, j)]),
|
|
116
|
+
]),
|
|
117
|
+
);
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
kids.splice(
|
|
123
|
+
i,
|
|
124
|
+
1,
|
|
125
|
+
...compact([
|
|
126
|
+
prefix ? textNode(prefix) : undefined,
|
|
127
|
+
elementNode(openTag, tagName, rest ? [textNode(rest)] : []),
|
|
128
|
+
]),
|
|
129
|
+
);
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function compact(nodes: (MdastNode | undefined)[]): MdastNode[] {
|
|
134
|
+
return nodes.filter((n): n is MdastNode => n !== undefined);
|
|
135
|
+
}
|
|
136
|
+
|