Skip to content

Commit 97e9b47

Browse files
committed
feat(jsdocs): sections and sorting
1 parent a9525a6 commit 97e9b47

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ The declared section will be automatically updated!
4747
### Supported Args
4848

4949
- `src`: Path to the source file. The default is `./src/index` and can be omitted.
50-
- `headingLevel`: Nested level for markdown headings (default is `3` => `###`)
50+
- `headingLevel`: Nested level for markdown group headings (default is `3` => `###`) - Note: Each export uses `headingLevel+1`.
5151

5252
## Development
5353

playground/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<!-- AUTOMD_START generator="jsdocs" src="./src/index" -->
44

5-
### `add(a, b)`
5+
#### `add(a, b)`
66

77
Adds two numbers together.
88

src/generators/jsdocs.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ export default defineGenerator({
1919
});
2020

2121
function renderSchema(schema: Schema, opts: { headingLevel: number }) {
22-
const md: string[] = [];
22+
const sections = Object.create(null) as Record<string, [string, string[]][]>;
23+
2324
for (const [name, meta] of Object.entries(schema.properties || {})) {
2425
// Only functions
25-
if (meta.type !== "function") {
26+
if (
27+
meta.type !== "function" ||
28+
meta.default?.toString?.().startsWith("class")
29+
) {
2630
continue;
2731
}
2832

33+
const lines: string[] = [];
34+
2935
// Parse tag annotations
3036
const tags = parseTags(meta.tags);
3137

@@ -36,28 +42,44 @@ function renderSchema(schema: Schema, opts: { headingLevel: number }) {
3642

3743
const jsSig = `${name}(${(meta.args || []).map((arg) => arg.name).join(", ")})`;
3844

39-
md.push(`${"#".repeat(opts.headingLevel)} \`${jsSig}\``, "");
45+
lines.push(`${"#".repeat(opts.headingLevel + 1)} \`${jsSig}\``, "");
4046

4147
if (meta.title) {
42-
md.push(meta.title.trim());
48+
lines.push(meta.title.trim());
4349
}
4450
if (meta.description) {
45-
md.push("", meta.description.trim());
51+
lines.push("", meta.description.trim());
4652
}
4753

4854
for (const tag of tags) {
4955
if (tag.tag === "@example") {
5056
const codeBlock = tag.contents.startsWith("`")
5157
? tag.contents
5258
: `\`\`\`ts\n${tag.contents}\n\`\`\``;
53-
md.push("", "**Example:**", "", codeBlock);
59+
lines.push("", "**Example:**", "", codeBlock);
5460
}
5561
}
5662

57-
md.push("");
63+
lines.push("");
64+
65+
const group = tags.find((t) => t.tag === "@group")?.contents || "";
66+
sections[group] = sections[group] || [];
67+
sections[group].push([name, lines]);
68+
}
69+
70+
const lines: string[] = [];
71+
for (const group of Object.keys(sections).sort()) {
72+
if (group) {
73+
lines.push(`${"#".repeat(opts.headingLevel)} ${group}`, "");
74+
}
75+
for (const item of sections[group].sort((i1, i2) =>
76+
i1[0].localeCompare(i2[0]),
77+
)) {
78+
lines.push(...item[1]);
79+
}
5880
}
5981

60-
return md.join("\n");
82+
return lines.join("\n");
6183
}
6284

6385
function parseTags(lines: string[] = []) {

0 commit comments

Comments
 (0)