-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #363 from capraynor/master
Add objectLiteralTypeDeclarationStyle option.
- Loading branch information
Showing
10 changed files
with
471 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 0 additions & 135 deletions
135
packages/typedoc-plugin-markdown/src/resources/helpers/property-table.ts
This file was deleted.
Oops, something went wrong.
198 changes: 198 additions & 0 deletions
198
packages/typedoc-plugin-markdown/src/resources/helpers/type-declaration-object-literal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
import * as Handlebars from 'handlebars'; | ||
import { DeclarationReflection, ReflectionType } from 'typedoc'; | ||
import { escapeChars, stripLineBreaks } from '../../utils'; | ||
import { MarkdownTheme } from "../../theme"; | ||
|
||
|
||
export default function (theme: MarkdownTheme) { | ||
Handlebars.registerHelper( | ||
'typeDeclarationMembers', | ||
function (this: DeclarationReflection[]) { | ||
const comments = this.map( | ||
(param) => !!param.comment?.hasVisibleComponent(), | ||
); | ||
const hasComments = !comments.every((value) => !value); | ||
|
||
const flattenParams = (current: any) => { | ||
return current.type?.declaration?.children?.reduce( | ||
(acc: any, child: any) => { | ||
const childObj = { | ||
...child, | ||
name: `${current.name}.${child.name}`, | ||
}; | ||
return parseParams(childObj, acc); | ||
}, | ||
[], | ||
); | ||
}; | ||
|
||
const parseParams = (current: any, acc: any) => { | ||
const shouldFlatten = current.type?.declaration?.children; | ||
|
||
return shouldFlatten | ||
? [...acc, current, ...flattenParams(current)] | ||
: [...acc, current]; | ||
}; | ||
|
||
const properties = this.reduce( | ||
(acc: any, current: any) => parseParams(current, acc), | ||
[], | ||
); | ||
|
||
let result = ""; | ||
switch (theme.objectLiteralTypeDeclarationStyle){ | ||
case "list": { | ||
result = getListMarkdownContent(properties); | ||
break; | ||
} | ||
case "table": { | ||
result = getTableMarkdownContent(properties, hasComments); | ||
break; | ||
} | ||
} | ||
return result; | ||
}, | ||
); | ||
} | ||
|
||
function getListMarkdownContent(properties: DeclarationReflection[]) { | ||
const propertyTable = getTableMarkdownContentWithoutComment(properties); | ||
const propertyDescriptions = properties.map((property) => { | ||
const name = property.name.match(/[\\`\\|]/g) !== null | ||
? escapeChars(getName(property)) | ||
: `${getName(property)}`; | ||
|
||
const propertyType = getPropertyType(property); | ||
const propertyTypeStr = Handlebars.helpers.type | ||
.call(propertyType); | ||
|
||
const comments = getComments(property); | ||
const commentsStr = comments ? Handlebars.helpers.comments(comments) : "\\-"; | ||
|
||
const md = ( | ||
`**${name}**: ${propertyTypeStr} | ||
${commentsStr} | ||
----- | ||
`) | ||
|
||
return md; | ||
}); | ||
|
||
const propertyComments = propertyDescriptions.join("\n\n"); | ||
|
||
const result = | ||
` | ||
${propertyTable} | ||
${propertyComments} | ||
` | ||
|
||
return result; | ||
} | ||
|
||
function getTableMarkdownContent(properties: DeclarationReflection[], hasComments: boolean,) { | ||
|
||
const headers = ['Name', 'Type']; | ||
|
||
if (hasComments) { | ||
headers.push('Description'); | ||
} | ||
const rows = properties.map((property) => { | ||
const propertyType = getPropertyType(property); | ||
const row: string[] = []; | ||
const nameCol: string[] = []; | ||
const name = | ||
property.name.match(/[\\`\\|]/g) !== null | ||
? escapeChars(getName(property)) | ||
: `\`${getName(property)}\``; | ||
nameCol.push(name); | ||
row.push(nameCol.join(' ')); | ||
row.push( | ||
Handlebars.helpers.type | ||
.call(propertyType) | ||
.replace(/(?<!\\)\|/g, '\\|'), | ||
); | ||
|
||
if (hasComments) { | ||
const comments = getComments(property); | ||
if (comments) { | ||
row.push( | ||
stripLineBreaks(Handlebars.helpers.comments(comments)).replace( | ||
/\|/g, | ||
'\\|', | ||
), | ||
); | ||
} else { | ||
row.push('-'); | ||
} | ||
} | ||
return `| ${row.join(' | ')} |\n`; | ||
}); | ||
|
||
const output = `\n| ${headers.join(' | ')} |\n| ${headers | ||
.map(() => ':------') | ||
.join(' | ')} |\n${rows.join('')}`; | ||
return output; | ||
} | ||
|
||
|
||
function getTableMarkdownContentWithoutComment(properties: DeclarationReflection[]){ | ||
|
||
const result = getTableMarkdownContent(properties, false); | ||
return result; | ||
} | ||
|
||
function getPropertyType(property: any) { | ||
if (property.getSignature) { | ||
return property.getSignature.type; | ||
} | ||
if (property.setSignature) { | ||
return property.setSignature.type; | ||
} | ||
return property.type ? property.type : property; | ||
} | ||
|
||
function getName(property: DeclarationReflection) { | ||
const md: string[] = []; | ||
if (property.flags.isRest) { | ||
md.push('...'); | ||
} | ||
if (property.getSignature) { | ||
md.push(`get ${property.getSignature.name}()`); | ||
} else if (property.setSignature) { | ||
md.push( | ||
`set ${ | ||
property.setSignature.name | ||
}(${property.setSignature.parameters?.map((parameter) => { | ||
return `${parameter.name}:${Handlebars.helpers.type.call( | ||
parameter.type, | ||
'all', | ||
false, | ||
)}`; | ||
})})`, | ||
); | ||
} else { | ||
md.push(property.name); | ||
} | ||
if (property.flags.isOptional) { | ||
md.push('?'); | ||
} | ||
return md.join(''); | ||
} | ||
|
||
function getComments(property: DeclarationReflection) { | ||
if (property.type instanceof ReflectionType) { | ||
if (property.type?.declaration?.signatures) { | ||
return property.type?.declaration.signatures[0].comment; | ||
} | ||
} | ||
if (property.signatures) { | ||
return property.signatures[0].comment; | ||
} | ||
return property.comment; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,7 +60,7 @@ | |
|
||
{{#with type.declaration.children}} | ||
|
||
{{{propertyTable}}} | ||
{{{typeDeclarationMembers}}} | ||
|
||
{{/with}} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,7 +84,7 @@ | |
|
||
{{#with declaration.children}} | ||
|
||
{{{propertyTable}}} | ||
{{{typeDeclarationMembers}}} | ||
|
||
{{/with}} | ||
|
||
|
Oops, something went wrong.