compmark-vue 0.1.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/LICENSE +21 -0
- package/README.md +113 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +197 -0
- package/dist/index.d.mts +28 -0
- package/dist/index.mjs +168 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# vuemark
|
|
2
|
+
|
|
3
|
+
<!-- automd:badges color=yellow -->
|
|
4
|
+
|
|
5
|
+
[](https://npmjs.com/package/compmark-vue)
|
|
6
|
+
[](https://npm.chart.dev/compmark-vue)
|
|
7
|
+
|
|
8
|
+
<!-- /automd -->
|
|
9
|
+
|
|
10
|
+
Auto-generate Markdown documentation from Vue 3 SFCs. Zero configuration required.
|
|
11
|
+
|
|
12
|
+
## Quick Start
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npx compmark-vue ./src/components/Button.vue
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
This parses the component and creates `Button.md` in your current directory.
|
|
19
|
+
|
|
20
|
+
### Example Output
|
|
21
|
+
|
|
22
|
+
Given a component like:
|
|
23
|
+
|
|
24
|
+
```vue
|
|
25
|
+
<script setup>
|
|
26
|
+
/**
|
|
27
|
+
* @emit submit Emitted on form submit
|
|
28
|
+
* @emit cancel Emitted on cancel
|
|
29
|
+
*/
|
|
30
|
+
const emit = defineEmits(["submit", "cancel"]);
|
|
31
|
+
|
|
32
|
+
const props = defineProps({
|
|
33
|
+
/** Title of the dialog */
|
|
34
|
+
title: {
|
|
35
|
+
type: String,
|
|
36
|
+
required: true,
|
|
37
|
+
},
|
|
38
|
+
/** Whether the dialog is visible */
|
|
39
|
+
visible: {
|
|
40
|
+
type: Boolean,
|
|
41
|
+
default: false,
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
</script>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
vuemark generates:
|
|
48
|
+
|
|
49
|
+
```md
|
|
50
|
+
# Dialog
|
|
51
|
+
|
|
52
|
+
## Props
|
|
53
|
+
|
|
54
|
+
| Name | Type | Required | Default | Description |
|
|
55
|
+
| ------- | ------- | -------- | ------- | ----------------------------- |
|
|
56
|
+
| title | String | Yes | - | Title of the dialog |
|
|
57
|
+
| visible | Boolean | No | `false` | Whether the dialog is visible |
|
|
58
|
+
|
|
59
|
+
## Emits
|
|
60
|
+
|
|
61
|
+
| Name | Description |
|
|
62
|
+
| ------ | ---------------------- |
|
|
63
|
+
| submit | Emitted on form submit |
|
|
64
|
+
| cancel | Emitted on cancel |
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Programmatic API
|
|
68
|
+
|
|
69
|
+
```sh
|
|
70
|
+
pnpm install compmark-vue
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { parseComponent, generateMarkdown } from "compmark-vue";
|
|
75
|
+
|
|
76
|
+
const doc = parseComponent("./src/components/Button.vue");
|
|
77
|
+
const md = generateMarkdown(doc);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Or parse from a string:
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { parseSFC, generateMarkdown } from "compmark-vue";
|
|
84
|
+
|
|
85
|
+
const doc = parseSFC(source, "Button.vue");
|
|
86
|
+
const md = generateMarkdown(doc);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Supported Syntax
|
|
90
|
+
|
|
91
|
+
- `defineProps({ ... })` — shorthand (`String`), array type (`[String, Number]`), and full object syntax
|
|
92
|
+
- `defineEmits([...])` — array syntax
|
|
93
|
+
- JSDoc comments on props and emits (`/** ... */`)
|
|
94
|
+
- `const props = defineProps(...)` variable assignment pattern
|
|
95
|
+
- Default value extraction (string, number, boolean literals, arrow functions)
|
|
96
|
+
|
|
97
|
+
## Development
|
|
98
|
+
|
|
99
|
+
<details>
|
|
100
|
+
|
|
101
|
+
<summary>local development</summary>
|
|
102
|
+
|
|
103
|
+
- Clone this repository
|
|
104
|
+
- Install latest LTS version of [Node.js](https://nodejs.org/en/)
|
|
105
|
+
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
|
|
106
|
+
- Install dependencies using `pnpm install`
|
|
107
|
+
- Run interactive tests using `pnpm dev`
|
|
108
|
+
|
|
109
|
+
</details>
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
Published under the [MIT](https://github.com/noopurphalak/vuemark/blob/main/LICENSE) license.
|
package/dist/cli.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { compileScript, parse } from "@vue/compiler-sfc";
|
|
5
|
+
//#region src/parser.ts
|
|
6
|
+
function parseSFC(source, filename) {
|
|
7
|
+
const doc = {
|
|
8
|
+
name: filename.replace(/\.vue$/, "").split("/").pop() ?? "Unknown",
|
|
9
|
+
props: [],
|
|
10
|
+
emits: []
|
|
11
|
+
};
|
|
12
|
+
const { descriptor } = parse(source, { filename });
|
|
13
|
+
if (!descriptor.scriptSetup && !descriptor.script) return doc;
|
|
14
|
+
const compiled = compileScript(descriptor, { id: filename });
|
|
15
|
+
const ast = compiled.scriptSetupAst;
|
|
16
|
+
if (!ast) return doc;
|
|
17
|
+
const scriptSource = descriptor.scriptSetup?.content ?? compiled.content;
|
|
18
|
+
for (const stmt of ast) {
|
|
19
|
+
const calls = extractDefineCalls(stmt);
|
|
20
|
+
for (const { callee, args, leadingComments } of calls) if (callee === "defineProps" && args[0]?.type === "ObjectExpression") doc.props = extractProps(args[0], scriptSource);
|
|
21
|
+
else if (callee === "defineEmits" && args[0]?.type === "ArrayExpression") doc.emits = extractEmits(args[0], leadingComments);
|
|
22
|
+
}
|
|
23
|
+
return doc;
|
|
24
|
+
}
|
|
25
|
+
function extractDefineCalls(stmt) {
|
|
26
|
+
const calls = [];
|
|
27
|
+
if (stmt.type === "ExpressionStatement" && stmt.expression.type === "CallExpression" && stmt.expression.callee.type === "Identifier") calls.push({
|
|
28
|
+
callee: stmt.expression.callee.name,
|
|
29
|
+
args: stmt.expression.arguments,
|
|
30
|
+
leadingComments: stmt.leadingComments ?? []
|
|
31
|
+
});
|
|
32
|
+
if (stmt.type === "VariableDeclaration") {
|
|
33
|
+
for (const decl of stmt.declarations) if (decl.init?.type === "CallExpression" && decl.init.callee.type === "Identifier") calls.push({
|
|
34
|
+
callee: decl.init.callee.name,
|
|
35
|
+
args: decl.init.arguments,
|
|
36
|
+
leadingComments: stmt.leadingComments ?? []
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return calls;
|
|
40
|
+
}
|
|
41
|
+
function extractProps(obj, source) {
|
|
42
|
+
const props = [];
|
|
43
|
+
for (const prop of obj.properties) {
|
|
44
|
+
if (prop.type !== "ObjectProperty") continue;
|
|
45
|
+
const p = prop;
|
|
46
|
+
const name = p.key.type === "Identifier" ? p.key.name : p.key.type === "StringLiteral" ? p.key.value : "";
|
|
47
|
+
if (!name) continue;
|
|
48
|
+
const description = extractJSDoc(p.leadingComments ?? []);
|
|
49
|
+
if (p.value.type === "Identifier") props.push({
|
|
50
|
+
name,
|
|
51
|
+
type: p.value.name,
|
|
52
|
+
required: false,
|
|
53
|
+
default: void 0,
|
|
54
|
+
description
|
|
55
|
+
});
|
|
56
|
+
else if (p.value.type === "ArrayExpression") {
|
|
57
|
+
const types = p.value.elements.filter((el) => el?.type === "Identifier").map((el) => el.name);
|
|
58
|
+
props.push({
|
|
59
|
+
name,
|
|
60
|
+
type: types.join(" | "),
|
|
61
|
+
required: false,
|
|
62
|
+
default: void 0,
|
|
63
|
+
description
|
|
64
|
+
});
|
|
65
|
+
} else if (p.value.type === "ObjectExpression") {
|
|
66
|
+
let type = "unknown";
|
|
67
|
+
let required = false;
|
|
68
|
+
let defaultVal;
|
|
69
|
+
for (const field of p.value.properties) {
|
|
70
|
+
if (field.type !== "ObjectProperty") continue;
|
|
71
|
+
const fieldName = field.key.type === "Identifier" ? field.key.name : "";
|
|
72
|
+
if (fieldName === "type") {
|
|
73
|
+
if (field.value.type === "Identifier") type = field.value.name;
|
|
74
|
+
else if (field.value.type === "ArrayExpression") type = field.value.elements.filter((el) => el?.type === "Identifier").map((el) => el.name).join(" | ");
|
|
75
|
+
} else if (fieldName === "required") required = field.value.type === "BooleanLiteral" && field.value.value;
|
|
76
|
+
else if (fieldName === "default") defaultVal = stringifyDefault(field.value, source);
|
|
77
|
+
}
|
|
78
|
+
props.push({
|
|
79
|
+
name,
|
|
80
|
+
type,
|
|
81
|
+
required,
|
|
82
|
+
default: defaultVal,
|
|
83
|
+
description
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return props;
|
|
88
|
+
}
|
|
89
|
+
function extractEmits(arr, leadingComments) {
|
|
90
|
+
const emits = [];
|
|
91
|
+
const jsdocMap = parseEmitJSDoc(leadingComments);
|
|
92
|
+
for (const el of arr.elements) if (el?.type === "StringLiteral") emits.push({
|
|
93
|
+
name: el.value,
|
|
94
|
+
description: jsdocMap.get(el.value) ?? ""
|
|
95
|
+
});
|
|
96
|
+
return emits;
|
|
97
|
+
}
|
|
98
|
+
function parseEmitJSDoc(comments) {
|
|
99
|
+
const map = /* @__PURE__ */ new Map();
|
|
100
|
+
for (const c of comments) {
|
|
101
|
+
if (c.type !== "CommentBlock") continue;
|
|
102
|
+
const lines = c.value.split("\n").map((l) => l.replace(/^\s*\*\s?/, "").replace(/^\s*\/?\*+\s?/, "").trim());
|
|
103
|
+
for (const line of lines) {
|
|
104
|
+
const match = line.match(/^@emit\s+(\S+)\s+(.*)/);
|
|
105
|
+
if (match?.[1] && match[2]) map.set(match[1], match[2].trim());
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return map;
|
|
109
|
+
}
|
|
110
|
+
function extractJSDoc(comments) {
|
|
111
|
+
for (let i = comments.length - 1; i >= 0; i--) {
|
|
112
|
+
const c = comments[i];
|
|
113
|
+
if (c.type !== "CommentBlock") continue;
|
|
114
|
+
const lines = c.value.split("\n").map((l) => l.replace(/^\s*\*\s?/, "").trim()).filter((l) => l && !l.startsWith("@") && !l.startsWith("/"));
|
|
115
|
+
if (lines.length > 0) return lines.join(" ");
|
|
116
|
+
}
|
|
117
|
+
return "";
|
|
118
|
+
}
|
|
119
|
+
function stringifyDefault(node, source) {
|
|
120
|
+
switch (node.type) {
|
|
121
|
+
case "StringLiteral": return JSON.stringify(node.value);
|
|
122
|
+
case "NumericLiteral": return String(node.value);
|
|
123
|
+
case "BooleanLiteral": return String(node.value);
|
|
124
|
+
case "NullLiteral": return "null";
|
|
125
|
+
case "ArrowFunctionExpression":
|
|
126
|
+
case "FunctionExpression":
|
|
127
|
+
if (node.start != null && node.end != null) return source.slice(node.start, node.end);
|
|
128
|
+
return "...";
|
|
129
|
+
default: return "...";
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/markdown.ts
|
|
134
|
+
function generateMarkdown(doc) {
|
|
135
|
+
const sections = [`# ${doc.name}`];
|
|
136
|
+
if (doc.props.length === 0 && doc.emits.length === 0) {
|
|
137
|
+
sections.push("", "No documentable props or emits found.");
|
|
138
|
+
return sections.join("\n") + "\n";
|
|
139
|
+
}
|
|
140
|
+
if (doc.props.length > 0) {
|
|
141
|
+
sections.push("", "## Props", "");
|
|
142
|
+
sections.push("| Name | Type | Required | Default | Description |");
|
|
143
|
+
sections.push("| --- | --- | --- | --- | --- |");
|
|
144
|
+
for (const p of doc.props) {
|
|
145
|
+
const def = p.default !== void 0 ? `\`${p.default}\`` : "-";
|
|
146
|
+
const desc = p.description || "-";
|
|
147
|
+
const req = p.required ? "Yes" : "No";
|
|
148
|
+
sections.push(`| ${p.name} | ${p.type} | ${req} | ${def} | ${desc} |`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (doc.emits.length > 0) {
|
|
152
|
+
sections.push("", "## Emits", "");
|
|
153
|
+
sections.push("| Name | Description |");
|
|
154
|
+
sections.push("| --- | --- |");
|
|
155
|
+
for (const e of doc.emits) {
|
|
156
|
+
const desc = e.description || "-";
|
|
157
|
+
sections.push(`| ${e.name} | ${desc} |`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return sections.join("\n") + "\n";
|
|
161
|
+
}
|
|
162
|
+
//#endregion
|
|
163
|
+
//#region src/index.ts
|
|
164
|
+
function parseComponent(filePath) {
|
|
165
|
+
const abs = resolve(filePath);
|
|
166
|
+
return parseSFC(readFileSync(abs, "utf-8"), abs.split("/").pop() ?? "Unknown.vue");
|
|
167
|
+
}
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region src/cli.ts
|
|
170
|
+
const filePath = process.argv[2];
|
|
171
|
+
if (!filePath) {
|
|
172
|
+
console.error("Usage: compmark-vue <path-to-component.vue>");
|
|
173
|
+
process.exit(1);
|
|
174
|
+
}
|
|
175
|
+
if (!filePath.endsWith(".vue")) {
|
|
176
|
+
console.error(`Error: Expected a .vue file, got: ${filePath}`);
|
|
177
|
+
process.exit(1);
|
|
178
|
+
}
|
|
179
|
+
const abs = resolve(filePath);
|
|
180
|
+
if (!existsSync(abs)) {
|
|
181
|
+
console.error(`Error: File not found: ${filePath}`);
|
|
182
|
+
process.exit(1);
|
|
183
|
+
}
|
|
184
|
+
try {
|
|
185
|
+
const doc = parseComponent(abs);
|
|
186
|
+
const md = generateMarkdown(doc);
|
|
187
|
+
const outFile = `${doc.name}.md`;
|
|
188
|
+
writeFileSync(join(process.cwd(), outFile), md, "utf-8");
|
|
189
|
+
console.log(`Created ${outFile}`);
|
|
190
|
+
} catch (err) {
|
|
191
|
+
const name = abs.split("/").pop() ?? filePath;
|
|
192
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
193
|
+
console.error(`Error: Could not parse ${name}: ${reason}`);
|
|
194
|
+
process.exit(1);
|
|
195
|
+
}
|
|
196
|
+
//#endregion
|
|
197
|
+
export {};
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
interface PropDoc {
|
|
3
|
+
name: string;
|
|
4
|
+
type: string;
|
|
5
|
+
required: boolean;
|
|
6
|
+
default: string | undefined;
|
|
7
|
+
description: string;
|
|
8
|
+
}
|
|
9
|
+
interface EmitDoc {
|
|
10
|
+
name: string;
|
|
11
|
+
description: string;
|
|
12
|
+
}
|
|
13
|
+
interface ComponentDoc {
|
|
14
|
+
name: string;
|
|
15
|
+
props: PropDoc[];
|
|
16
|
+
emits: EmitDoc[];
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/parser.d.ts
|
|
20
|
+
declare function parseSFC(source: string, filename: string): ComponentDoc;
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/markdown.d.ts
|
|
23
|
+
declare function generateMarkdown(doc: ComponentDoc): string;
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/index.d.ts
|
|
26
|
+
declare function parseComponent(filePath: string): ComponentDoc;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { type ComponentDoc, type EmitDoc, type PropDoc, generateMarkdown, parseComponent, parseSFC };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { compileScript, parse } from "@vue/compiler-sfc";
|
|
4
|
+
//#region src/parser.ts
|
|
5
|
+
function parseSFC(source, filename) {
|
|
6
|
+
const doc = {
|
|
7
|
+
name: filename.replace(/\.vue$/, "").split("/").pop() ?? "Unknown",
|
|
8
|
+
props: [],
|
|
9
|
+
emits: []
|
|
10
|
+
};
|
|
11
|
+
const { descriptor } = parse(source, { filename });
|
|
12
|
+
if (!descriptor.scriptSetup && !descriptor.script) return doc;
|
|
13
|
+
const compiled = compileScript(descriptor, { id: filename });
|
|
14
|
+
const ast = compiled.scriptSetupAst;
|
|
15
|
+
if (!ast) return doc;
|
|
16
|
+
const scriptSource = descriptor.scriptSetup?.content ?? compiled.content;
|
|
17
|
+
for (const stmt of ast) {
|
|
18
|
+
const calls = extractDefineCalls(stmt);
|
|
19
|
+
for (const { callee, args, leadingComments } of calls) if (callee === "defineProps" && args[0]?.type === "ObjectExpression") doc.props = extractProps(args[0], scriptSource);
|
|
20
|
+
else if (callee === "defineEmits" && args[0]?.type === "ArrayExpression") doc.emits = extractEmits(args[0], leadingComments);
|
|
21
|
+
}
|
|
22
|
+
return doc;
|
|
23
|
+
}
|
|
24
|
+
function extractDefineCalls(stmt) {
|
|
25
|
+
const calls = [];
|
|
26
|
+
if (stmt.type === "ExpressionStatement" && stmt.expression.type === "CallExpression" && stmt.expression.callee.type === "Identifier") calls.push({
|
|
27
|
+
callee: stmt.expression.callee.name,
|
|
28
|
+
args: stmt.expression.arguments,
|
|
29
|
+
leadingComments: stmt.leadingComments ?? []
|
|
30
|
+
});
|
|
31
|
+
if (stmt.type === "VariableDeclaration") {
|
|
32
|
+
for (const decl of stmt.declarations) if (decl.init?.type === "CallExpression" && decl.init.callee.type === "Identifier") calls.push({
|
|
33
|
+
callee: decl.init.callee.name,
|
|
34
|
+
args: decl.init.arguments,
|
|
35
|
+
leadingComments: stmt.leadingComments ?? []
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return calls;
|
|
39
|
+
}
|
|
40
|
+
function extractProps(obj, source) {
|
|
41
|
+
const props = [];
|
|
42
|
+
for (const prop of obj.properties) {
|
|
43
|
+
if (prop.type !== "ObjectProperty") continue;
|
|
44
|
+
const p = prop;
|
|
45
|
+
const name = p.key.type === "Identifier" ? p.key.name : p.key.type === "StringLiteral" ? p.key.value : "";
|
|
46
|
+
if (!name) continue;
|
|
47
|
+
const description = extractJSDoc(p.leadingComments ?? []);
|
|
48
|
+
if (p.value.type === "Identifier") props.push({
|
|
49
|
+
name,
|
|
50
|
+
type: p.value.name,
|
|
51
|
+
required: false,
|
|
52
|
+
default: void 0,
|
|
53
|
+
description
|
|
54
|
+
});
|
|
55
|
+
else if (p.value.type === "ArrayExpression") {
|
|
56
|
+
const types = p.value.elements.filter((el) => el?.type === "Identifier").map((el) => el.name);
|
|
57
|
+
props.push({
|
|
58
|
+
name,
|
|
59
|
+
type: types.join(" | "),
|
|
60
|
+
required: false,
|
|
61
|
+
default: void 0,
|
|
62
|
+
description
|
|
63
|
+
});
|
|
64
|
+
} else if (p.value.type === "ObjectExpression") {
|
|
65
|
+
let type = "unknown";
|
|
66
|
+
let required = false;
|
|
67
|
+
let defaultVal;
|
|
68
|
+
for (const field of p.value.properties) {
|
|
69
|
+
if (field.type !== "ObjectProperty") continue;
|
|
70
|
+
const fieldName = field.key.type === "Identifier" ? field.key.name : "";
|
|
71
|
+
if (fieldName === "type") {
|
|
72
|
+
if (field.value.type === "Identifier") type = field.value.name;
|
|
73
|
+
else if (field.value.type === "ArrayExpression") type = field.value.elements.filter((el) => el?.type === "Identifier").map((el) => el.name).join(" | ");
|
|
74
|
+
} else if (fieldName === "required") required = field.value.type === "BooleanLiteral" && field.value.value;
|
|
75
|
+
else if (fieldName === "default") defaultVal = stringifyDefault(field.value, source);
|
|
76
|
+
}
|
|
77
|
+
props.push({
|
|
78
|
+
name,
|
|
79
|
+
type,
|
|
80
|
+
required,
|
|
81
|
+
default: defaultVal,
|
|
82
|
+
description
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return props;
|
|
87
|
+
}
|
|
88
|
+
function extractEmits(arr, leadingComments) {
|
|
89
|
+
const emits = [];
|
|
90
|
+
const jsdocMap = parseEmitJSDoc(leadingComments);
|
|
91
|
+
for (const el of arr.elements) if (el?.type === "StringLiteral") emits.push({
|
|
92
|
+
name: el.value,
|
|
93
|
+
description: jsdocMap.get(el.value) ?? ""
|
|
94
|
+
});
|
|
95
|
+
return emits;
|
|
96
|
+
}
|
|
97
|
+
function parseEmitJSDoc(comments) {
|
|
98
|
+
const map = /* @__PURE__ */ new Map();
|
|
99
|
+
for (const c of comments) {
|
|
100
|
+
if (c.type !== "CommentBlock") continue;
|
|
101
|
+
const lines = c.value.split("\n").map((l) => l.replace(/^\s*\*\s?/, "").replace(/^\s*\/?\*+\s?/, "").trim());
|
|
102
|
+
for (const line of lines) {
|
|
103
|
+
const match = line.match(/^@emit\s+(\S+)\s+(.*)/);
|
|
104
|
+
if (match?.[1] && match[2]) map.set(match[1], match[2].trim());
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return map;
|
|
108
|
+
}
|
|
109
|
+
function extractJSDoc(comments) {
|
|
110
|
+
for (let i = comments.length - 1; i >= 0; i--) {
|
|
111
|
+
const c = comments[i];
|
|
112
|
+
if (c.type !== "CommentBlock") continue;
|
|
113
|
+
const lines = c.value.split("\n").map((l) => l.replace(/^\s*\*\s?/, "").trim()).filter((l) => l && !l.startsWith("@") && !l.startsWith("/"));
|
|
114
|
+
if (lines.length > 0) return lines.join(" ");
|
|
115
|
+
}
|
|
116
|
+
return "";
|
|
117
|
+
}
|
|
118
|
+
function stringifyDefault(node, source) {
|
|
119
|
+
switch (node.type) {
|
|
120
|
+
case "StringLiteral": return JSON.stringify(node.value);
|
|
121
|
+
case "NumericLiteral": return String(node.value);
|
|
122
|
+
case "BooleanLiteral": return String(node.value);
|
|
123
|
+
case "NullLiteral": return "null";
|
|
124
|
+
case "ArrowFunctionExpression":
|
|
125
|
+
case "FunctionExpression":
|
|
126
|
+
if (node.start != null && node.end != null) return source.slice(node.start, node.end);
|
|
127
|
+
return "...";
|
|
128
|
+
default: return "...";
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/markdown.ts
|
|
133
|
+
function generateMarkdown(doc) {
|
|
134
|
+
const sections = [`# ${doc.name}`];
|
|
135
|
+
if (doc.props.length === 0 && doc.emits.length === 0) {
|
|
136
|
+
sections.push("", "No documentable props or emits found.");
|
|
137
|
+
return sections.join("\n") + "\n";
|
|
138
|
+
}
|
|
139
|
+
if (doc.props.length > 0) {
|
|
140
|
+
sections.push("", "## Props", "");
|
|
141
|
+
sections.push("| Name | Type | Required | Default | Description |");
|
|
142
|
+
sections.push("| --- | --- | --- | --- | --- |");
|
|
143
|
+
for (const p of doc.props) {
|
|
144
|
+
const def = p.default !== void 0 ? `\`${p.default}\`` : "-";
|
|
145
|
+
const desc = p.description || "-";
|
|
146
|
+
const req = p.required ? "Yes" : "No";
|
|
147
|
+
sections.push(`| ${p.name} | ${p.type} | ${req} | ${def} | ${desc} |`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (doc.emits.length > 0) {
|
|
151
|
+
sections.push("", "## Emits", "");
|
|
152
|
+
sections.push("| Name | Description |");
|
|
153
|
+
sections.push("| --- | --- |");
|
|
154
|
+
for (const e of doc.emits) {
|
|
155
|
+
const desc = e.description || "-";
|
|
156
|
+
sections.push(`| ${e.name} | ${desc} |`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return sections.join("\n") + "\n";
|
|
160
|
+
}
|
|
161
|
+
//#endregion
|
|
162
|
+
//#region src/index.ts
|
|
163
|
+
function parseComponent(filePath) {
|
|
164
|
+
const abs = resolve(filePath);
|
|
165
|
+
return parseSFC(readFileSync(abs, "utf-8"), abs.split("/").pop() ?? "Unknown.vue");
|
|
166
|
+
}
|
|
167
|
+
//#endregion
|
|
168
|
+
export { generateMarkdown, parseComponent, parseSFC };
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "compmark-vue",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Auto-generate Markdown documentation from Vue 3 SFCs",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "noopurphalak/vuemark",
|
|
7
|
+
"bin": {
|
|
8
|
+
"compmark-vue": "./dist/cli.mjs"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"type": "module",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"types": "./dist/index.d.mts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": "./dist/index.mjs"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "obuild",
|
|
21
|
+
"dev": "vitest dev",
|
|
22
|
+
"fmt": "automd && oxlint . --fix && oxfmt .",
|
|
23
|
+
"lint": "oxlint . && oxfmt --check .",
|
|
24
|
+
"prepack": "pnpm build",
|
|
25
|
+
"release": "pnpm test && pnpm build && changelogen --release && npm publish && git push --follow-tags",
|
|
26
|
+
"test": "pnpm lint && pnpm typecheck && vitest run --coverage",
|
|
27
|
+
"typecheck": "tsgo --noEmit --skipLibCheck"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@vue/compiler-sfc": "^3.5.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@babel/types": "latest",
|
|
34
|
+
"@types/node": "latest",
|
|
35
|
+
"@typescript/native-preview": "latest",
|
|
36
|
+
"@vitest/coverage-v8": "latest",
|
|
37
|
+
"automd": "latest",
|
|
38
|
+
"changelogen": "latest",
|
|
39
|
+
"obuild": "latest",
|
|
40
|
+
"oxfmt": "latest",
|
|
41
|
+
"oxlint": "latest",
|
|
42
|
+
"typescript": "latest",
|
|
43
|
+
"vitest": "latest"
|
|
44
|
+
},
|
|
45
|
+
"packageManager": "pnpm@10.29.3"
|
|
46
|
+
}
|