Skip to content

Commit

Permalink
fix(core): handle duplicate symbols with leading underscores
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed Jan 4, 2025
1 parent 51245fd commit ecd1ec1
Show file tree
Hide file tree
Showing 23 changed files with 624 additions and 281 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { en, jp, ko, zh } from '@plugin/internationalization/index.js';
import { Application, Converter } from 'typedoc';

/**
* Returns subset of translatable strings for the plugin.
*
* These will then be merged with the main set of TypeDoc string.
*
* @category Functions
*/
export function setupInternationalization(app: Application): void {
app.converter.on(Converter.EVENT_BEGIN, () => {
app.internationalization.addTranslations(
Expand All @@ -12,11 +19,7 @@ export function setupInternationalization(app: Application): void {
}

/**
* Returns subset of translatable strings for the plugin.
*
* These will then be merged with the main set of TypeDoc string.
*
* @category Functions
*/
function getTranslatable(app: Application) {
const LOCALES = {
Expand Down
40 changes: 27 additions & 13 deletions packages/typedoc-plugin-markdown/src/theme/base/url-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,23 @@ export class UrlBuilder {
url = removeFirstScopedDirectory(url);
}

const duplicateUrls = this.urls.filter(
(urlMapping) =>
urlMapping.url.toLowerCase() === url.toLowerCase() &&
urlMapping.url !== url,
);

if (duplicateUrls.length > 0) {
while (
this.urls.some(
(urlMapping) =>
urlMapping.url.toLowerCase() === url.toLowerCase() &&
urlMapping.model.name !== reflection.name,
)
) {
const urlParts = url.split('.');
urlParts[urlParts.length - 2] += `-${duplicateUrls.length}`;
const baseName = urlParts.slice(0, -1).join('.');
const match = baseName.match(/-(\d+)$/);
const currentCount = match ? parseInt(match[1], 10) : 0;
const baseWithoutSuffix = match
? baseName.slice(0, match.index)
: baseName;

urlParts[urlParts.length - 2] =
`${baseWithoutSuffix}-${currentCount + 1}`;
url = urlParts.join('.');
}
}
Expand Down Expand Up @@ -523,7 +531,10 @@ export class UrlBuilder {
);
}

private applyAnchorUrl(reflection: Reflection, containerUrl: string) {
private applyAnchorUrl(
reflection: DeclarationReflection,
containerUrl: string,
) {
const anchorPrefix = this.options.getValue('anchorPrefix');
const anchorId = this.getAnchorId(reflection);

Expand Down Expand Up @@ -573,7 +584,7 @@ export class UrlBuilder {
}
}

private getAnchorId(reflection: Reflection) {
private getAnchorId(reflection: DeclarationReflection) {
const preserveAnchorCasing = this.options.getValue('preserveAnchorCasing');

const anchorName = this.getAnchorName(reflection);
Expand All @@ -585,12 +596,15 @@ export class UrlBuilder {
return null;
}

private getAnchorName(reflection: Reflection) {
private getAnchorName(reflection: DeclarationReflection) {
if ([ReflectionKind.TypeParameter].includes(reflection.kind)) {
return null;
}
if (reflection.kind === ReflectionKind.Constructor) {
return 'Constructors';
if (
reflection.kind === ReflectionKind.Constructor &&
reflection.signatures
) {
return `new-${reflection.signatures[0].name}`;
}
const anchorParts = [reflection.name.replace(/[\\[\]]/g, '')];
const typeParams = (reflection as DeclarationReflection)?.typeParameters;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { heading, horizontalRule } from '@plugin/libs/markdown/index.js';
import { MarkdownThemeContext } from '@plugin/theme/index.js';
import {
ContainerReflection,
DeclarationReflection,
DocumentReflection,
ReflectionKind,
} from 'typedoc';

Expand Down Expand Up @@ -37,30 +37,31 @@ export function body(
}),
);
} else {
if (model.children) {
const groupChildren = model.groups
?.filter(
(group) =>
!(group.owningReflection instanceof DocumentReflection),
)
.reduce((acc, group) => {
return [...acc, ...group.children];
}, []);
md.push(
this.partials.members(groupChildren as DeclarationReflection[], {
headingLevel: options.headingLevel,
}),
);
if (model.groups?.length) {
model.groups.forEach((group, i) => {
if (group.allChildrenHaveOwnDocument()) {
md.push(heading(options.headingLevel, group.title));
md.push(this.partials.groupIndex(group));
} else {
md.push(
this.partials.members(
group.children as DeclarationReflection[],
{
headingLevel: options.headingLevel,
},
),
);
if (model.groups && i < model.groups?.length - 1) {
md.push(horizontalRule());
}
}
});
}
}
} else {
const groups = model.groups?.filter(
(group) => !(group.owningReflection instanceof DocumentReflection),
);

if (groups?.length) {
if (model.groups?.length) {
md.push(
this.partials.groups(groups, {
this.partials.groups(model, {
headingLevel: options.headingLevel,
kind: model.kind,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import {

export function categories(
this: MarkdownThemeContext,
model: ReflectionCategory[],
models: ReflectionCategory[],
options: { headingLevel: number },
) {
const md: string[] = [];
model
?.filter((category) => !category.allChildrenHaveOwnDocument())
.forEach((category) => {
models.forEach((category) => {
if (category.allChildrenHaveOwnDocument()) {
md.push(heading(options.headingLevel, category.title));
if (category.description) {
md.push(this.helpers.getCommentParts(category.description));
}
md.push(this.partials.groupIndex(category));
} else {
const categoryChildren = category.children?.filter(
(child) => child.kind !== ReflectionKind.Constructor,
);
Expand All @@ -29,6 +34,7 @@ export function categories(
}),
);
}
});
}
});
return md.join('\n\n');
}
Original file line number Diff line number Diff line change
@@ -1,78 +1,109 @@
import { heading } from '@plugin/libs/markdown/index.js';
import { MarkdownThemeContext } from '@plugin/theme/index.js';
import {
ContainerReflection,
DeclarationReflection,
ReflectionGroup,
ProjectReflection,
ReflectionKind,
} from 'typedoc';

export function groups(
this: MarkdownThemeContext,
model: ReflectionGroup[],
model: ContainerReflection,
options: { headingLevel: number; kind: ReflectionKind },
) {
const groupsWithChildren = model?.filter(
(group) => !group.allChildrenHaveOwnDocument(),
);

const md: string[] = [];

const getGroupTitle = (groupTitle: string) => {
return groupTitle;
};

groupsWithChildren?.forEach((group) => {
const isEventProps = getGroupTitle(group.title) === 'Events';
if (group.categories) {
md.push(heading(options.headingLevel, getGroupTitle(group.title)));
if (group.description) {
md.push(this.helpers.getCommentParts(group.description));
model.groups?.forEach((group) => {
if (
group.title === this.i18n.kind_plural_module() ||
group.allChildrenHaveOwnDocument()
) {
const isPackages =
this.options.getValue('entryPointStrategy') === 'packages' &&
this.getPackagesCount() > 1 &&
group.title === this.i18n.kind_plural_module() &&
model.kind === ReflectionKind.Project;
if (isPackages) {
md.push(heading(options.headingLevel, this.i18n.theme_packages()));
} else {
md.push(heading(options.headingLevel, group.title));
}
md.push(
this.partials.categories(group.categories, {
headingLevel: options.headingLevel + 1,
}),
);
} else {
const isPropertiesGroup = group.children.every(
(child) => child.kind === ReflectionKind.Property,
);

const isEnumGroup = group.children.every(
(child) => child.kind === ReflectionKind.EnumMember,
);

md.push(heading(options.headingLevel, getGroupTitle(group.title)));

if (group.description) {
md.push(this.helpers.getCommentParts(group.description));
}
if (
isPropertiesGroup &&
this.helpers.useTableFormat('properties', options.kind)
) {
md.push(
this.partials.propertiesTable(
group.children as DeclarationReflection[],
{
isEventProps,
},
),
);
} else if (isEnumGroup && this.helpers.useTableFormat('enums')) {
if (group.categories) {
group.categories.forEach((categoryGroup) => {
md.push(heading(options.headingLevel + 1, categoryGroup.title));
if (categoryGroup.description) {
md.push(this.helpers.getCommentParts(categoryGroup.description));
}
md.push(this.partials.groupIndex(categoryGroup));
});
} else {
if (isPackages) {
md.push(this.partials.packagesIndex(model as ProjectReflection));
} else {
md.push(this.partials.groupIndex(group));
}
}
} else {
const isEventProps = getGroupTitle(group.title) === 'Events';
if (group.categories) {
md.push(heading(options.headingLevel, getGroupTitle(group.title)));
if (group.description) {
md.push(this.helpers.getCommentParts(group.description));
}
md.push(
this.partials.enumMembersTable(
group.children as DeclarationReflection[],
),
this.partials.categories(group.categories, {
headingLevel: options.headingLevel + 1,
}),
);
} else {
if (group.children) {
const isPropertiesGroup = group.children.every(
(child) => child.kind === ReflectionKind.Property,
);

const isEnumGroup = group.children.every(
(child) => child.kind === ReflectionKind.EnumMember,
);

md.push(heading(options.headingLevel, getGroupTitle(group.title)));

if (group.description) {
md.push(this.helpers.getCommentParts(group.description));
}
if (
isPropertiesGroup &&
this.helpers.useTableFormat('properties', options.kind)
) {
md.push(
this.partials.propertiesTable(
group.children as DeclarationReflection[],
{
isEventProps,
},
),
);
} else if (isEnumGroup && this.helpers.useTableFormat('enums')) {
md.push(
this.partials.members(group.children as DeclarationReflection[], {
headingLevel: options.headingLevel + 1,
groupTitle: group.title,
}),
this.partials.enumMembersTable(
group.children as DeclarationReflection[],
),
);
} else {
if (group.children) {
md.push(
this.partials.members(group.children as DeclarationReflection[], {
headingLevel: options.headingLevel + 1,
groupTitle: group.title,
}),
);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export * from './member.parametersList.js';
export * from './member.parametersTable.js';
export * from './member.propertiesTable.js';
export * from './member.reference.js';
export * from './member.reflectionIndex.js';
export * from './member.signature.js';
export * from './member.signatureParameters.js';
export * from './member.signatureReturns.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,6 @@ export function memberWithGroups(
});
}

if (
model.documents ||
model?.groups?.some((group) => group.allChildrenHaveOwnDocument())
) {
md.push(
this.partials.reflectionIndex(model, {
headingLevel: options.headingLevel,
}),
);
}

md.push(this.partials.body(model, { headingLevel: options.headingLevel }));

return md.join('\n\n');
Expand Down
Loading

0 comments on commit ecd1ec1

Please sign in to comment.