This repository has been archived by the owner on Apr 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathgenerate-plugin-options-docs.js
175 lines (147 loc) · 4.84 KB
/
generate-plugin-options-docs.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const prettier = require(`prettier`)
const Joi = require(`@hapi/joi`)
const Handlebars = require(`handlebars`)
const fs = require(`fs-extra`)
const _ = require(`lodash`)
const toc = require(`markdown-toc`)
const prettierConfig = require(`./prettier.config.js`)
const {
pluginOptionsSchema,
} = require(`./plugin/dist/steps/declare-plugin-options-schema`)
// :( poor children
const excludeParentsChildren = [`RootQuery`]
/**
* Takes the keys from a Joi schema and recursively
* turns the nested keys into structured markdown documentation
*
* @param {object} keys
* @param {string} mdString
* @param {number} level
* @param {string} parent
*/
function joiKeysToMD({
keys,
mdString = ``,
level = 1,
parent = null,
parentMetas = [],
}) {
if (
!keys ||
(parentMetas.length && parentMetas.find((meta) => meta.portableOptions))
) {
return mdString
}
Object.entries(keys).forEach(([key, value]) => {
const isRequired = value.flags && value.flags.presence === `required`
const title = `${parent ? `${parent}.` : ``}${key}${
isRequired ? ` (**required**)` : ``
}`
mdString += `${`#`.repeat(level + 1)} ${title}`
if (value.description) {
mdString += `\n\n`
const description = value.description.trim()
mdString += description.endsWith(`.`) ? description : `${description}.`
}
if (value.type) {
const { trueType } =
(value.meta && value.meta.find((meta) => `trueType` in meta)) || {}
mdString += `\n\n`
mdString += `**Field type**: \`${_.startCase(trueType || value.type)}\``
}
if (
(value.flags && `default` in value.flags) ||
(value.meta && value.meta.find((meta) => `default` in meta))
) {
const defaultValue =
value.meta.find((meta) => `default` in meta)?.default ||
value.flags.default
let printedValue
if (typeof defaultValue === `string`) {
printedValue = defaultValue
} else if (Array.isArray(defaultValue)) {
printedValue = `[${defaultValue.join(`, `)}]`
} else if (
[`boolean`, `function`, `number`].includes(typeof defaultValue)
) {
printedValue = defaultValue.toString()
} else if (defaultValue === null) {
printedValue = `null`
}
if (typeof printedValue === `string`) {
mdString += `\n\n`
mdString += `**Default value**: ${
printedValue.includes(`\n`)
? `\n\`\`\`js\n${printedValue}\n\`\`\``
: `\`${printedValue}\``
}`
}
}
if (value.meta) {
const examples = value.meta.filter((meta) => `example` in meta)
examples.forEach(({ example }) => {
mdString += `\n\n\`\`\`js\n` + example + `\n\`\`\`\n`
})
}
mdString += `\n\n`
const excludeChildren = excludeParentsChildren.includes(key)
if (!excludeChildren && value.children) {
mdString = joiKeysToMD({
keys: value.children,
mdString,
level: level + 1,
parent: title,
parentMetas: value.meta,
})
}
if (!excludeChildren && value.items && value.items.length) {
value.items.forEach((item) => {
if (item.children) {
mdString = joiKeysToMD({
keys: item.children,
mdString,
level: level + 1,
parent: title + `[]`,
parentMetas: value.meta,
})
}
})
}
})
return mdString
}
/**
* Converts the Joi schema description into markdown
* and writes it to the filesystem
*
* @param {object} description
*/
async function generateMdFileFromSchemaDescription(description) {
const template = Handlebars.compile(`# Plugin Options
[comment]: # (This file is automatically generated. Do not edit it directly. Instead, edit the Joi schema in ./plugin/src/steps/declare-plugin-options-schema.js)
{{{tableOfContents}}}
{{{docs}}}
# Up Next :point_right:
- :boat: [Migrating from other WP source plugins](./migrating-from-other-wp-source-plugins.md)
- :house: [Hosting WordPress](./hosting.md)
- :athletic_shoe: [Themes, Starters, and Examples](./themes-starters-examples.md)
- :medal_sports: [Usage with popular WPGraphQL extensions](./usage-with-popular-wp-graphql-extensions.md)
- :hammer_and_wrench: [Debugging and troubleshooting](./debugging-and-troubleshooting.md)
- :national_park: [Community and Support](./community-and-support.md)
- :point_left: [Back to README.md](../README.md)`)
const docs = joiKeysToMD({
keys: description.children,
})
const tableOfContents = toc(docs).content
const mdContents = template({
tableOfContents,
docs,
})
const mdContentsFormatted = prettier.format(mdContents, {
parser: `markdown`,
...prettierConfig,
})
await fs.writeFile(`./docs/plugin-options.md`, mdContentsFormatted)
}
const description = pluginOptionsSchema({ Joi }).describe()
generateMdFileFromSchemaDescription(description)