Skip to content

Commit

Permalink
fix(core): handle quoted symbol names (#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed May 11, 2024
1 parent 7d8a703 commit 5b412fb
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/typedoc-plugin-markdown/src/libs/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { formatMarkdown } from './format-markdown';
export { formatTableColumn } from './format-table-column';
export { getFileNameWithExtension } from './get-file-name-with-extension';
export { getFirstParagrph } from './get-first-paragraph';
export { isQuoted } from './is-quoted';
export { removeFirstScopedDirectory } from './remove-first-scoped-directory';
export { removeLineBreaks } from './remove-line-breaks';
export { sanitizeComments } from './sanitize-comments';
Expand Down
3 changes: 3 additions & 0 deletions packages/typedoc-plugin-markdown/src/libs/utils/is-quoted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isQuoted(str: string) {
return str.startsWith('"') && str.endsWith('"');
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MarkdownRenderer } from '@plugin/app/application';
import { isQuoted } from '@plugin/libs/utils';
import { OutputFileStrategy, PLURAL_KIND_KEY_MAP } from '@plugin/options';
import { MarkdownTheme, NavigationItem } from '@plugin/theme';
import * as path from 'path';
Expand Down Expand Up @@ -262,7 +263,7 @@ export function buildNavigation(
child: DeclarationReflection,
children: NavigationItem[] | null,
) {
if (!navigationOptions.excludeFolders) {
if (!isQuoted(child.name) && !navigationOptions.excludeFolders) {
const titleParts = child.name.split('/');
if (!child.name.startsWith('@') && titleParts.length > 1) {
let currentLevel = acc;
Expand Down
28 changes: 19 additions & 9 deletions packages/typedoc-plugin-markdown/src/theme/core/url-builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MarkdownRenderer } from '@plugin/app/application';
import {
getFileNameWithExtension,
isQuoted,
removeFirstScopedDirectory,
slugifyUrl,
} from '@plugin/libs/utils';
Expand Down Expand Up @@ -313,10 +314,7 @@ export function buildUrls(theme: MarkdownTheme, project: ProjectReflection) {
}

function getUrlPath(reflection: DeclarationReflection, urlOption: UrlOption) {
const alias = reflection.name
.replace(/^_+|_+$/g, '')
.replace(/</, '-')
.replace(/>/, '-');
const alias = getAlias(reflection.name);

const parentDir = urlOption.parentUrl
? path.dirname(urlOption.parentUrl)
Expand Down Expand Up @@ -364,6 +362,16 @@ export function buildUrls(theme: MarkdownTheme, project: ProjectReflection) {
);
}

function getAlias(name: string) {
if (isQuoted(name)) {
name = name.replace(/\//g, '_');
}
return name
.replace(/"/g, '')
.replace(/^_+|_+$/g, '')
.replace(/[<>]/g, '-');
}

function applyAnchorUrl(
reflection: DeclarationReflection,
container: Reflection,
Expand Down Expand Up @@ -471,16 +479,18 @@ export function buildUrls(theme: MarkdownTheme, project: ProjectReflection) {

function flattenFiles(urls: UrlMapping<Reflection>[]) {
const getUrl = (reflection: Reflection) => {
const fullname = reflection.getFullName();
const fullnameParts = fullname.replace(/\//g, '.').split('.');
const fullName = reflection.getFullName();
const fullNameParts = fullName.replace(/\//g, '.').split('.');
if (reflection.kind !== ReflectionKind.Module) {
fullnameParts.splice(
fullnameParts.length - 1,
fullNameParts.splice(
fullNameParts.length - 1,
0,
ReflectionKind.singularString(reflection.kind).split(' ')[0],
);
}
const url = `${fullnameParts.join('.')}.md`;
const url = `${fullNameParts.join('.')}${fileExtension}`
.replace(/"/g, '')
.replace(/^\./g, '');
reflection.url = url;
return url;
};
Expand Down
6 changes: 5 additions & 1 deletion packages/typedoc-plugin-markdown/test/fixtures/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ const config: Record<string, Fixture> = {
},
modules: {
only: false,
entryPoints: ['/modules/module-1', '/modules/module-2'],
entryPoints: [
'/modules/module-1',
'/modules/module-2',
'/modules/module-3',
],
commonOptions: {
plugin: [path.join(__dirname, 'custom-plugins', 'navigation-plugin.mjs')],
readme: 'none',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* @module "/Test/Module/Name"
*/
export interface InterfaceA {}
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,23 @@ exports[`Navigation should gets Navigation Json for entry modules: (Output File

exports[`Navigation should gets Navigation Json for modules with parts: (Output File Strategy "members") (Option Group "1") 1`] = `
"[
{
"title": "\\"/Test/Module/Name\\"",
"kind": 2,
"path": "Test_Module_Name/README.md",
"children": [
{
"title": "Interfaces",
"children": [
{
"title": "InterfaceA",
"kind": 256,
"path": "Test_Module_Name/interfaces/InterfaceA.md"
}
]
}
]
},
{
"title": "@scope/namespace",
"kind": 2,
Expand Down Expand Up @@ -1454,6 +1471,23 @@ exports[`Navigation should gets Navigation Json for modules with parts: (Output

exports[`Navigation should gets Navigation Json for modules with parts: (Output File Strategy "members") (Option Group "2") 1`] = `
"[
{
"title": "\\"/Test/Module/Name\\"",
"kind": 2,
"path": "Test.Module.Name.md",
"children": [
{
"title": "Interfaces",
"children": [
{
"title": "InterfaceA",
"kind": 256,
"path": "Test.Module.Name.Interface.InterfaceA.md"
}
]
}
]
},
{
"title": "@scope/namespace",
"kind": 2,
Expand Down Expand Up @@ -1861,6 +1895,11 @@ exports[`Navigation should gets Navigation Json for modules with parts: (Output

exports[`Navigation should gets Navigation Json for modules with parts: (Output File Strategy "modules") (Option Group "1") 1`] = `
"[
{
"title": "\\"/Test/Module/Name\\"",
"kind": 2,
"path": "Test_Module_Name.md"
},
{
"title": "@scope/namespace",
"kind": 2,
Expand Down Expand Up @@ -1911,6 +1950,11 @@ exports[`Navigation should gets Navigation Json for modules with parts: (Output

exports[`Navigation should gets Navigation Json for modules with parts: (Output File Strategy "modules") (Option Group "2") 1`] = `
"[
{
"title": "\\"/Test/Module/Name\\"",
"kind": 2,
"path": "Test.Module.Name.md"
},
{
"title": "@scope/namespace",
"kind": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ exports[`Urls should gets Urls for entry module: outputFileStrategy: modules 2`]
exports[`Urls should gets Urls for modules parts: outputFileStrategy: members 1`] = `
[
"README.md",
"Test_Module_Name/README.md",
"Test_Module_Name/interfaces/InterfaceA.md",
"module-1/submodules/submodule-1/README.md",
"module-1/submodules/submodule-1/classes/ClassA.md",
"module-1/submodules/submodule-1/classes/ClassB.md",
Expand Down Expand Up @@ -112,6 +114,8 @@ exports[`Urls should gets Urls for modules parts: outputFileStrategy: members 2`
"@scope.namespace.Type.TypeB.md",
"@scope.namespace.md",
"README.md",
"Test.Module.Name.Interface.InterfaceA.md",
"Test.Module.Name.md",
"module-1.submodules.submodule-1.Class.ClassA.md",
"module-1.submodules.submodule-1.Class.ClassB.md",
"module-1.submodules.submodule-1.Enumeration.EnumA.md",
Expand Down Expand Up @@ -163,6 +167,7 @@ exports[`Urls should gets Urls for modules parts: outputFileStrategy: members 2`
exports[`Urls should gets Urls for modules parts: outputFileStrategy: modules 1`] = `
[
"README.md",
"Test_Module_Name.md",
"module-1/submodules/submodule-1.md",
"module-1/submodules/submodule-2.md",
"module-1/submodules/submodule-3-with-modules/nested-submodule-1.md",
Expand All @@ -176,6 +181,7 @@ exports[`Urls should gets Urls for modules parts: outputFileStrategy: modules 2`
[
"@scope.namespace.md",
"README.md",
"Test.Module.Name.md",
"module-1.submodules.submodule-1.md",
"module-1.submodules.submodule-2.md",
"module-1.submodules.submodule-3-with-modules.nested-submodule-1.md",
Expand Down

0 comments on commit 5b412fb

Please sign in to comment.