Skip to content

Commit cc967ca

Browse files
committed
fix(core): fix symbol url anchors when "flattenOutputFiles" is "true" (#616)
1 parent 62062af commit cc967ca

File tree

3 files changed

+77
-80
lines changed

3 files changed

+77
-80
lines changed

packages/typedoc-plugin-markdown/src/theme/core/url-builder.ts

+41-45
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function buildUrls(theme: MarkdownTheme, project: ProjectReflection) {
5858
buildUrlsFromProject(project);
5959
}
6060

61-
return flattenOutputFiles ? flattenFiles(urls) : urls;
61+
return urls;
6262

6363
function buildEntryUrls() {
6464
const preserveReadme =
@@ -235,28 +235,35 @@ export function buildUrls(theme: MarkdownTheme, project: ProjectReflection) {
235235
);
236236

237237
if (mapping) {
238-
const directory = urlOptions.directory || mapping.directory;
239-
const urlPath = getUrlPath(reflection, {
240-
...urlOptions,
241-
directory,
242-
});
238+
let url: string;
239+
let urlPath = '';
240+
241+
if (flattenOutputFiles) {
242+
url = getFlattenedUrl(reflection);
243+
} else {
244+
const directory = urlOptions.directory || mapping.directory;
245+
urlPath = getUrlPath(reflection, {
246+
...urlOptions,
247+
directory,
248+
});
243249

244-
let url = getUrl(reflection, urlPath, urlOptions);
250+
url = getUrl(reflection, urlPath, urlOptions);
245251

246-
if (ignoreScopes) {
247-
url = removeFirstScopedDirectory(url);
248-
}
252+
if (ignoreScopes) {
253+
url = removeFirstScopedDirectory(url);
254+
}
249255

250-
const duplicateUrls = urls.filter(
251-
(urlMapping) =>
252-
urlMapping.url.toLowerCase().replace(/-\d+$/, '') ===
253-
url.toLowerCase(),
254-
);
256+
const duplicateUrls = urls.filter(
257+
(urlMapping) =>
258+
urlMapping.url.toLowerCase().replace(/-\d+$/, '') ===
259+
url.toLowerCase(),
260+
);
255261

256-
if (duplicateUrls.length > 0) {
257-
const urlParts = url.split('.');
258-
urlParts[urlParts.length - 2] += `-${duplicateUrls.length}`;
259-
url = urlParts.join('.');
262+
if (duplicateUrls.length > 0) {
263+
const urlParts = url.split('.');
264+
urlParts[urlParts.length - 2] += `-${duplicateUrls.length}`;
265+
url = urlParts.join('.');
266+
}
260267
}
261268

262269
urls.push({
@@ -475,31 +482,20 @@ export function buildUrls(theme: MarkdownTheme, project: ProjectReflection) {
475482
: getFileNameWithExtension('globals', fileExtension);
476483
}
477484

478-
function flattenFiles(urls: UrlMapping<Reflection>[]) {
479-
const getUrl = (reflection: Reflection) => {
480-
const fullName = reflection.getFullName();
481-
const fullNameParts = fullName.replace(/\//g, '.').split('.');
482-
if (reflection.kind !== ReflectionKind.Module) {
483-
fullNameParts.splice(
484-
fullNameParts.length - 1,
485-
0,
486-
ReflectionKind.singularString(reflection.kind).split(' ')[0],
487-
);
488-
}
489-
const url = `${fullNameParts.join('.')}${fileExtension}`
490-
.replace(/"/g, '')
491-
.replace(/^\./g, '');
492-
reflection.url = url;
493-
return url;
494-
};
495-
return urls.map((urlMapping) => {
496-
if (urlMapping.model.kind === ReflectionKind.Project) {
497-
return urlMapping;
498-
}
499-
return {
500-
...urlMapping,
501-
url: getUrl(urlMapping.model),
502-
};
503-
});
485+
function getFlattenedUrl(reflection: DeclarationReflection) {
486+
const fullName = reflection.getFullName();
487+
const fullNameParts = fullName.replace(/\//g, '.').split('.');
488+
if (reflection.kind !== ReflectionKind.Module) {
489+
fullNameParts.splice(
490+
fullNameParts.length - 1,
491+
0,
492+
ReflectionKind.singularString(reflection.kind).split(' ')[0],
493+
);
494+
}
495+
const url = `${fullNameParts.join('.')}${fileExtension}`
496+
.replace(/"/g, '')
497+
.replace(/^\./g, '');
498+
reflection.url = url;
499+
return url;
504500
}
505501
}

packages/typedoc-plugin-markdown/test/fixtures/config.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ const config: Record<string, Fixture> = {
116116
{
117117
useHTMLAnchors: true,
118118
preserveAnchorCasing: true,
119-
publicPath: '/some-path',
120-
fileExtension: 'mdx',
119+
publicPath: 'http://example.com',
120+
fileExtension: '.mdx',
121121
sanitizeComments: true,
122+
flattenOutputFiles: true,
122123
},
123124
],
124125
},

packages/typedoc-plugin-markdown/test/specs/__snapshots__/comments.spec.ts.snap

+33-33
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ Comments form module comments
109109
110110
Links using \`{@link}\` inline tags.
111111
112-
- [CommentInterface](/some-path/interfaces/CommentInterface.mdx) - Links to CommentInterface
113-
- [Links to CommentInterface.prop](/some-path/interfaces/CommentInterface.mdx#prop)
114-
- [Links to CommentInterface.propb](/some-path/interfaces/CommentInterface.mdx#propb)
115-
- [CommentEnum.MemberB](/some-path/enumerations/CommentEnum.mdx#MemberB)
116-
- [SameName:var](/some-path/variables/SameName.mdx)
117-
- [SameName:interface](/some-path/interfaces/SameName.mdx)
118-
- [SameName.prop](/some-path/interfaces/SameName.mdx#prop)
119-
- [prop:var](/some-path/variables/prop.mdx)
120-
- [_prop_with_underscore:var](/some-path/variables/prop_with_underscore.mdx)
112+
- [CommentInterface](http:/example.com/Interface.CommentInterface.mdx) - Links to CommentInterface
113+
- [Links to CommentInterface.prop](http:/example.com/Interface.CommentInterface.mdx#prop)
114+
- [Links to CommentInterface.propb](http:/example.com/Interface.CommentInterface.mdx#propb)
115+
- [CommentEnum.MemberB](http:/example.com/Enumeration.CommentEnum.mdx#MemberB)
116+
- [SameName:var](http:/example.com/Variable.SameName.mdx)
117+
- [SameName:interface](http:/example.com/Interface.SameName.mdx)
118+
- [SameName.prop](http:/example.com/Interface.SameName.mdx#prop)
119+
- [prop:var](http:/example.com/Variable.prop.mdx)
120+
- [_prop_with_underscore:var](http:/example.com/Variable._prop_with_underscore.mdx)
121121
122122
External links:
123123
@@ -178,25 +178,25 @@ This is a simple example on how to use include.
178178
179179
## Enumerations
180180
181-
- [CommentEnum](/some-path/enumerations/CommentEnum.mdx)
181+
- [CommentEnum](http:/example.com/Enumeration.CommentEnum.mdx)
182182
183183
## Interfaces
184184
185-
- [CommentInterface](/some-path/interfaces/CommentInterface.mdx)
186-
- [CommentInterfaceExtended](/some-path/interfaces/CommentInterfaceExtended.mdx)
187-
- [SameName](/some-path/interfaces/SameName.mdx)
185+
- [CommentInterface](http:/example.com/Interface.CommentInterface.mdx)
186+
- [CommentInterfaceExtended](http:/example.com/Interface.CommentInterfaceExtended.mdx)
187+
- [SameName](http:/example.com/Interface.SameName.mdx)
188188
189189
## Variables
190190
191-
- [SameName](/some-path/variables/SameName.mdx)
192-
- [\\_prop\\_with\\_underscore](/some-path/variables/prop_with_underscore.mdx)
193-
- [prop](/some-path/variables/prop.mdx)
194-
- [propb](/some-path/variables/propb.mdx)
191+
- [SameName](http:/example.com/Variable.SameName.mdx)
192+
- [\\_prop\\_with\\_underscore](http:/example.com/Variable._prop_with_underscore.mdx)
193+
- [prop](http:/example.com/Variable.prop.mdx)
194+
- [propb](http:/example.com/Variable.propb.mdx)
195195
196196
## Functions
197197
198-
- [multipleExampleTags](/some-path/functions/multipleExampleTags.mdx)
199-
- [singleExampleTag](/some-path/functions/singleExampleTag.mdx)
198+
- [multipleExampleTags](http:/example.com/Function.multipleExampleTags.mdx)
199+
- [singleExampleTag](http:/example.com/Function.singleExampleTag.mdx)
200200
"
201201
`;
202202
@@ -414,15 +414,15 @@ Comments form module comments
414414
415415
Links using \`{@link}\` inline tags.
416416
417-
- [CommentInterface](/some-path/README.mdx#CommentInterface) - Links to CommentInterface
418-
- [Links to CommentInterface.prop](/some-path/README.mdx#prop)
419-
- [Links to CommentInterface.propb](/some-path/README.mdx#propb)
420-
- [CommentEnum.MemberB](/some-path/README.mdx#MemberB)
421-
- [SameName:var](/some-path/README.mdx#SameName-1)
422-
- [SameName:interface](/some-path/README.mdx#SameName)
423-
- [SameName.prop](/some-path/README.mdx#prop-2)
424-
- [prop:var](/some-path/README.mdx#prop-3)
425-
- [_prop_with_underscore:var](/some-path/README.mdx#_prop_with_underscore)
417+
- [CommentInterface](http:/example.com/README.mdx#CommentInterface) - Links to CommentInterface
418+
- [Links to CommentInterface.prop](http:/example.com/README.mdx#prop)
419+
- [Links to CommentInterface.propb](http:/example.com/README.mdx#propb)
420+
- [CommentEnum.MemberB](http:/example.com/README.mdx#MemberB)
421+
- [SameName:var](http:/example.com/README.mdx#SameName-1)
422+
- [SameName:interface](http:/example.com/README.mdx#SameName)
423+
- [SameName.prop](http:/example.com/README.mdx#prop-2)
424+
- [prop:var](http:/example.com/README.mdx#prop-3)
425+
- [_prop_with_underscore:var](http:/example.com/README.mdx#_prop_with_underscore)
426426
427427
External links:
428428
@@ -491,7 +491,7 @@ This is a simple example on how to use include.
491491
492492
| Enumeration Member | Value | Description |
493493
| :------ | :------ | :------ |
494-
| <a id="Member" name="Member"></a> \`Member\` | \`0\` | <p>Comment for Member</p>Some \\<p\\> html \\</p\\> and \\<tag\\>\\</tag\\>.<p>**Deprecated**</p><p>Deprecated member</p><p>**See**</p><p>[SameName](/some-path/README.mdx#SameName-1)</p> |
494+
| <a id="Member" name="Member"></a> \`Member\` | \`0\` | <p>Comment for Member</p>Some \\<p\\> html \\</p\\> and \\<tag\\>\\</tag\\>.<p>**Deprecated**</p><p>Deprecated member</p><p>**See**</p><p>[SameName](http:/example.com/README.mdx#SameName-1)</p> |
495495
| <a id="MemberB" name="MemberB"></a> \`MemberB\` | \`1\` | - |
496496
497497
## Interfaces
@@ -502,7 +502,7 @@ This is a simple example on how to use include.
502502
503503
#### Extended by
504504
505-
- [\`CommentInterfaceExtended\`](/some-path/README.mdx#CommentInterfaceExtended)
505+
- [\`CommentInterfaceExtended\`](http:/example.com/README.mdx#CommentInterfaceExtended)
506506
507507
#### Properties
508508
@@ -519,14 +519,14 @@ This is a simple example on how to use include.
519519
520520
#### Extends
521521
522-
- [\`CommentInterface\`](/some-path/README.mdx#CommentInterface)
522+
- [\`CommentInterface\`](http:/example.com/README.mdx#CommentInterface)
523523
524524
#### Properties
525525
526526
| Property | Type | Inherited from |
527527
| :------ | :------ | :------ |
528-
| <a id="prop-1" name="prop-1"></a> \`prop\` | \`string\` | [\`CommentInterface\`](/some-path/README.mdx#CommentInterface).[\`prop\`](/some-path/README.mdx#prop) |
529-
| <a id="propb-1" name="propb-1"></a> \`propb\` | \`string\` | [\`CommentInterface\`](/some-path/README.mdx#CommentInterface).[\`propb\`](/some-path/README.mdx#propb) |
528+
| <a id="prop-1" name="prop-1"></a> \`prop\` | \`string\` | [\`CommentInterface\`](http:/example.com/README.mdx#CommentInterface).[\`prop\`](http:/example.com/README.mdx#prop) |
529+
| <a id="propb-1" name="propb-1"></a> \`propb\` | \`string\` | [\`CommentInterface\`](http:/example.com/README.mdx#CommentInterface).[\`propb\`](http:/example.com/README.mdx#propb) |
530530
| <a id="propc" name="propc"></a> \`propc\` | \`string\` | - |
531531
532532
***

0 commit comments

Comments
 (0)