forked from withastro/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocgen.mjs
123 lines (106 loc) · 3.62 KB
/
docgen.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import fs from 'fs';
import jsdoc from 'jsdoc-api';
import fetch from 'node-fetch';
// Fill this in to test a response locally, with fetching.
const STUB = ``; // fs.readFileSync('/PATH/TO/MONOREPO/astro/packages/astro/src/@types/astro.ts', {encoding: 'utf-8'});
const HEADER = `---
# NOTE: This file is auto-generated from 'scripts/docgen.mjs'
# Do not make edits to it directly, they will be overwritten.
# Instead, change this file: https://github.com/withastro/astro/blob/main/packages/astro/src/%40types/astro.ts
# Translators, please remove this note and the <DontEditWarning/> component.
layout: ~/layouts/MainLayout.astro
title: Configuration Reference
i18nReady: true
githubURL: https://github.com/withastro/astro/blob/main/packages/astro/src/%40types/astro.ts
setup: |
import Since from '../../../components/Since.astro';
import DontEditWarning from '../../../components/DontEditWarning.astro';
---
<DontEditWarning />
The following reference covers all supported configuration options in Astro. To learn more about configuring Astro, read our guide on [Configuring Astro](/en/guides/configuring-astro/).
\`\`\`js
// astro.config.mjs
import { defineConfig } from 'astro/config'
export default defineConfig({
// your configuration options here...
})
\`\`\`
`;
const FOOTER = ``;
/**
* The simple demo does not rely on the TypeScript compiler API; instead, it parses the
* source file directly. It uses the default parser configuration.
*/
export async function run() {
const inputBuffer =
STUB ||
(await fetch(
'https://raw.githubusercontent.com/withastro/astro/main/packages/astro/src/%40types/astro.ts'
).then((r) => r.text()));
// Get all `@docs` JSDoc comments in the file.
const allComments = [
...inputBuffer.matchAll(/\/\*\*\s*\n([^\*]|\*[^\/])*@docs([^\*]|\*[^\/])*\*\//g),
];
const allCommentsInput = allComments
.map((m) => m[0])
.filter((c) => c.includes('* @docs'))
.join('\n\n');
console.log(jsdoc);
console.log(allCommentsInput);
console.log(jsdoc.explainSync({ source: allCommentsInput }));
const allParsedComments = jsdoc
.explainSync({ source: allCommentsInput })
.filter((data) => data.tags);
let result = ``;
for (const comment of allParsedComments) {
if (comment.kind === 'heading') {
result += `## ${comment.name}\n\n`;
if (comment.description) {
result += comment.description.trim() + '\n\n';
}
continue;
}
const cliFlag = comment.tags.find((f) => f.title === 'cli');
const typerawFlag = comment.tags.find((f) => f.title === 'typeraw');
if (!comment.name) {
throw new Error(`Missing @docs JSDoc tag: @name`);
}
if (!comment.type && !typerawFlag) {
throw new Error(`Missing @docs JSDoc tag: @type or @typeraw`);
}
const typesFormatted = typerawFlag
? typerawFlag.text.replace(/\{(.*)\}/, '$1')
: comment.type.names.join(' | ');
result += [
`### ${comment.longname}`,
``,
`<p>`,
``,
[
`**Type:** \`${typesFormatted}\``,
cliFlag ? `**CLI:** \`${cliFlag.text}\`` : undefined,
comment.defaultvalue ? `**Default:** ${comment.defaultvalue}` : undefined,
comment.version ? `<Since v="${comment.version}" />` : undefined,
]
.filter((l) => l !== undefined)
.join('<br>\n'),
`</p>`,
``,
comment.description && comment.description.trim(),
comment.see
? `**See Also:**\n${comment.see.map((s) => `- ${s}`.trim()).join('\n')}`
: undefined,
`\n\n`,
]
.filter((l) => l !== undefined)
.join('\n');
}
result = result.replace(/https:\/\/docs\.astro\.build\//g, '/');
console.log(result);
fs.writeFileSync(
'src/pages/en/reference/configuration-reference.md',
HEADER + result + FOOTER,
'utf8'
);
}
run();