@timvir/mdx 0.1.36 → 0.1.39
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/index.js +75 -9
- package/package.json +6 -1
package/index.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import
|
|
1
|
+
import generate from "@babel/generator";
|
|
2
|
+
import { parse } from "@babel/parser";
|
|
3
|
+
import * as t from "@babel/types";
|
|
4
|
+
import * as crypto from "node:crypto";
|
|
5
|
+
import * as espree from "espree";
|
|
6
|
+
import * as fs from "node:fs";
|
|
2
7
|
import { fromMarkdown } from "mdast-util-from-markdown";
|
|
3
8
|
import { mdxFromMarkdown } from "mdast-util-mdx";
|
|
4
9
|
import { mdxjs } from "micromark-extension-mdxjs";
|
|
5
|
-
import * as
|
|
6
|
-
import
|
|
10
|
+
import * as path from "node:path";
|
|
11
|
+
import prettier from "prettier";
|
|
7
12
|
import { visit } from "unist-util-visit";
|
|
8
|
-
import * as espree from "espree";
|
|
9
13
|
|
|
10
14
|
export function remarkPlugin() {
|
|
11
15
|
let counter = 0;
|
|
@@ -32,6 +36,14 @@ export function remarkPlugin() {
|
|
|
32
36
|
*/
|
|
33
37
|
const module = path.join(path.dirname(filename), component, "samples", variant);
|
|
34
38
|
|
|
39
|
+
function loadSource() {
|
|
40
|
+
if (fs.existsSync(module)) {
|
|
41
|
+
return fs.readFileSync(module, "utf8");
|
|
42
|
+
} else {
|
|
43
|
+
return fs.readFileSync(module + ".tsx", "utf8");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
35
47
|
({
|
|
36
48
|
component: () => {
|
|
37
49
|
/*
|
|
@@ -90,12 +102,66 @@ export function remarkPlugin() {
|
|
|
90
102
|
}
|
|
91
103
|
},
|
|
92
104
|
source: () => {
|
|
105
|
+
const source = loadSource();
|
|
106
|
+
|
|
107
|
+
const { children } = fromMarkdown(`{${JSON.stringify(source)}}`, {
|
|
108
|
+
extensions: [mdxjs()],
|
|
109
|
+
mdastExtensions: [mdxFromMarkdown()],
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
for (const [k] of Object.keys(node)) {
|
|
113
|
+
delete node[k];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
for (const [k, v] of Object.entries(children[0])) {
|
|
117
|
+
node[k] = v;
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
[`source/component`]: () => {
|
|
93
121
|
const source = (() => {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
122
|
+
const file = parse(loadSource(), {
|
|
123
|
+
sourceType: "module",
|
|
124
|
+
plugins: ["jsx", "typescript"],
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const exportDefaultDeclaration = file.program.body.find((node) => t.isExportDefaultDeclaration(node));
|
|
128
|
+
const { declaration } = exportDefaultDeclaration;
|
|
129
|
+
const { code } = generate.default(declaration);
|
|
130
|
+
return prettier.format(code, {
|
|
131
|
+
parser: "typescript",
|
|
132
|
+
printWidth: 80,
|
|
133
|
+
});
|
|
134
|
+
})();
|
|
135
|
+
|
|
136
|
+
const { children } = fromMarkdown(`{${JSON.stringify(source)}}`, {
|
|
137
|
+
extensions: [mdxjs()],
|
|
138
|
+
mdastExtensions: [mdxFromMarkdown()],
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
for (const [k] of Object.keys(node)) {
|
|
142
|
+
delete node[k];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
for (const [k, v] of Object.entries(children[0])) {
|
|
146
|
+
node[k] = v;
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
[`source/markup`]: () => {
|
|
150
|
+
const source = (() => {
|
|
151
|
+
const file = parse(loadSource(), {
|
|
152
|
+
sourceType: "module",
|
|
153
|
+
plugins: ["jsx", "typescript"],
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const exportDefaultDeclaration = file.program.body.find((node) => t.isExportDefaultDeclaration(node));
|
|
157
|
+
const { declaration } = exportDefaultDeclaration;
|
|
158
|
+
const body = declaration.body.body;
|
|
159
|
+
const returnStatement = body.find((node) => t.isReturnStatement(node));
|
|
160
|
+
const { code } = generate.default(returnStatement.argument);
|
|
161
|
+
return prettier.format(code, {
|
|
162
|
+
parser: "mdx",
|
|
163
|
+
printWidth: 80,
|
|
164
|
+
});
|
|
99
165
|
})();
|
|
100
166
|
|
|
101
167
|
const { children } = fromMarkdown(`{${JSON.stringify(source)}}`, {
|
package/package.json
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@timvir/mdx",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.39",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"exports": {
|
|
8
8
|
".": "./index.js"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
+
"@babel/generator": "^7.17.0",
|
|
12
|
+
"@babel/parser": "^7.17.0",
|
|
13
|
+
"@babel/types": "^7.17.0",
|
|
14
|
+
"espree": "^9.3.1",
|
|
11
15
|
"mdast-util-from-markdown": "^1.2.0",
|
|
12
16
|
"mdast-util-mdx": "^2.0.0",
|
|
13
17
|
"micromark-extension-mdxjs": "^1.0.0",
|
|
18
|
+
"prettier": "^2.6.2",
|
|
14
19
|
"unist-util-visit": "^4.1.0"
|
|
15
20
|
},
|
|
16
21
|
"repository": {
|