-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs-infra] Simplify API sections typing (#43128)
Signed-off-by: Alexandre Fauquette <[email protected]>
- Loading branch information
1 parent
75c24b0
commit b9a041d
Showing
19 changed files
with
566 additions
and
320 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
82 changes: 82 additions & 0 deletions
82
docs/src/modules/components/ApiPage/definitions/classes.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,82 @@ | ||
import { PropsTranslations, ComponentApiContent } from '@mui-internal/api-docs-builder'; | ||
import { Translate } from '@mui/docs/i18n'; | ||
import kebabCase from 'lodash/kebabCase'; | ||
import type { TableOfContentsParams } from 'docs/src/modules/components/ApiPage'; | ||
|
||
export interface ClassDefinition { | ||
className: string; | ||
key: string; | ||
hash: string; | ||
description?: string; | ||
isGlobal?: boolean; | ||
isDeprecated?: boolean; | ||
deprecationInfo?: string; | ||
} | ||
|
||
export type GetCssToCParams = { | ||
classes: ClassDefinition[]; | ||
t: Translate; | ||
hash?: string; | ||
}; | ||
|
||
export const getClassesToC = ({ classes, t, hash }: GetCssToCParams): TableOfContentsParams[] => | ||
!classes || classes.length === 0 | ||
? [] | ||
: [ | ||
{ | ||
text: t('api-docs.classes'), | ||
hash: hash ?? 'classes', | ||
children: [ | ||
...classes.map(({ key, hash: classeHash }) => ({ | ||
text: key, | ||
hash: classeHash, | ||
children: [], | ||
})), | ||
], | ||
}, | ||
]; | ||
|
||
export interface GetClassApiDefinitionsParams { | ||
componentClasses: ComponentApiContent['classes']; | ||
classDescriptions: PropsTranslations['classDescriptions']; | ||
componentName: string; | ||
} | ||
|
||
const errorMessage = (componentName: string, className: string, slotName: string): string => | ||
`${className} description from component ${componentName} should include ${slotName} since its definition includes "{{${slotName}}}"`; | ||
|
||
export function getClassApiDefinitions(params: GetClassApiDefinitionsParams): ClassDefinition[] { | ||
const { componentClasses, classDescriptions, componentName } = params; | ||
|
||
return componentClasses.map((classDefinition) => { | ||
const { | ||
conditions, | ||
nodeName, | ||
deprecationInfo, | ||
description: translatedDescription, | ||
} = classDescriptions[classDefinition.key] ?? {}; // Not all classes have a description. | ||
|
||
let description = translatedDescription ?? classDefinition.description; | ||
|
||
if (description.includes('{{conditions}}')) { | ||
if (!conditions) { | ||
throw Error(errorMessage(componentName, classDefinition.className, 'conditions')); | ||
} | ||
description = description.replace(/{{conditions}}/, conditions); | ||
} | ||
|
||
if (description.includes('{{nodeName}}')) { | ||
if (!nodeName) { | ||
throw Error(errorMessage(componentName, classDefinition.className, 'nodeName')); | ||
} | ||
description = description.replace(/{{nodeName}}/, nodeName); | ||
} | ||
|
||
return { | ||
...classDefinition, | ||
description, | ||
deprecationInfo, | ||
hash: `${kebabCase(componentName)}-classes-${classDefinition.className}`, | ||
}; | ||
}); | ||
} |
Oops, something went wrong.