@terrafirmagreg-team/mdx 1.2.3 → 1.2.5

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.
@@ -1,110 +0,0 @@
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
-
@@ -1,136 +0,0 @@
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
-