flecks/packages/dox/src/generate.js

155 lines
5.0 KiB
JavaScript
Raw Normal View History

2022-03-07 02:44:48 -06:00
const makeFilenameRewriter = (filenameRewriters) => (filename, line, column) => (
Object.entries(filenameRewriters)
.reduce(
(filename, [from, to]) => filename.replace(new RegExp(from), to),
`${filename}:${line}:${column}`,
)
);
2022-03-09 08:51:10 -06:00
export const generateBuildConfigsPage = (buildConfigs) => {
const source = [];
source.push('# Build configuration files');
source.push('');
source.push('This page documents all the build configuration files in this project.');
source.push('');
if (buildConfigs.length > 0) {
buildConfigs
.sort(({config: l}, {config: r}) => (l < r ? -1 : 1))
.forEach(({config, comment}) => {
source.push(`## \`${config}\``);
source.push('');
source.push(comment);
source.push('');
});
}
return source.join('\n');
};
2022-03-09 14:43:54 -06:00
export const generateConfigPage = (configs) => {
const source = [];
source.push('# Configuration');
source.push('');
source.push('This page documents all the configuration in this project.');
source.push('');
Object.entries(configs)
.sort(([l], [r]) => (l < r ? -1 : 1))
.forEach(([fleck, configs]) => {
2023-11-30 21:41:42 -06:00
// source.push(`## \`${fleck}\``);
source.push('```javascript');
source.push(`'${fleck}': {`);
2022-03-09 14:43:54 -06:00
configs.forEach(({comment, config, defaultValue}) => {
comment.split('\n').forEach((line) => {
2023-11-30 21:41:42 -06:00
source.push(` // ${line}`);
2022-03-09 14:43:54 -06:00
});
2023-11-30 21:41:42 -06:00
const value = defaultValue
.split('\n')
.map((line, i, array) => {
let output = '';
if (array.length - 1 === i) {
output += ' ';
}
else if (0 !== i) {
output += ' ';
}
output += line.trim();
return output;
})
.join('\n');
source.push(` ${config}: ${value}`);
2022-03-09 14:43:54 -06:00
});
2023-11-30 21:41:42 -06:00
source.push('}');
source.push('```');
2022-03-09 14:43:54 -06:00
});
return source.join('\n');
};
2022-03-07 02:44:48 -06:00
export const generateHookPage = (hooks, flecks) => {
2022-03-07 06:22:26 -06:00
const {filenameRewriters} = flecks.get('@flecks/dox/server');
2022-03-07 02:44:48 -06:00
const rewriteFilename = makeFilenameRewriter(filenameRewriters);
2022-03-07 00:21:16 -06:00
const source = [];
source.push('# Hooks');
source.push('');
source.push('This page documents all the hooks in this project.');
source.push('');
Object.entries(hooks)
.sort(([lhook], [rhook]) => (lhook < rhook ? -1 : 1))
.forEach(([hook, {implementations = [], invocations = [], specification}]) => {
source.push(`## \`${hook}\``);
source.push('');
const {description, example, params} = specification || {
params: [],
};
if (description) {
description.split('\n').forEach((line) => {
source.push(`> ${line}`);
});
source.push('');
}
if (params.length > 0) {
source.push('<details>');
source.push('<summary>Parameters</summary>');
source.push('<ul>');
params.forEach(({description, name, type}) => {
source.push(`<li><strong><code>{${type}}</code></strong> <code>${name}</code>`);
source.push(`<blockquote>${description}</blockquote></li>`);
});
source.push('</ul>');
source.push('</details>');
source.push('');
}
if (implementations.length > 0) {
source.push('<details>');
source.push('<summary>Implementations</summary>');
source.push('<ul>');
implementations.forEach(({filename, loc: {start: {column, line}}}) => {
2022-03-07 02:44:48 -06:00
source.push(`<li>${rewriteFilename(filename, line, column)}</li>`);
2022-03-07 00:21:16 -06:00
});
source.push('</ul>');
source.push('</details>');
source.push('');
}
if (invocations.length > 0) {
source.push('<details>');
source.push('<summary>Invocations</summary>');
source.push('<ul>');
invocations.forEach(({filename, loc: {start: {column, line}}}) => {
2022-03-07 02:44:48 -06:00
source.push(`<li>${rewriteFilename(filename, line, column)}</li>`);
2022-03-07 00:21:16 -06:00
});
source.push('</ul>');
source.push('</details>');
source.push('');
}
if (example) {
source.push('### Example usage');
source.push('');
source.push('```javascript');
2022-08-11 06:37:54 -05:00
source.push('export const hooks = {');
source.push(` '${hook}': ${example}`);
2022-03-07 00:21:16 -06:00
source.push('};');
source.push('```');
source.push('');
}
});
return source.join('\n');
};
2022-03-07 02:44:48 -06:00
export const generateTodoPage = (todos, flecks) => {
2022-03-07 06:22:26 -06:00
const {filenameRewriters} = flecks.get('@flecks/dox/server');
2022-03-07 02:44:48 -06:00
const rewriteFilename = makeFilenameRewriter(filenameRewriters);
2022-03-07 00:21:16 -06:00
const source = [];
source.push('# TODO');
source.push('');
source.push('This page documents all the TODO items in this project.');
source.push('');
if (todos.length > 0) {
todos.forEach(({filename, loc: {start: {column, line}}, text}) => {
2022-03-07 02:44:48 -06:00
source.push(`- ${rewriteFilename(filename, line, column)}`);
2022-03-07 00:21:16 -06:00
text.split('\n').forEach((line) => {
source.push(` > ${line}`);
});
});
source.push('');
}
return source.join('\n');
};