Skip to content

Commit

Permalink
[docs-infra] Move class deprecation info to translation file
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoAndai committed Jan 9, 2024
1 parent 2adc9dd commit 3ba8004
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type ClassDescription = {
description: string;
nodeName?: string;
conditions?: string;
deprecationInfo?: string;
};
};
export type ClassesSectionProps = {
Expand Down Expand Up @@ -80,6 +81,7 @@ export default function ClassesSection(props: ClassesSectionProps) {
?.replace(/{{conditions}}/, classDescriptions[classDefinition.key].conditions!)
?.replace(/{{nodeName}}/, classDescriptions[classDefinition.key].nodeName!) ??
classDefinition.description,
deprecationInfo: classDescriptions[classDefinition.key]?.deprecationInfo,
};
});

Expand Down
69 changes: 36 additions & 33 deletions packages/api-docs-builder/ApiBuilders/ComponentApiBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ export interface ReactApi extends ReactDocgenApi {
typeDescriptions?: { [t: string]: string };
};
};
classDescriptions: { [key: string]: { description: string; conditions?: string } };
classDescriptions: {
[key: string]: {
description: string;
conditions?: string;
nodeName?: string;
deprecationInfo?: string;
};
};
slotDescriptions?: { [key: string]: string };
};
}
Expand Down Expand Up @@ -318,36 +325,30 @@ async function annotateComponentDefinition(api: ReactApi) {
/**
* Substitute CSS class description conditions with placeholder
*/
function extractClassConditions(descriptions: any) {
const classConditions: {
[key: string]: { description: string; conditions?: string; nodeName?: string };
} = {};
function extractClassCondition(description: string) {
const stylesRegex =
/((Styles|State class|Class name) applied to )(.*?)(( if | unless | when |, ){1}(.*))?\./;

Object.entries(descriptions).forEach(([className, description]: any) => {
if (className) {
const conditions = description.match(stylesRegex);

if (conditions && conditions[6]) {
classConditions[className] = {
description: renderMarkdown(
description.replace(stylesRegex, '$1{{nodeName}}$5{{conditions}}.'),
),
nodeName: renderMarkdown(conditions[3]),
conditions: renderMarkdown(conditions[6].replace(/`(.*?)`/g, '<code>$1</code>')),
};
} else if (conditions && conditions[3] && conditions[3] !== 'the root element') {
classConditions[className] = {
description: renderMarkdown(description.replace(stylesRegex, '$1{{nodeName}}$5.')),
nodeName: renderMarkdown(conditions[3]),
};
} else {
classConditions[className] = { description: renderMarkdown(description) };
}
}
});
return classConditions;
const conditions = description.match(stylesRegex);

if (conditions && conditions[6]) {
return {
description: renderMarkdown(
description.replace(stylesRegex, '$1{{nodeName}}$5{{conditions}}.'),
),
nodeName: renderMarkdown(conditions[3]),
conditions: renderMarkdown(conditions[6].replace(/`(.*?)`/g, '<code>$1</code>')),
};
}

if (conditions && conditions[3] && conditions[3] !== 'the root element') {
return {
description: renderMarkdown(description.replace(stylesRegex, '$1{{nodeName}}$5.')),
nodeName: renderMarkdown(conditions[3]),
};
}

return { description: renderMarkdown(description) };
}

/**
Expand Down Expand Up @@ -526,14 +527,16 @@ const attachTranslations = (reactApi: ReactApi) => {
}

/**
* CSS class descriptions.
* CSS class descriptions and deprecations.
*/
const classDescriptions: Record<string, string> = {};
reactApi.classes.forEach((classDefinition) => {
classDescriptions[classDefinition.key] = classDefinition.description;
reactApi.classes.forEach((classDefinition, index) => {
translations.classDescriptions[classDefinition.key] = {
...extractClassCondition(classDefinition.description),
deprecationInfo: classDefinition.deprecationInfo,
};
delete reactApi.classes[index].deprecationInfo; // store deprecation info in translations only
});

translations.classDescriptions = extractClassConditions(classDescriptions);
reactApi.translations = translations;
};

Expand Down

0 comments on commit 3ba8004

Please sign in to comment.