|
| 1 | +// @ts-check |
| 2 | +import { ReflectionKind } from 'typedoc'; |
| 3 | +import { MarkdownTheme, MarkdownThemeContext } from 'typedoc-plugin-markdown'; |
| 4 | + |
| 5 | +/** |
| 6 | + * @param {import('typedoc-plugin-markdown').MarkdownApplication} app |
| 7 | + */ |
| 8 | +export function load(app) { |
| 9 | + app.renderer.defineTheme('clerkTheme', ClerkMarkdownTheme); |
| 10 | +} |
| 11 | + |
| 12 | +class ClerkMarkdownTheme extends MarkdownTheme { |
| 13 | + /** |
| 14 | + * @param {import('typedoc-plugin-markdown').MarkdownPageEvent} page |
| 15 | + */ |
| 16 | + getRenderContext(page) { |
| 17 | + return new ClerkMarkdownThemeContext(this, page, this.application.options); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Our custom Clerk theme |
| 23 | + * @extends MarkdownThemeContext |
| 24 | + */ |
| 25 | +class ClerkMarkdownThemeContext extends MarkdownThemeContext { |
| 26 | + /** |
| 27 | + * @param {MarkdownTheme} theme |
| 28 | + * @param {import('typedoc-plugin-markdown').MarkdownPageEvent} page |
| 29 | + * @param {MarkdownTheme["application"]["options"]} options |
| 30 | + */ |
| 31 | + constructor(theme, page, options) { |
| 32 | + super(theme, page, options); |
| 33 | + |
| 34 | + const superPartials = this.partials; |
| 35 | + |
| 36 | + this.partials = { |
| 37 | + ...superPartials, |
| 38 | + /** |
| 39 | + * Copied from default theme / source code. This hides the return type from the output |
| 40 | + * https://github.com/typedoc2md/typedoc-plugin-markdown/blob/179a54c502b318cd4f3951e5e8b90f7f7a4752d8/packages/typedoc-plugin-markdown/src/theme/context/partials/member.signatureReturns.ts |
| 41 | + * @param {import('typedoc').SignatureReflection} model |
| 42 | + * @param {{ headingLevel: number }} options |
| 43 | + */ |
| 44 | + signatureReturns: (model, options) => { |
| 45 | + const md = []; |
| 46 | + |
| 47 | + /** |
| 48 | + * @type any |
| 49 | + */ |
| 50 | + const modelType = model.type; |
| 51 | + /** |
| 52 | + * @type {import('typedoc').DeclarationReflection} |
| 53 | + */ |
| 54 | + const typeDeclaration = modelType?.declaration; |
| 55 | + |
| 56 | + md.push(heading(options.headingLevel, this.i18n.theme_returns())); |
| 57 | + |
| 58 | + if (model.comment?.blockTags.length) { |
| 59 | + const tags = model.comment.blockTags |
| 60 | + .filter(tag => tag.tag === '@returns') |
| 61 | + .map(tag => this.helpers.getCommentParts(tag.content)); |
| 62 | + md.push(tags.join('\n\n')); |
| 63 | + } |
| 64 | + |
| 65 | + if (typeDeclaration?.signatures) { |
| 66 | + typeDeclaration.signatures.forEach(signature => { |
| 67 | + md.push( |
| 68 | + this.partials.signature(signature, { |
| 69 | + headingLevel: options.headingLevel + 1, |
| 70 | + nested: true, |
| 71 | + }), |
| 72 | + ); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + if (typeDeclaration?.children) { |
| 77 | + md.push( |
| 78 | + this.partials.typeDeclaration(typeDeclaration, { |
| 79 | + headingLevel: options.headingLevel, |
| 80 | + }), |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + return md.join('\n\n'); |
| 85 | + }, |
| 86 | + /** |
| 87 | + * Copied from default theme / source code. This hides the "Type parameters" section and the signature title from the output |
| 88 | + * https://github.com/typedoc2md/typedoc-plugin-markdown/blob/179a54c502b318cd4f3951e5e8b90f7f7a4752d8/packages/typedoc-plugin-markdown/src/theme/context/partials/member.signature.ts |
| 89 | + * @param {import('typedoc').SignatureReflection} model |
| 90 | + * @param {{ headingLevel: number, nested?: boolean, accessor?: string, multipleSignatures?: boolean }} options |
| 91 | + */ |
| 92 | + signature: (model, options) => { |
| 93 | + const md = []; |
| 94 | + |
| 95 | + if (!options.nested && model.sources && !this.options.getValue('disableSources')) { |
| 96 | + md.push(this.partials.sources(model)); |
| 97 | + } |
| 98 | + |
| 99 | + let modelComments = options.multipleSignatures ? model.comment : model.comment || model.parent?.comment; |
| 100 | + |
| 101 | + if (modelComments && model.parent?.comment?.summary && !options.multipleSignatures) { |
| 102 | + modelComments = Object.assign(modelComments, { |
| 103 | + summary: model.parent.comment.summary, |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + if (modelComments && model.parent?.comment?.blockTags) { |
| 108 | + modelComments.blockTags = [...(model.parent?.comment?.blockTags || []), ...(model.comment?.blockTags || [])]; |
| 109 | + } |
| 110 | + |
| 111 | + if (modelComments) { |
| 112 | + md.push( |
| 113 | + this.partials.comment(modelComments, { |
| 114 | + headingLevel: options.headingLevel, |
| 115 | + showTags: false, |
| 116 | + showSummary: true, |
| 117 | + }), |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + if (!options.multipleSignatures && model.parent?.documents) { |
| 122 | + md.push( |
| 123 | + this.partials.documents(model?.parent, { |
| 124 | + headingLevel: options.headingLevel, |
| 125 | + }), |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + if (model.parameters?.length) { |
| 130 | + md.push(heading(options.headingLevel, this.internationalization.kindPluralString(ReflectionKind.Parameter))); |
| 131 | + if (this.helpers.useTableFormat('parameters')) { |
| 132 | + md.push(this.partials.parametersTable(model.parameters)); |
| 133 | + } else { |
| 134 | + md.push( |
| 135 | + this.partials.parametersList(model.parameters, { |
| 136 | + headingLevel: options.headingLevel, |
| 137 | + }), |
| 138 | + ); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (model.type) { |
| 143 | + md.push( |
| 144 | + this.partials.signatureReturns(model, { |
| 145 | + headingLevel: options.headingLevel, |
| 146 | + }), |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + if (modelComments) { |
| 151 | + md.push( |
| 152 | + this.partials.comment(modelComments, { |
| 153 | + headingLevel: options.headingLevel, |
| 154 | + showTags: true, |
| 155 | + showSummary: false, |
| 156 | + }), |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + md.push(this.partials.inheritance(model, { headingLevel: options.headingLevel })); |
| 161 | + |
| 162 | + return md.join('\n\n'); |
| 163 | + }, |
| 164 | + /** |
| 165 | + * Copied from default theme / source code. This hides the "Type parameters" section from the output |
| 166 | + * https://github.com/typedoc2md/typedoc-plugin-markdown/blob/179a54c502b318cd4f3951e5e8b90f7f7a4752d8/packages/typedoc-plugin-markdown/src/theme/context/partials/member.memberWithGroups.ts#L58 |
| 167 | + * @param {import('typedoc').DeclarationReflection} model |
| 168 | + * @param {{ headingLevel: number }} options |
| 169 | + */ |
| 170 | + memberWithGroups: (model, options) => { |
| 171 | + const md = []; |
| 172 | + |
| 173 | + if ( |
| 174 | + ![ReflectionKind.Module, ReflectionKind.Namespace].includes(model.kind) && |
| 175 | + model.sources && |
| 176 | + !this.options.getValue('disableSources') |
| 177 | + ) { |
| 178 | + md.push(this.partials.sources(model)); |
| 179 | + } |
| 180 | + |
| 181 | + if (model.comment) { |
| 182 | + md.push( |
| 183 | + this.partials.comment(model.comment, { |
| 184 | + headingLevel: options.headingLevel, |
| 185 | + }), |
| 186 | + ); |
| 187 | + } |
| 188 | + |
| 189 | + if (model.typeHierarchy?.next) { |
| 190 | + md.push( |
| 191 | + this.partials.hierarchy(model.typeHierarchy, { |
| 192 | + headingLevel: options.headingLevel, |
| 193 | + }), |
| 194 | + ); |
| 195 | + } |
| 196 | + |
| 197 | + if (model.implementedTypes?.length) { |
| 198 | + md.push(heading(options.headingLevel, this.i18n.theme_implements())); |
| 199 | + md.push( |
| 200 | + unorderedList(model.implementedTypes.map(implementedType => this.partials.someType(implementedType))), |
| 201 | + ); |
| 202 | + } |
| 203 | + |
| 204 | + if (model.kind === ReflectionKind.Class && model.categories?.length) { |
| 205 | + model.groups |
| 206 | + ?.filter(group => group.title === this.i18n.kind_plural_constructor()) |
| 207 | + .forEach(group => { |
| 208 | + md.push(heading(options.headingLevel, this.i18n.kind_plural_constructor())); |
| 209 | + group.children.forEach(child => { |
| 210 | + md.push( |
| 211 | + this.partials.constructor(/** @type {import('typedoc').DeclarationReflection} */ (child), { |
| 212 | + headingLevel: options.headingLevel + 1, |
| 213 | + }), |
| 214 | + ); |
| 215 | + }); |
| 216 | + }); |
| 217 | + } |
| 218 | + |
| 219 | + if ('signatures' in model && model.signatures?.length) { |
| 220 | + model.signatures.forEach(signature => { |
| 221 | + md.push( |
| 222 | + this.partials.signature(signature, { |
| 223 | + headingLevel: options.headingLevel, |
| 224 | + }), |
| 225 | + ); |
| 226 | + }); |
| 227 | + } |
| 228 | + |
| 229 | + if (model.indexSignatures?.length) { |
| 230 | + md.push(heading(options.headingLevel, this.i18n.theme_indexable())); |
| 231 | + model.indexSignatures.forEach(indexSignature => { |
| 232 | + md.push(this.partials.indexSignature(indexSignature)); |
| 233 | + }); |
| 234 | + } |
| 235 | + |
| 236 | + md.push(this.partials.body(model, { headingLevel: options.headingLevel })); |
| 237 | + |
| 238 | + return md.join('\n\n'); |
| 239 | + }, |
| 240 | + }; |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | +/** |
| 245 | + * Returns a heading in markdown format |
| 246 | + * @param {number} level The level of the heading |
| 247 | + * @param {string} text The text of the heading |
| 248 | + */ |
| 249 | +function heading(level, text) { |
| 250 | + level = level > 6 ? 6 : level; |
| 251 | + return `${[...Array(level)].map(() => '#').join('')} ${text}`; |
| 252 | +} |
| 253 | + |
| 254 | +/** |
| 255 | + * Create an unordered list from an array of items |
| 256 | + * @param {string[]} items |
| 257 | + * @returns |
| 258 | + */ |
| 259 | +function unorderedList(items) { |
| 260 | + return items.map(item => `- ${item}`).join('\n'); |
| 261 | +} |
0 commit comments