import nunjucks from 'nunjucks'; import jsdoc from '../doc.json' assert { type: 'json' }; // TODO: load tutorial.mdx and append rendered api.mdx to the bottom (to make sure TOC works) // TODO: split const env = nunjucks.configure('.', { autoescape: false }); const docs = jsdoc.docs.reduce((acc, obj) => Object.assign(acc, { [obj.longname]: obj }), {}); function renderAsMDX(name) { const item = docs[name]; if (!item) { console.warn('Not found: ' + name); return ''; } return `### ${item.longname} ${item.description.replaceAll(/\{\@link ([a-zA-Z]+)?\#?([a-zA-Z]*)\}/g, (_, a, b) => { // console.log(_, 'a', a, 'b', b); return `${a}${b ? `#${b}` : ''}`; })} ${!!item.params?.length ? '**Parameters**' : ''} ${ item.params ?.map( (param, i) => `- ${param.name} (${param.type?.names?.join('|')}): ${param.description?.replace(/(<([^>]+)>)/gi, '')}`, ) .join('\n') || '' } ${ item.examples?.length ? `**Examples**
${item.examples?.map((example, k) => ``).join('\n\n')}
` : '' }`; } env.addFilter('jsdoc', renderAsMDX); const rendered = nunjucks.render('tutorial.mdx', { docs }); console.log(rendered);