1- import type { JSDocTagInfo , SymbolDisplayPart } from 'typescript' ;
1+ import type { JSDocTagInfo } from 'typescript' ;
22import type {
33 DocEntry ,
44 DocEntryConstructor ,
55 MarkdownEmoji ,
66 MarkdownHeadingLevel ,
77 MarkdownOptions
8- } from './types' ;
9-
10- interface Params {
11- name : string ;
12- documentation : string ;
13- }
14-
15- type Row = Required < Pick < DocEntry , 'name' | 'type' | 'documentation' > > &
16- Pick < DocEntry , 'url' > & {
17- params : Params [ ] ;
18- examples : string [ ] ;
19- returnType ?: string ;
20- references ?: string [ ] ;
21- } ;
8+ } from '../types' ;
9+ import {
10+ jsDocsToExamples ,
11+ jsDocsToParams ,
12+ jsDocsToReferences ,
13+ jsDocsToReturnType
14+ } from './jdocs/mapper' ;
15+ import { inlineReferences } from './jdocs/parser' ;
16+ import type { Params , Row } from './types' ;
2217
2318const toParams = ( parameters ?: DocEntry [ ] ) : Params [ ] =>
2419 ( parameters ?? [ ] ) . map ( ( { name, documentation} : DocEntry ) => ( {
@@ -32,37 +27,6 @@ const inlineDocParam = (documentation: string | undefined): string =>
3227const inlineParams = ( params : Params [ ] ) : string [ ] =>
3328 params . map ( ( { name, documentation} ) => `* \`${ name } \`${ inlineDocParam ( documentation ) } ` ) ;
3429
35- // Example of inputs:
36- // [
37- // 'hello2',
38- // 'https://daviddalbusco.com',
39- // '{@link hello } – Another related function',
40- // '{@link https://github.com/peterpeterparker/tsdoc-markdown Source code}'
41- // ]
42- const inlineReferences = ( references : string [ ] ) : string [ ] => {
43- const inlineReference = ( reference : string ) : string => {
44- const linkMatch = / \{ @ l i n k \s + ( [ ^ \s } ] + ) \s * (?: \s + ( [ ^ } ] + ) ) ? \} / . exec ( reference ) ;
45-
46- if ( linkMatch !== null ) {
47- const [ _ , target , label ] = linkMatch ;
48-
49- if ( target . startsWith ( 'http' ) ) {
50- return `* [${ label ?? target } ](${ target } )` ;
51- }
52-
53- return `* \`${ target . trim ( ) } \`${ label ? ` – ${ label } ` : '' } ` ;
54- }
55-
56- if ( reference . startsWith ( 'http' ) ) {
57- return `* [${ reference } ](${ reference } )` ;
58- }
59-
60- return `* ${ reference } ` ;
61- } ;
62-
63- return references . map ( inlineReference ) ;
64- } ;
65-
6630const reduceStatic = ( values : DocEntry [ ] ) : [ DocEntry [ ] , DocEntry [ ] ] =>
6731 values . reduce < [ DocEntry [ ] , DocEntry [ ] ] > (
6832 ( [ i , s ] , value ) => [
@@ -241,69 +205,6 @@ const toMarkdown = ({
241205 headingLevel : MarkdownHeadingLevel | '####' ;
242206 docType : 'Constant' | 'Function' | 'Method' | 'Property' | 'Type' | 'Enum' ;
243207} & Pick < MarkdownOptions , 'emoji' > ) : string => {
244- const jsDocsToSymbolDisplayParts = ( {
245- jsDocs = [ ] ,
246- tagInfoName
247- } : {
248- jsDocs ?: JSDocTagInfo [ ] ;
249- tagInfoName : 'returns' | 'param' | 'see' ;
250- } ) : SymbolDisplayPart [ ] [ ] => {
251- const tags = jsDocs . filter ( ( { name} : JSDocTagInfo ) => name === tagInfoName ) ;
252- const texts = tags . map ( ( { text} ) => text ) ;
253-
254- return texts . reduce < SymbolDisplayPart [ ] [ ] > ( ( acc , values ) => {
255- if ( values === undefined ) {
256- return acc ;
257- }
258-
259- return [ ...acc , values ] ;
260- } , [ ] ) ;
261- } ;
262-
263- const jsDocsToReturnType = ( jsDocs ?: JSDocTagInfo [ ] ) : string => {
264- const returns = jsDocsToSymbolDisplayParts ( { jsDocs, tagInfoName : 'returns' } ) ;
265- return returns . map ( ( parts ) => parts . map ( ( { text} ) => text ) . join ( '' ) ) . join ( ' ' ) ;
266- } ;
267-
268- const jsDocsToReferences = ( jsDocs ?: JSDocTagInfo [ ] ) : string [ ] => {
269- const sees = jsDocsToSymbolDisplayParts ( { jsDocs, tagInfoName : 'see' } ) ;
270-
271- return sees
272- . map ( ( texts ) =>
273- texts
274- // Filter TypeScript unstripped comment asterix
275- . filter ( ( { text} ) => text !== '*' )
276- . reduce ( ( acc , { text} ) => `${ acc } ${ text } ` , '' )
277- )
278- . map ( ( value ) => value . trim ( ) ) ;
279- } ;
280-
281- const jsDocsToParams = ( jsDocs ?: JSDocTagInfo [ ] ) : Params [ ] => {
282- const params = jsDocsToSymbolDisplayParts ( { jsDocs, tagInfoName : 'param' } ) ;
283-
284- const toParam = ( parts : SymbolDisplayPart [ ] ) : Params | undefined => {
285- if ( parts . find ( ( { kind, text} ) => kind === 'parameterName' && text !== '' ) === undefined ) {
286- return undefined ;
287- }
288-
289- const name = parts . find ( ( { kind} ) => kind === 'parameterName' ) ?. text ?? '' ;
290- const documentation = parts . find ( ( { kind} ) => kind === 'text' ) ?. text ?? '' ;
291-
292- return { name, documentation} ;
293- } ;
294-
295- return params . map ( toParam ) . filter ( ( param ) => param !== undefined ) as Params [ ] ;
296- } ;
297-
298- const jsDocsToExamples = ( jsDocs : JSDocTagInfo [ ] ) : string [ ] => {
299- const examples : JSDocTagInfo [ ] = jsDocs . filter ( ( { name} : JSDocTagInfo ) => name === 'example' ) ;
300- const texts = examples
301- . map ( ( { text} ) => text )
302- . filter ( Boolean )
303- . flat ( 1 ) as SymbolDisplayPart [ ] ;
304- return texts . map ( ( { text} ) => text ) . filter ( Boolean ) ;
305- } ;
306-
307208 const rows : Row [ ] = entries . map (
308209 ( { name, type, documentation, parameters, jsDocs, url} : DocEntry ) => ( {
309210 name,
0 commit comments