const makeFilenameRewriter = (filenameRewriters) => (filename, line, column) => ( Object.entries(filenameRewriters) .reduce( (filename, [from, to]) => filename.replace(new RegExp(from), to), `${filename}:${line}:${column}`, ) ); 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'); }; 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]) => { source.push(`## \`${fleck}\``); source.push(''); configs.forEach(({comment, config, defaultValue}) => { comment.split('\n').forEach((line) => { source.push(`> ${line}`); }); source.push(''); source.push('```javascript'); source.push(`${config}: ${defaultValue}`); source.push('```'); source.push(''); }); }); return source.join('\n'); }; export const generateHookPage = (hooks, flecks) => { const {filenameRewriters} = flecks.get('@flecks/dox/server'); const rewriteFilename = makeFilenameRewriter(filenameRewriters); 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('
'); source.push('Parameters'); source.push(''); source.push('
'); source.push(''); } if (implementations.length > 0) { source.push('
'); source.push('Implementations'); source.push(''); source.push('
'); source.push(''); } if (invocations.length > 0) { source.push('
'); source.push('Invocations'); source.push(''); source.push('
'); source.push(''); } if (example) { source.push('### Example usage'); source.push(''); source.push('```javascript'); source.push('export default {'); source.push(' [Hooks]: {'); source.push(` '${hook}': ${example}`); source.push(' },'); source.push('};'); source.push('```'); source.push(''); } }); return source.join('\n'); }; export const generateTodoPage = (todos, flecks) => { const {filenameRewriters} = flecks.get('@flecks/dox/server'); const rewriteFilename = makeFilenameRewriter(filenameRewriters); 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}) => { source.push(`- ${rewriteFilename(filename, line, column)}`); text.split('\n').forEach((line) => { source.push(` > ${line}`); }); }); source.push(''); } return source.join('\n'); };