|
| 1 | +import type { ApiClass, ApiFunction, ApiMethod, ApiModule, ApiPackage, ApiVariable } from '@libtmux/api-model' |
| 2 | +import { parseRst, type RoleResolver, renderHtml } from '@libtmux/rst-lite' |
| 3 | + |
| 4 | +export type DocstringRenderMode = 'introspect' | 'rst-lite' | 'none' |
| 5 | + |
| 6 | +export type DocstringRenderOptions = { |
| 7 | + mode?: DocstringRenderMode |
| 8 | + roleResolver?: RoleResolver |
| 9 | +} |
| 10 | + |
| 11 | +const shouldRender = (docstring: string | null, format: string): boolean => { |
| 12 | + if (!docstring) { |
| 13 | + return false |
| 14 | + } |
| 15 | + if (format === 'markdown') { |
| 16 | + return false |
| 17 | + } |
| 18 | + return true |
| 19 | +} |
| 20 | + |
| 21 | +const renderDocstring = (docstring: string | null, format: string, roleResolver?: RoleResolver): string | null => { |
| 22 | + if (!shouldRender(docstring, format)) { |
| 23 | + return null |
| 24 | + } |
| 25 | + const doc = parseRst(docstring ?? '') |
| 26 | + return renderHtml(doc, { roleResolver }) |
| 27 | +} |
| 28 | + |
| 29 | +const applyToFunction = (fn: ApiFunction, roleResolver?: RoleResolver): ApiFunction => { |
| 30 | + return { |
| 31 | + ...fn, |
| 32 | + docstringHtml: renderDocstring(fn.docstring, fn.docstringFormat, roleResolver), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +const applyToMethod = (method: ApiMethod, roleResolver?: RoleResolver): ApiMethod => { |
| 37 | + return { |
| 38 | + ...method, |
| 39 | + docstringHtml: renderDocstring(method.docstring, method.docstringFormat, roleResolver), |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +const applyToVariable = (variable: ApiVariable, roleResolver?: RoleResolver): ApiVariable => { |
| 44 | + return { |
| 45 | + ...variable, |
| 46 | + docstringHtml: renderDocstring(variable.docstring, variable.docstringFormat, roleResolver), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +const applyToClass = (klass: ApiClass, roleResolver?: RoleResolver): ApiClass => { |
| 51 | + return { |
| 52 | + ...klass, |
| 53 | + docstringHtml: renderDocstring(klass.docstring, klass.docstringFormat, roleResolver), |
| 54 | + methods: klass.methods.map((method) => applyToMethod(method, roleResolver)), |
| 55 | + attributes: klass.attributes.map((attribute) => applyToVariable(attribute, roleResolver)), |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +const applyToModule = (module: ApiModule, roleResolver?: RoleResolver): ApiModule => { |
| 60 | + return { |
| 61 | + ...module, |
| 62 | + docstringHtml: renderDocstring(module.docstring, module.docstringFormat, roleResolver), |
| 63 | + classes: module.classes.map((klass) => applyToClass(klass, roleResolver)), |
| 64 | + functions: module.functions.map((fn) => applyToFunction(fn, roleResolver)), |
| 65 | + variables: module.variables.map((variable) => applyToVariable(variable, roleResolver)), |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +export const renderDocstrings = (api: ApiPackage, options: DocstringRenderOptions = {}): ApiPackage => { |
| 70 | + const mode = options.mode ?? 'introspect' |
| 71 | + if (mode !== 'rst-lite') { |
| 72 | + return api |
| 73 | + } |
| 74 | + |
| 75 | + return { |
| 76 | + ...api, |
| 77 | + modules: api.modules.map((module) => applyToModule(module, options.roleResolver)), |
| 78 | + } |
| 79 | +} |
0 commit comments