@bjmhe/automd 0.0.0 → 0.0.4
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/README.md +4 -5
- package/dist/_parse-KesGTpQx.d.ts +49 -0
- package/dist/_parse.d.ts +4 -0
- package/dist/_parse.js +67 -0
- package/dist/_parse.js.map +1 -0
- package/dist/_utils.d.ts +18 -0
- package/dist/_utils.js +36 -0
- package/dist/_utils.js.map +1 -0
- package/dist/automd-C2wjw_SG.d.ts +131 -0
- package/dist/automd.d.ts +4 -0
- package/dist/automd.js +88 -0
- package/dist/automd.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +107 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +4 -0
- package/dist/config.js +41 -0
- package/dist/config.js.map +1 -0
- package/dist/generator.d.ts +4 -0
- package/dist/generator.js +11 -0
- package/dist/generator.js.map +1 -0
- package/dist/index.d.ts +2 -195
- package/dist/index.js +5 -806
- package/dist/transform-BOfYBrHa.js +599 -0
- package/dist/transform-BOfYBrHa.js.map +1 -0
- package/dist/transform.d.ts +4 -0
- package/dist/transform.js +6 -0
- package/package.json +24 -19
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
/*! Keep it simple, keep it free */
|
|
2
|
+
import { containsAutomd, findBlocks, parseRawArgs } from "./_parse.js";
|
|
3
|
+
import { getPkg, resolvePath as resolvePath$1 } from "./_utils.js";
|
|
4
|
+
import { resolveConfig } from "./config.js";
|
|
5
|
+
import { defineGenerator } from "./generator.js";
|
|
6
|
+
import { titleCase } from "scule";
|
|
7
|
+
import { findExportNames, resolvePath } from "mlly";
|
|
8
|
+
import { basename, extname, join } from "pathe";
|
|
9
|
+
import MagicString from "magic-string";
|
|
10
|
+
import { image, link, md } from "mdbox";
|
|
11
|
+
import { readFile, readdir, stat } from "node:fs/promises";
|
|
12
|
+
//#region src/generators/badges.ts
|
|
13
|
+
const badgeTypes = {
|
|
14
|
+
npmVersion: {
|
|
15
|
+
name: "npm version",
|
|
16
|
+
to: "https://npmjs.com/package/{name}"
|
|
17
|
+
},
|
|
18
|
+
npmDownloads: {
|
|
19
|
+
name: "npm downloads",
|
|
20
|
+
to: "https://npm.chart.dev/{name}"
|
|
21
|
+
},
|
|
22
|
+
bundlephobia: {
|
|
23
|
+
name: "bundle size",
|
|
24
|
+
to: "https://bundlephobia.com/package/{name}"
|
|
25
|
+
},
|
|
26
|
+
bundlejs: {
|
|
27
|
+
name: "bundle size",
|
|
28
|
+
to: "https://bundlejs.com/?q={name}"
|
|
29
|
+
},
|
|
30
|
+
packagephobia: {
|
|
31
|
+
name: "install size",
|
|
32
|
+
to: "https://packagephobia.com/result?p={name}"
|
|
33
|
+
},
|
|
34
|
+
codecov: {
|
|
35
|
+
name: "codecov",
|
|
36
|
+
to: "https://codecov.io/gh/{github}"
|
|
37
|
+
},
|
|
38
|
+
license: {
|
|
39
|
+
name: "license",
|
|
40
|
+
to: "https://github.com/{github}/blob/{licenseBranch}/LICENSE"
|
|
41
|
+
},
|
|
42
|
+
engine: {
|
|
43
|
+
name: "engine",
|
|
44
|
+
to: "https://npmx.dev/api/registry/badge/engines/{name}"
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const badgeProviders = {
|
|
48
|
+
shields: {
|
|
49
|
+
npmVersion: "https://img.shields.io/npm/v/{name}",
|
|
50
|
+
npmDownloads: "https://img.shields.io/npm/dm/{name}",
|
|
51
|
+
bundlephobia: "https://img.shields.io/bundlephobia/minzip/{name}",
|
|
52
|
+
packagephobia: "https://badgen.net/packagephobia/install/{name}",
|
|
53
|
+
bundlejs: "https://img.shields.io/bundlejs/size/{name}",
|
|
54
|
+
codecov: "https://img.shields.io/codecov/c/gh/{github}",
|
|
55
|
+
license: "https://img.shields.io/github/license/{github}",
|
|
56
|
+
engine: false
|
|
57
|
+
},
|
|
58
|
+
badgen: {
|
|
59
|
+
npmVersion: "https://flat.badgen.net/npm/v/{name}",
|
|
60
|
+
npmDownloads: "https://flat.badgen.net/npm/dm/{name}",
|
|
61
|
+
bundlephobia: "https://flat.badgen.net/bundlephobia/minzip/{name}",
|
|
62
|
+
bundlejs: false,
|
|
63
|
+
packagephobia: "https://flat.badgen.net/packagephobia/install/{name}",
|
|
64
|
+
codecov: "https://flat.badgen.net/codecov/c/github/{github}",
|
|
65
|
+
license: "https://flat.badgen.net/github/license/{github}",
|
|
66
|
+
engine: false
|
|
67
|
+
},
|
|
68
|
+
badgenClassic: {
|
|
69
|
+
npmVersion: "https://badgen.net/npm/v/{name}",
|
|
70
|
+
npmDownloads: "https://badgen.net/npm/dm/{name}",
|
|
71
|
+
bundlephobia: "https://badgen.net/bundlephobia/minzip/{name}",
|
|
72
|
+
bundlejs: false,
|
|
73
|
+
packagephobia: "https://badgen.net/packagephobia/install/{name}",
|
|
74
|
+
codecov: "https://badgen.net/codecov/c/github/{github}",
|
|
75
|
+
license: "https://badgen.net/github/license/{github}",
|
|
76
|
+
engine: false
|
|
77
|
+
},
|
|
78
|
+
npmx: {
|
|
79
|
+
npmVersion: "https://npmx.dev/api/registry/badge/version/{name}",
|
|
80
|
+
npmDownloads: "https://npmx.dev/api/registry/badge/downloads/{name}",
|
|
81
|
+
bundlephobia: "https://npmx.dev/api/registry/badge/size/{name}",
|
|
82
|
+
bundlejs: false,
|
|
83
|
+
packagephobia: false,
|
|
84
|
+
codecov: false,
|
|
85
|
+
license: "https://npmx.dev/api/registry/badge/license/{name}",
|
|
86
|
+
engine: "https://npmx.dev/api/registry/badge/engines/{name}"
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
const badges = defineGenerator({
|
|
90
|
+
name: "badges",
|
|
91
|
+
async generate({ config, args }) {
|
|
92
|
+
const pkg = await getPkg(config.dir, args);
|
|
93
|
+
const ctx = {
|
|
94
|
+
name: pkg.name,
|
|
95
|
+
github: pkg.github,
|
|
96
|
+
licenseBranch: "main",
|
|
97
|
+
...args
|
|
98
|
+
};
|
|
99
|
+
const fillStr = (str) => str.replace(/{(\w+)}/g, (_, key) => ctx[key] || "");
|
|
100
|
+
const provider = badgeProviders[args.provider] || badgeProviders.shields;
|
|
101
|
+
const providerParams = Object.entries({
|
|
102
|
+
color: args.color,
|
|
103
|
+
labelColor: args.labelColor,
|
|
104
|
+
...args.styleParams
|
|
105
|
+
}).filter(([, value]) => value).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join("&");
|
|
106
|
+
const badges = {
|
|
107
|
+
npmVersion: {
|
|
108
|
+
enabled: ctx.name && args.npmVersion !== false,
|
|
109
|
+
...badgeTypes.npmVersion
|
|
110
|
+
},
|
|
111
|
+
npmDownloads: {
|
|
112
|
+
enabled: ctx.name && args.npmDownloads !== false,
|
|
113
|
+
...badgeTypes.npmDownloads
|
|
114
|
+
},
|
|
115
|
+
bundlephobia: {
|
|
116
|
+
enabled: args.bundlephobia && ctx.name,
|
|
117
|
+
...badgeTypes.bundlephobia
|
|
118
|
+
},
|
|
119
|
+
bundlejs: {
|
|
120
|
+
enabled: args.bundlejs && ctx.name,
|
|
121
|
+
...badgeTypes.bundlejs
|
|
122
|
+
},
|
|
123
|
+
packagephobia: {
|
|
124
|
+
enabled: args.packagephobia && ctx.name,
|
|
125
|
+
...badgeTypes.packagephobia
|
|
126
|
+
},
|
|
127
|
+
codecov: {
|
|
128
|
+
enabled: args.codecov && ctx.github,
|
|
129
|
+
...badgeTypes.codecov
|
|
130
|
+
},
|
|
131
|
+
license: {
|
|
132
|
+
enabled: args.license && ctx.github,
|
|
133
|
+
...badgeTypes.license
|
|
134
|
+
},
|
|
135
|
+
engine: {
|
|
136
|
+
enabled: args.engine && ctx.name,
|
|
137
|
+
...badgeTypes.engine
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
const md = [];
|
|
141
|
+
for (const [badgeType, badge] of Object.entries(badges)) {
|
|
142
|
+
if (!badge.enabled || !provider[badgeType]) continue;
|
|
143
|
+
const to = fillStr(badge.to);
|
|
144
|
+
const imgURL = fillStr(provider[badgeType]) + (providerParams ? `?${providerParams}` : "");
|
|
145
|
+
md.push(link(to, image(imgURL, badge.name)));
|
|
146
|
+
}
|
|
147
|
+
return { contents: md.join("\n") };
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
//#endregion
|
|
151
|
+
//#region src/generators/contributors.ts
|
|
152
|
+
const PROVIDERS = {
|
|
153
|
+
CONTRIB_ROCKS: "contrib.rocks",
|
|
154
|
+
MARKUPGO: "markupgo"
|
|
155
|
+
};
|
|
156
|
+
const contributors = defineGenerator({
|
|
157
|
+
name: "contributors",
|
|
158
|
+
async generate({ config, args }) {
|
|
159
|
+
const { github } = await getPkg(config.dir, args);
|
|
160
|
+
const provider = args.provider || PROVIDERS.CONTRIB_ROCKS;
|
|
161
|
+
if (!github) throw new Error("`github` is required!");
|
|
162
|
+
const lines = [];
|
|
163
|
+
if (typeof args.license === "string") lines.push(`Published under the [${args.license.toUpperCase()}](https://github.com/${github}/blob/main/LICENSE) license.`);
|
|
164
|
+
let madeBy = `[community](https://github.com/${github}/graphs/contributors) 💛`;
|
|
165
|
+
if (typeof args.author === "string") {
|
|
166
|
+
const authors = args.author.split(",").map((author) => author.trim()).map((user) => `[@${user}](https://github.com/${user})`).join(", ");
|
|
167
|
+
if (authors.length > 0) madeBy = `${authors} and ${madeBy}`;
|
|
168
|
+
}
|
|
169
|
+
lines.push(`Made by ${madeBy}`);
|
|
170
|
+
if (provider === PROVIDERS.MARKUPGO) {
|
|
171
|
+
const params = [];
|
|
172
|
+
args = {
|
|
173
|
+
circleSize: "64",
|
|
174
|
+
center: "true",
|
|
175
|
+
...args
|
|
176
|
+
};
|
|
177
|
+
if (Number(args.max) >= 0) params.push(["count", args.max]);
|
|
178
|
+
if (Number(args.width)) params.push(["width", args.width]);
|
|
179
|
+
if (Number(args.circleSize)) params.push(["circleSize", args.circleSize]);
|
|
180
|
+
if (Number(args.circleRadius)) params.push(["circleRadius", args.circleRadius]);
|
|
181
|
+
if (Number(args.circleSpacing)) params.push(["circleSpacing", args.circleSpacing]);
|
|
182
|
+
if (args.center) params.push(["center", Boolean(args.center).toString()]);
|
|
183
|
+
if (!args.markupGoLogo) params.push(["removeLogo", "true"]);
|
|
184
|
+
if (args.anon) params.push(["anon", Boolean(args.anon).toString()]);
|
|
185
|
+
let paramsStr = params.map(([k, v]) => `${k}=${v}`).join("&");
|
|
186
|
+
paramsStr = paramsStr ? `?${paramsStr}` : "";
|
|
187
|
+
lines.push(`<br><br>`, `<a href="https://github.com/${github}/graphs/contributors">`, `<img src="https://markupgo.com/github/${github}/contributors${paramsStr}" />`, `</a>`);
|
|
188
|
+
} else {
|
|
189
|
+
const params = [["repo", github]];
|
|
190
|
+
if (args.max) params.push(["max", args.max]);
|
|
191
|
+
if (args.anon) params.push(["anon", args.anon]);
|
|
192
|
+
const paramsStr = params.map(([k, v]) => `${k}=${v}`).join("&");
|
|
193
|
+
lines.push(`<br><br>`, `<a href="https://github.com/${github}/graphs/contributors">`, `<img src="https://contrib.rocks/image?${paramsStr}" />`, `</a>`);
|
|
194
|
+
}
|
|
195
|
+
return { contents: lines.join("\n") };
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
//#endregion
|
|
199
|
+
//#region src/generators/dir-tree.ts
|
|
200
|
+
const DEFAULT_IGNORE = [
|
|
201
|
+
"node_modules",
|
|
202
|
+
".git",
|
|
203
|
+
".DS_Store",
|
|
204
|
+
".nuxt",
|
|
205
|
+
".output",
|
|
206
|
+
".nitro",
|
|
207
|
+
"dist",
|
|
208
|
+
"coverage",
|
|
209
|
+
".cache",
|
|
210
|
+
".turbo"
|
|
211
|
+
];
|
|
212
|
+
async function parseGitignore(dir) {
|
|
213
|
+
try {
|
|
214
|
+
return (await readFile(join(dir, ".gitignore"), "utf8")).split("\n").map((line) => line.trim()).filter((line) => line && !line.startsWith("#"));
|
|
215
|
+
} catch {
|
|
216
|
+
return [];
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
function shouldIgnore(name, ignorePatterns, defaultIgnore) {
|
|
220
|
+
const allPatterns = [...defaultIgnore, ...ignorePatterns];
|
|
221
|
+
for (const pattern of allPatterns) {
|
|
222
|
+
if (name === pattern.replace(/^\//, "").replace(/\/$/, "")) return true;
|
|
223
|
+
if (pattern.startsWith("*") && name.endsWith(pattern.slice(1))) return true;
|
|
224
|
+
if (pattern.endsWith("*") && name.startsWith(pattern.slice(0, -1))) return true;
|
|
225
|
+
}
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
async function buildTree(dir, ignorePatterns, maxDepth, currentDepth = 0) {
|
|
229
|
+
if (maxDepth > 0 && currentDepth >= maxDepth) return [];
|
|
230
|
+
const entries = await readdir(dir);
|
|
231
|
+
const result = [];
|
|
232
|
+
for (const entry of entries) {
|
|
233
|
+
if (shouldIgnore(entry, ignorePatterns, DEFAULT_IGNORE)) continue;
|
|
234
|
+
const fullPath = join(dir, entry);
|
|
235
|
+
const isDirectory = (await stat(fullPath)).isDirectory();
|
|
236
|
+
const treeEntry = {
|
|
237
|
+
name: entry,
|
|
238
|
+
isDirectory
|
|
239
|
+
};
|
|
240
|
+
if (isDirectory) treeEntry.children = await buildTree(fullPath, ignorePatterns, maxDepth, currentDepth + 1);
|
|
241
|
+
result.push(treeEntry);
|
|
242
|
+
}
|
|
243
|
+
result.sort((a, b) => {
|
|
244
|
+
if (a.isDirectory !== b.isDirectory) return a.isDirectory ? -1 : 1;
|
|
245
|
+
return a.name.localeCompare(b.name);
|
|
246
|
+
});
|
|
247
|
+
return result;
|
|
248
|
+
}
|
|
249
|
+
function renderTree(entries, isLast = []) {
|
|
250
|
+
const lines = [];
|
|
251
|
+
for (let i = 0; i < entries.length; i++) {
|
|
252
|
+
const entry = entries[i];
|
|
253
|
+
const isLastEntry = i === entries.length - 1;
|
|
254
|
+
let linePrefix = "";
|
|
255
|
+
for (const element_ of isLast) linePrefix += element_ ? " " : "│ ";
|
|
256
|
+
const connector = isLastEntry ? "└── " : "├── ";
|
|
257
|
+
const suffix = entry.isDirectory ? "/" : "";
|
|
258
|
+
lines.push(`${linePrefix}${connector}${entry.name}${suffix}`);
|
|
259
|
+
if (entry.children && entry.children.length > 0) {
|
|
260
|
+
const childLines = renderTree(entry.children, [...isLast, isLastEntry]);
|
|
261
|
+
lines.push(...childLines);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return lines;
|
|
265
|
+
}
|
|
266
|
+
const dirTree = defineGenerator({
|
|
267
|
+
name: "dir-tree",
|
|
268
|
+
async generate({ args, config, url }) {
|
|
269
|
+
const srcPath = args.src || ".";
|
|
270
|
+
const fullPath = resolvePath$1(srcPath, {
|
|
271
|
+
url,
|
|
272
|
+
dir: config.dir
|
|
273
|
+
});
|
|
274
|
+
if (!(await stat(fullPath)).isDirectory()) throw new Error(`Path "${srcPath}" is not a directory`);
|
|
275
|
+
const userIgnore = args.ignore ? String(args.ignore).split(",").map((s) => s.trim()) : [];
|
|
276
|
+
return { contents: "```\n" + renderTree(await buildTree(fullPath, [...await parseGitignore(fullPath), ...userIgnore], args.maxDepth ? Number(args.maxDepth) : 0)).join("\n") + "\n```" };
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
//#endregion
|
|
280
|
+
//#region src/generators/fetch.ts
|
|
281
|
+
const fetch = defineGenerator({
|
|
282
|
+
name: "fetch",
|
|
283
|
+
async generate({ args }) {
|
|
284
|
+
const { $fetch } = await import("ofetch");
|
|
285
|
+
let url = args.url;
|
|
286
|
+
if (!url) throw new Error("URL is required!");
|
|
287
|
+
if (url.startsWith("gh:")) url = `https://raw.githubusercontent.com/${url.slice(3)}`;
|
|
288
|
+
return { contents: await $fetch(url) };
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
//#endregion
|
|
292
|
+
//#region src/generators/file.ts
|
|
293
|
+
const file = defineGenerator({
|
|
294
|
+
name: "file",
|
|
295
|
+
async generate({ args, config, url }) {
|
|
296
|
+
const fullPath = resolvePath$1(args.src, {
|
|
297
|
+
url,
|
|
298
|
+
dir: config.dir
|
|
299
|
+
});
|
|
300
|
+
let contents = await readFile(fullPath, "utf8");
|
|
301
|
+
if (!args.noTrim) contents = contents.trim();
|
|
302
|
+
if (args.lines) {
|
|
303
|
+
const groups = /^(?<startLine>\d+)?:(?<endLine>\d+)?$/.exec(args.lines)?.groups;
|
|
304
|
+
if (!groups) throw new Error("invalid lines format");
|
|
305
|
+
const lines = contents.split("\n");
|
|
306
|
+
const startLine = Number(groups.startLine) || 1;
|
|
307
|
+
const endLine = Number(groups.endLine) || lines.length;
|
|
308
|
+
if (startLine < 1) throw new Error("first line's index can not be smaller than 1");
|
|
309
|
+
contents = lines.slice(startLine - 1, endLine).join("\n");
|
|
310
|
+
}
|
|
311
|
+
if (args.code) contents = md.codeBlock(contents, args.lang || extname(fullPath).slice(1), { ext: args.name === false ? void 0 : typeof args.name === "string" ? args.name : `[${basename(fullPath)}]` });
|
|
312
|
+
return { contents };
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
//#endregion
|
|
316
|
+
//#region src/generators/jsdocs.ts
|
|
317
|
+
const jsdocs = defineGenerator({
|
|
318
|
+
name: "jsdocs",
|
|
319
|
+
async generate({ config, args, url }) {
|
|
320
|
+
const { loadSchema } = await import("untyped/loader");
|
|
321
|
+
return { contents: _render(await loadSchema(resolvePath$1(args.src, {
|
|
322
|
+
url,
|
|
323
|
+
dir: config.dir
|
|
324
|
+
}), { jiti: {
|
|
325
|
+
fsCache: false,
|
|
326
|
+
moduleCache: false
|
|
327
|
+
} }), args, Number.parseInt(args.headingLevel) || 2).join("\n").replace(/\n{3,}/g, "\n\n") };
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
function _render(schema, opts, headingLevel) {
|
|
331
|
+
const sections = Object.create(null);
|
|
332
|
+
for (const [key, keySchema] of Object.entries(schema.properties || {})) {
|
|
333
|
+
const section = _renderSection(key, keySchema, opts, headingLevel + 1);
|
|
334
|
+
if (!section) continue;
|
|
335
|
+
sections[section.group] = sections[section.group] || [];
|
|
336
|
+
sections[section.group].push([section.heading, section.lines]);
|
|
337
|
+
}
|
|
338
|
+
const lines = [];
|
|
339
|
+
const sortedGroups = Object.keys(sections).sort((a, b) => {
|
|
340
|
+
if (a === "") return 1;
|
|
341
|
+
if (b === "") return -1;
|
|
342
|
+
return a.localeCompare(b);
|
|
343
|
+
});
|
|
344
|
+
for (const group of sortedGroups) {
|
|
345
|
+
if (group) lines.push(`\n${"#".repeat(headingLevel)} ${titleCase(group)}\n`);
|
|
346
|
+
const sortedSections = sections[group].sort((a, b) => a[0].localeCompare(b[0]));
|
|
347
|
+
for (const section of sortedSections) {
|
|
348
|
+
const heading = `\n${"#".repeat(headingLevel + 1)} ${section[0]}\n`;
|
|
349
|
+
lines.push(heading, ...section[1]);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return lines;
|
|
353
|
+
}
|
|
354
|
+
function _renderSection(key, schema, opts, headingLevel) {
|
|
355
|
+
const tags = _parseTags(schema.tags);
|
|
356
|
+
if (tags.some((t) => t.tag === "@deprecated" || t.tag === "@internal")) return;
|
|
357
|
+
const group = tags.find((t) => t.tag === "@group")?.contents || opts.defaultGroup || "";
|
|
358
|
+
if (opts.group && (typeof opts.group === "string" ? group !== opts.group : !opts.group.includes(group))) return;
|
|
359
|
+
let heading = `\`${key}\``;
|
|
360
|
+
const lines = [];
|
|
361
|
+
if (schema.type === "function") heading = `\`${_generateFunctionSig(key, schema)}\``;
|
|
362
|
+
else if (schema.type !== "object") {
|
|
363
|
+
lines.push(`- **Type**: \`${schema.markdownType || schema.tsType || schema.type}\``);
|
|
364
|
+
if ("default" in schema) lines.push(`- **Default**: \`${JSON.stringify(schema.default)}\``);
|
|
365
|
+
lines.push("");
|
|
366
|
+
}
|
|
367
|
+
lines.push(..._renderBody(schema));
|
|
368
|
+
for (const tag of tags) if (tag.tag === "@example") {
|
|
369
|
+
const codeBlock = tag.contents.startsWith("`") ? tag.contents : `\`\`\`ts\n${tag.contents}\n\`\`\``;
|
|
370
|
+
lines.push("", "**Example:**", "", codeBlock);
|
|
371
|
+
}
|
|
372
|
+
if (schema.type === "object") lines.push(..._render(schema, opts, headingLevel));
|
|
373
|
+
return {
|
|
374
|
+
heading,
|
|
375
|
+
lines,
|
|
376
|
+
group
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
function _renderBody(schema) {
|
|
380
|
+
const lines = [];
|
|
381
|
+
if (schema.title) lines.push(schema.title.trim());
|
|
382
|
+
if (schema.title && schema.description) lines.push("");
|
|
383
|
+
if (schema.description) lines.push(...schema.description.split("\n").map((line) => line.trim()).join("\n\n").split("\n"));
|
|
384
|
+
return lines;
|
|
385
|
+
}
|
|
386
|
+
function _parseTags(lines = []) {
|
|
387
|
+
const parsedTags = [];
|
|
388
|
+
let tag = "";
|
|
389
|
+
let contentLines = [];
|
|
390
|
+
for (const line of lines.join("\n").split("\n")) if (line.startsWith("@")) {
|
|
391
|
+
if (tag) parsedTags.push({
|
|
392
|
+
tag,
|
|
393
|
+
contents: contentLines.join("\n")
|
|
394
|
+
});
|
|
395
|
+
const [_tag, ...rest] = line.split(" ");
|
|
396
|
+
tag = _tag;
|
|
397
|
+
contentLines = rest;
|
|
398
|
+
} else contentLines.push(line);
|
|
399
|
+
if (tag) parsedTags.push({
|
|
400
|
+
tag,
|
|
401
|
+
contents: contentLines.join("\n")
|
|
402
|
+
});
|
|
403
|
+
return parsedTags;
|
|
404
|
+
}
|
|
405
|
+
function _generateFunctionSig(name, meta) {
|
|
406
|
+
return `${name}(${(meta.args || []).map((arg) => {
|
|
407
|
+
let str = arg.name;
|
|
408
|
+
if (arg.optional) str += "?";
|
|
409
|
+
const tsType = _simpleArgType(arg.tsType);
|
|
410
|
+
if (tsType) str += `: ${tsType}`;
|
|
411
|
+
return str;
|
|
412
|
+
}).join(", ")})`;
|
|
413
|
+
}
|
|
414
|
+
function _simpleArgType(tsType = "") {
|
|
415
|
+
return tsType.split(/\s*\|\s*/).filter((t) => t && t !== "object" && t.startsWith("{")).map((ot) => ot.split(/\s*[,;]\s*/g).map((p) => p.replaceAll(/\s*:\s*(string|boolean|number)/g, "")).join(", ")).join(" | ");
|
|
416
|
+
}
|
|
417
|
+
//#endregion
|
|
418
|
+
//#region src/generators/jsimport.ts
|
|
419
|
+
const DEFAULT_CDN = "https://esm.sh/";
|
|
420
|
+
const jsimport = defineGenerator({
|
|
421
|
+
name: "jsimport",
|
|
422
|
+
async generate({ config, args }) {
|
|
423
|
+
const { name } = await getPkg(config.dir, args);
|
|
424
|
+
const importPath = name + (args.path || "");
|
|
425
|
+
const importNames = [].concat(args.import, args.imports).filter(Boolean).flatMap((i) => i.split(/\s*,\s*/));
|
|
426
|
+
if (args.src) {
|
|
427
|
+
const exportNames = findExportNames(await readFile(await resolvePath(args.src, { url: config.dir }), "utf8"));
|
|
428
|
+
if (exportNames && exportNames.length > 0) importNames.push(...exportNames);
|
|
429
|
+
}
|
|
430
|
+
const lines = [];
|
|
431
|
+
const fmtImports = importNames.length > 1 ? `\n${importNames.map((i) => " " + i + ",").join("\n")}\n` : importNames[0] && ` ${importNames[0]} ` || "";
|
|
432
|
+
const formatMultiLine = (str) => {
|
|
433
|
+
return str.length > (args.printWidth || 80) ? str : str.replace(/\n/g, "").replace(/,\s*}/, "}").replace(/(\w)}/, "$1 }").replace(/\s+/g, " ");
|
|
434
|
+
};
|
|
435
|
+
if (args.esm !== false) {
|
|
436
|
+
const code = formatMultiLine(`import {${fmtImports}} from "${importPath}";`);
|
|
437
|
+
lines.push("**ESM** (Node.js, Bun, Deno)", md.codeBlock(code, "js"));
|
|
438
|
+
}
|
|
439
|
+
if (args.cjs) {
|
|
440
|
+
const code = formatMultiLine(`const {${fmtImports}} = require("${importPath}");`);
|
|
441
|
+
lines.push("**CommonJS** (Legacy Node.js)", md.codeBlock(code, "js"));
|
|
442
|
+
}
|
|
443
|
+
if (args.cdn) {
|
|
444
|
+
const code = formatMultiLine(`import {${fmtImports}} from "${typeof args.cdn === "string" ? args.cdn : DEFAULT_CDN}${importPath}";`);
|
|
445
|
+
lines.push("**CDN** (Deno and Browsers)", md.codeBlock(code, "js"));
|
|
446
|
+
}
|
|
447
|
+
return { contents: lines.join("\n\n") };
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
//#endregion
|
|
451
|
+
//#region src/generators/pm.ts
|
|
452
|
+
const INSTALL_COMMANDS = [
|
|
453
|
+
["npm", "install"],
|
|
454
|
+
["yarn", "add"],
|
|
455
|
+
["pnpm", "add"],
|
|
456
|
+
["bun", "install"],
|
|
457
|
+
[
|
|
458
|
+
"deno",
|
|
459
|
+
"install",
|
|
460
|
+
" --dev",
|
|
461
|
+
"npm:"
|
|
462
|
+
]
|
|
463
|
+
];
|
|
464
|
+
const NYPM_COMMAND = ["npx nypm", "install"];
|
|
465
|
+
const RUN_COMMANDS = [
|
|
466
|
+
["npm", "npx "],
|
|
467
|
+
["pnpm", "pnpm dlx "],
|
|
468
|
+
["bun", "bunx "],
|
|
469
|
+
["deno", "deno run -A npm:"]
|
|
470
|
+
];
|
|
471
|
+
const pmInstall = defineGenerator({
|
|
472
|
+
name: "pm-install",
|
|
473
|
+
async generate({ config, args }) {
|
|
474
|
+
const { name, version } = await getPkg(config.dir, args);
|
|
475
|
+
if (!name) return { contents: "<!-- package name is unspecified -->" };
|
|
476
|
+
let versionSuffix = "";
|
|
477
|
+
if (args.version) versionSuffix = typeof args.version === "string" ? `@${args.version}` : `@^${version}`;
|
|
478
|
+
const contents = (args.auto === false ? INSTALL_COMMANDS : [NYPM_COMMAND, ...INSTALL_COMMANDS]).map(([cmd, install, dev = " -D", pkgPrefix = ""]) => `# ${cmd.includes("nypm") ? "✨ Auto-detect" : cmd}\n${cmd} ${install}${args.dev ? dev : args.global ? "g" : ""} ${pkgPrefix}${name}${versionSuffix}`);
|
|
479
|
+
if ((args.separate ?? false) === false) return { contents: md.codeBlock(contents.join("\n\n"), "sh") };
|
|
480
|
+
return { contents: contents.map((cmd) => md.codeBlock(cmd, "sh")).join("\n\n") };
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
//#endregion
|
|
484
|
+
//#region src/generators/index.ts
|
|
485
|
+
var generators_default = {
|
|
486
|
+
jsdocs,
|
|
487
|
+
badges,
|
|
488
|
+
"pm-i": pmInstall,
|
|
489
|
+
"pm-install": pmInstall,
|
|
490
|
+
"pm-x": defineGenerator({
|
|
491
|
+
name: "pm-x",
|
|
492
|
+
async generate({ config, args }) {
|
|
493
|
+
const { name, version } = await getPkg(config.dir, args);
|
|
494
|
+
if (!name) return { contents: "<!-- package name is unspecified -->" };
|
|
495
|
+
let versionSuffix = "";
|
|
496
|
+
if (args.version) versionSuffix = typeof args.version === "string" ? `@${args.version}` : `@${version}`;
|
|
497
|
+
const contents = RUN_COMMANDS.map(([pm, cmd]) => `# ${pm}\n${cmd}${name}${versionSuffix}${args.args ? ` ${args.args}` : ""}`);
|
|
498
|
+
if ((args.separate ?? false) === false) return { contents: md.codeBlock(contents.join("\n\n"), "sh") };
|
|
499
|
+
return { contents: contents.map((cmd) => md.codeBlock(cmd, "sh")).join("\n\n") };
|
|
500
|
+
}
|
|
501
|
+
}),
|
|
502
|
+
fetch,
|
|
503
|
+
file,
|
|
504
|
+
jsimport,
|
|
505
|
+
"with-automd": defineGenerator({
|
|
506
|
+
name: "with-automd",
|
|
507
|
+
generate({ args }) {
|
|
508
|
+
const lastUpdate = args.lastUpdate ? ` (last updated: ${typeof args.lastUpdate === "string" ? args.lastUpdate : (/* @__PURE__ */ new Date()).toDateString()})` : "";
|
|
509
|
+
const emoji = args.emoji === false ? "" : "🤖 ";
|
|
510
|
+
const lines = [];
|
|
511
|
+
if (args.separator !== false) lines.push("---", "");
|
|
512
|
+
lines.push(`_${emoji}auto updated with [automd](https://automd.unjs.io)${lastUpdate}_`);
|
|
513
|
+
return { contents: lines.join("\n") };
|
|
514
|
+
}
|
|
515
|
+
}),
|
|
516
|
+
contributors,
|
|
517
|
+
"dir-tree": dirTree
|
|
518
|
+
};
|
|
519
|
+
//#endregion
|
|
520
|
+
//#region src/transform.ts
|
|
521
|
+
/**
|
|
522
|
+
* Edits a markdown document based on certain rules and configurations.
|
|
523
|
+
*
|
|
524
|
+
* @param contents - The text of the markdown document you want to edit.
|
|
525
|
+
* @param _config - Optional. The settings that affect how the document will be edited. See
|
|
526
|
+
* {@link Config}.
|
|
527
|
+
* @param url - Optional. The URL associated with the document, if any.
|
|
528
|
+
* @returns - The result of the transformation, including any changes made and how long it took. See
|
|
529
|
+
* {@link TransformResult}.
|
|
530
|
+
*/
|
|
531
|
+
async function transform(contents, _config, url) {
|
|
532
|
+
const start = performance.now();
|
|
533
|
+
const config = resolveConfig(_config);
|
|
534
|
+
const editor = new MagicString(contents);
|
|
535
|
+
const updates = [];
|
|
536
|
+
const generators = {
|
|
537
|
+
...generators_default,
|
|
538
|
+
...config.generators
|
|
539
|
+
};
|
|
540
|
+
const blocks = findBlocks(contents);
|
|
541
|
+
for (const block of blocks) {
|
|
542
|
+
const result = await _transformBlock(block, config, generators, url);
|
|
543
|
+
if (result.unwrap) editor.overwrite(block._loc.start, block._loc.end, `${result.contents.trim()}`);
|
|
544
|
+
else editor.overwrite(block.loc.start, block.loc.end, `\n\n${result.contents.trim()}\n\n`);
|
|
545
|
+
updates.push({
|
|
546
|
+
block,
|
|
547
|
+
result
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
const hasChanged = editor.hasChanged();
|
|
551
|
+
const hasIssues = updates.some((u) => u.result.issues?.filter(Boolean).length);
|
|
552
|
+
const time = performance.now() - start;
|
|
553
|
+
return {
|
|
554
|
+
hasChanged,
|
|
555
|
+
hasIssues,
|
|
556
|
+
contents: hasChanged ? editor.toString() : contents,
|
|
557
|
+
updates,
|
|
558
|
+
time
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
async function _transformBlock(block, config, generators, url) {
|
|
562
|
+
const args = parseRawArgs(block.rawArgs);
|
|
563
|
+
const generator = generators[block.generator];
|
|
564
|
+
if (!generator) {
|
|
565
|
+
const suggestions = (await import("didyoumean2").then((r) => r.default || r))(block.generator, Object.keys(generators));
|
|
566
|
+
const error = `Unknown generator:\`${block.generator}\`.${suggestions ? ` Did you mean "generator:\`${suggestions}\`"?` : ""}`;
|
|
567
|
+
return {
|
|
568
|
+
contents: `<!-- ⚠️ ${error} -->`,
|
|
569
|
+
issues: [error]
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
const context = {
|
|
573
|
+
args,
|
|
574
|
+
config,
|
|
575
|
+
block,
|
|
576
|
+
transform: (contents) => transform(contents, config, url),
|
|
577
|
+
url
|
|
578
|
+
};
|
|
579
|
+
try {
|
|
580
|
+
const result = await generator.generate(context);
|
|
581
|
+
if (!result.unwrap && containsAutomd(result.contents)) result.unwrap = true;
|
|
582
|
+
if (result.unwrap) {
|
|
583
|
+
const nestedRes = await context.transform(result.contents);
|
|
584
|
+
result.contents = nestedRes.contents;
|
|
585
|
+
if (nestedRes.hasIssues) result.issues = [...result.issues || [], ...nestedRes.updates.flatMap((u) => u.result.issues || [])].filter(Boolean);
|
|
586
|
+
}
|
|
587
|
+
return result;
|
|
588
|
+
} catch (error) {
|
|
589
|
+
return {
|
|
590
|
+
contents: `<!-- ⚠️ (${block.generator}) ${error.message || error} -->`,
|
|
591
|
+
issues: [error]
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
//#endregion
|
|
596
|
+
export { transform as t };
|
|
597
|
+
|
|
598
|
+
/*! Built with love & coffee ☕ */
|
|
599
|
+
//# sourceMappingURL=transform-BOfYBrHa.js.map
|