Skip to content

Commit 422fd07

Browse files
committed
refactor: extract renderMemberList helper
1 parent 9f06f7f commit 422fd07

1 file changed

Lines changed: 51 additions & 36 deletions

File tree

server/utils/docs/render.ts

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,33 @@ async function renderJsDocTags(tags: JsDocTag[], symbolLookup: SymbolLookup): Pr
254254
// Member Rendering
255255
// =============================================================================
256256

257+
type DefinitionListItem = {
258+
signature: string
259+
description?: string
260+
}
261+
262+
function renderMemberList(title: string, items: DefinitionListItem[]): string {
263+
const lines: string[] = []
264+
265+
if (items.length === 0) {
266+
return ''
267+
}
268+
269+
lines.push(`<div class="docs-members">`)
270+
lines.push(`<h4>${title}</h4>`)
271+
lines.push(`<dl>`)
272+
for (const item of items) {
273+
lines.push(`<dt><code>${escapeHtml(item.signature)}</code></dt>`)
274+
if (item.description) {
275+
lines.push(`<dd>${escapeHtml(item.description.split('\n')[0] ?? '')}</dd>`)
276+
}
277+
}
278+
lines.push(`</dl>`)
279+
lines.push(`</div>`)
280+
281+
return lines.join('\n')
282+
}
283+
257284
/**
258285
* Render class members (constructor, properties, methods).
259286
*/
@@ -272,65 +299,53 @@ function renderClassMembers(def: NonNullable<DenoDocNode['classDef']>): string {
272299
}
273300

274301
if (properties && properties.length > 0) {
275-
lines.push(`<div class="docs-members">`)
276-
lines.push(`<h4>Properties</h4>`)
277-
lines.push(`<dl>`)
278-
for (const prop of properties) {
302+
const propertyItems: DefinitionListItem[] = properties.map(prop => {
279303
const modifiers: string[] = []
280304
if (prop.isStatic) modifiers.push('static')
281305
if (prop.readonly) modifiers.push('readonly')
282306
const modStr = modifiers.length > 0 ? `${modifiers.join(' ')} ` : ''
283307
const type = formatType(prop.tsType)
284308
const opt = prop.optional ? '?' : ''
285-
lines.push(
286-
`<dt><code>${escapeHtml(modStr)}${escapeHtml(prop.name)}${opt}: ${escapeHtml(type)}</code></dt>`,
287-
)
288-
if (prop.jsDoc?.doc) {
289-
lines.push(`<dd>${escapeHtml(prop.jsDoc.doc.split('\n')[0] ?? '')}</dd>`)
309+
310+
return {
311+
signature: `${modStr}${prop.name}${opt}: ${type}`,
312+
description: prop.jsDoc?.doc,
290313
}
291-
}
292-
lines.push(`</dl>`)
293-
lines.push(`</div>`)
314+
})
315+
316+
lines.push(renderMemberList('Properties', propertyItems))
294317
}
295318

296319
const getters = methods?.filter(m => m.kind === 'getter') || []
297320
const regularMethods = methods?.filter(m => m.kind !== 'getter') || []
298321

299322
if (getters.length > 0) {
300-
lines.push(`<div class="docs-members">`)
301-
lines.push(`<h4>Getters</h4>`)
302-
lines.push(`<dl>`)
303-
for (const getter of getters) {
323+
const getterItems: DefinitionListItem[] = getters.map(getter => {
304324
const ret = formatType(getter.functionDef?.returnType) || 'unknown'
305325
const staticStr = getter.isStatic ? 'static ' : ''
306-
lines.push(
307-
`<dt><code>${escapeHtml(staticStr)}get ${escapeHtml(getter.name)}: ${escapeHtml(ret)}</code></dt>`,
308-
)
309-
if (getter.jsDoc?.doc) {
310-
lines.push(`<dd>${escapeHtml(getter.jsDoc.doc.split('\n')[0] ?? '')}</dd>`)
326+
327+
return {
328+
signature: `${staticStr}get ${getter.name}: ${ret}`,
329+
description: getter.jsDoc?.doc,
311330
}
312-
}
313-
lines.push(`</dl>`)
314-
lines.push(`</div>`)
331+
})
332+
333+
lines.push(renderMemberList('Getters', getterItems))
315334
}
316335

317336
if (regularMethods.length > 0) {
318-
lines.push(`<div class="docs-members">`)
319-
lines.push(`<h4>Methods</h4>`)
320-
lines.push(`<dl>`)
321-
for (const method of regularMethods) {
337+
const methodItems: DefinitionListItem[] = regularMethods.map(method => {
322338
const params = method.functionDef?.params?.map(p => formatParam(p)).join(', ') || ''
323339
const ret = formatType(method.functionDef?.returnType) || 'void'
324340
const staticStr = method.isStatic ? 'static ' : ''
325-
lines.push(
326-
`<dt><code>${escapeHtml(staticStr)}${escapeHtml(method.name)}(${escapeHtml(params)}): ${escapeHtml(ret)}</code></dt>`,
327-
)
328-
if (method.jsDoc?.doc) {
329-
lines.push(`<dd>${escapeHtml(method.jsDoc.doc.split('\n')[0] ?? '')}</dd>`)
341+
342+
return {
343+
signature: `${staticStr}${method.name}(${params}): ${ret}`,
344+
description: method.jsDoc?.doc,
330345
}
331-
}
332-
lines.push(`</dl>`)
333-
lines.push(`</div>`)
346+
})
347+
348+
lines.push(renderMemberList('Methods', methodItems))
334349
}
335350

336351
return lines.join('\n')

0 commit comments

Comments
 (0)