Allow for propless vars in magic vars documentation (#461)
This commit is contained in:
parent
4fb5c37a35
commit
4fb9bd4054
1 changed files with 49 additions and 16 deletions
65
gen-docs.ts
65
gen-docs.ts
|
@ -12,24 +12,57 @@ interface Widget {
|
||||||
isVisible: boolean;
|
isVisible: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface MagicVar {
|
||||||
|
name: string;
|
||||||
|
desc: string;
|
||||||
|
prop?: string;
|
||||||
|
}
|
||||||
|
|
||||||
function parseMagicVariables(data: string) {
|
function parseMagicVariables(data: string) {
|
||||||
const pattern = /^.*\/\/\s*@desc\s*(\w+)\s*-\s*(.*)$/gm;
|
const desc_pattern = /^.*\/\/\s*@desc\s*(\w+)\s*-\s*(.*)$/; // matches `// @desc <name> - <desc>`
|
||||||
const prop_pattern = /^.*\/\/\s+@prop +\s*(.*)$/gm;
|
const prop_pattern = /^.*\/\/\s+@prop +\s*(.*)$/; // matches `// @prop <prop>`
|
||||||
let properties = [...data.matchAll(prop_pattern)]
|
const continuation = /^.*\/\/\s*(.*)$/; // matches `// <...>`
|
||||||
let output = [];
|
let defs: MagicVar[] = [];
|
||||||
let i = 0;
|
let last: "desc" | "prop" | null = null; // what was the last line
|
||||||
for (const [_, name, desc] of data.matchAll(pattern)) {
|
for (const line of data.split("\n")) {
|
||||||
output.push(
|
const desc = desc_pattern.exec(line);
|
||||||
`### \`${name}\`
|
const prop = prop_pattern.exec(line);
|
||||||
${desc.replaceAll("\\n", "\n\n")}
|
const cont = continuation.exec(line);
|
||||||
#### Structure
|
if(desc) {
|
||||||
\`\`\`
|
defs.push({
|
||||||
${properties[i][1]}
|
name: desc[1],
|
||||||
\`\`\`
|
desc: desc[2],
|
||||||
`);
|
prop: undefined,
|
||||||
i = i + 1
|
});
|
||||||
|
last = "desc";
|
||||||
|
} else if(prop && defs.length > 0) {
|
||||||
|
defs[defs.length - 1].prop = prop[1];
|
||||||
|
last = "prop";
|
||||||
|
} else if(cont && defs.length > 0) {
|
||||||
|
if(last == "desc") {
|
||||||
|
defs[defs.length - 1].desc += `\n\n${cont[1]}`;
|
||||||
|
} else if(last == "prop" && defs[defs.length - 1].prop) {
|
||||||
|
defs[defs.length - 1].prop += `\n\n${cont[1]}`;
|
||||||
|
} // else this is just a comment, we ignore
|
||||||
|
} else {
|
||||||
|
last = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return output.join("\n");
|
let output = "";
|
||||||
|
for (const {name, desc, prop} of defs) {
|
||||||
|
output +=
|
||||||
|
`### \`${name}\`\n` +
|
||||||
|
`${desc}\n`;
|
||||||
|
if(prop != null) {
|
||||||
|
output +=
|
||||||
|
'#### Structure\n' +
|
||||||
|
'```\n' +
|
||||||
|
`${prop}\n` +
|
||||||
|
'```\n';
|
||||||
|
}
|
||||||
|
output += '\n';
|
||||||
|
}
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseVars(code: string): Record<string, string> {
|
function parseVars(code: string): Record<string, string> {
|
||||||
|
|
Loading…
Add table
Reference in a new issue