UNPKG

doxdox-parser-dox

Version:
66 lines (65 loc) 2.42 kB
import fs from 'node:fs/promises'; import { slugify } from 'doxdox-core'; import dox from 'dox'; const formatParameter = (param) => param ? param.toString().replace(/\[|\]/gu, '') : null; export default async (cwd, path) => await parseString(path, await fs.readFile(path, 'utf8')); export const parseString = async (path, content) => { const docs = dox.parseComments(content, { raw: true, skipSingleStar: true }); const methods = docs .filter(method => !method.ignore && method.ctx) .map(method => { const params = (method.tags || []) .filter(tag => tag.type === 'param') .map(({ name = null, description = null, types = {} }) => ({ name: formatParameter(name), description, types: types || [] })); const returns = (method.tags || []) .filter(tag => tag.type === 'return' || tag.type === 'returns') .map(({ name = null, description = null, types = {} }) => ({ name: formatParameter(name), description, types: types || [] })); const isFunction = params.length > 0 || method.ctx.type === 'constructor' || method.ctx.type === 'method' || method.ctx.type === 'function' || (method.ctx.type === 'declaration' && (method.ctx.value?.match(/^(async )?\(/) || method.ctx.string?.match(/\)$/))); const name = method.ctx.cons ? `${method.ctx.cons}.${method.ctx.name}` : method.ctx.name; const fullName = isFunction ? `${name}(${params .map(param => param.name) .filter(name => name && !name.match(/\./)) .join(', ')})` : name; return { type: method.ctx.type, slug: `${slugify(path)}-${slugify(method.ctx.string)}`, name, fullName, description: method.description.full || null, params, returns, private: method.isPrivate }; }) .sort((a, b) => { if (a.name.toLocaleLowerCase() < b.name.toLocaleLowerCase()) { return -1; } if (a.name.toLocaleLowerCase() > b.name.toLocaleLowerCase()) { return 1; } return 0; }); return { path, methods }; };