-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathcustom-theme.mjs
163 lines (147 loc) · 5.77 KB
/
custom-theme.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// @ts-check
import { ReflectionType, UnionType } from 'typedoc';
import { MarkdownTheme, MarkdownThemeContext } from 'typedoc-plugin-markdown';
/**
* @param {import('typedoc-plugin-markdown').MarkdownApplication} app
*/
export function load(app) {
app.renderer.defineTheme('clerkTheme', ClerkMarkdownTheme);
}
class ClerkMarkdownTheme extends MarkdownTheme {
/**
* @param {import('typedoc-plugin-markdown').MarkdownPageEvent<import('typedoc').Reflection>} page
*/
getRenderContext(page) {
return new ClerkMarkdownThemeContext(this, page, this.application.options);
}
}
/**
* Our custom Clerk theme
* @extends MarkdownThemeContext
*/
class ClerkMarkdownThemeContext extends MarkdownThemeContext {
/**
* @param {MarkdownTheme} theme
* @param {import('typedoc-plugin-markdown').MarkdownPageEvent<import('typedoc').Reflection>} page
* @param {MarkdownTheme["application"]["options"]} options
*/
constructor(theme, page, options) {
super(theme, page, options);
const superPartials = this.partials;
this.partials = {
...superPartials,
/**
* This hides the "Type parameters" section and the signature title from the output
* @param {import('typedoc').SignatureReflection} model
* @param {{ headingLevel: number, nested?: boolean, accessor?: string, multipleSignatures?: boolean; hideTitle?: boolean }} options
*/
signature: (model, options) => {
const customizedOptions = {
...options,
hideTitle: true,
};
const customizedModel = model;
customizedModel.typeParameters = undefined;
const output = superPartials.signature(customizedModel, customizedOptions);
return output;
},
/**
* This hides the "Type parameters" section from the output
* @param {import('typedoc').DeclarationReflection} model
* @param {{ headingLevel: number }} options
*/
memberWithGroups: (model, options) => {
const customizedModel = model;
customizedModel.typeParameters = undefined;
const output = superPartials.memberWithGroups(customizedModel, options);
return output;
},
/**
* This hides the "Type parameters" section and the declaration title from the output
* @param {import('typedoc').DeclarationReflection} model
* @param {{ headingLevel: number, nested?: boolean }} options
*/
declaration: (model, options = { headingLevel: 2, nested: false }) => {
// Create a local override
const localPartials = {
...this.partials,
declarationTitle: () => '',
};
// Store original so that we can restore it later
const originalPartials = this.partials;
const customizedModel = model;
customizedModel.typeParameters = undefined;
this.partials = localPartials;
const output = superPartials.declaration(customizedModel, options);
this.partials = originalPartials;
return output;
},
/**
* This modifies the output of union types by wrapping everything in a single `<code>foo | bar</code>` tag instead of doing `<code>foo</code>` | `<code>bar</code>`
* @param {import('typedoc').UnionType} model
*/
unionType: model => {
const defaultOutput = superPartials.unionType(model);
const output = defaultOutput
// Escape stuff that would be turned into markdown
.replace(/__experimental_/g, '\\_\\_experimental\\_')
// Remove any backticks
.replace(/`/g, '')
// Remove any `<code>` and `</code>` tags
.replace(/<code>/g, '')
.replace(/<\/code>/g, '');
return `<code>${output}</code>`;
},
/**
* This ensures that everything is wrapped in a single codeblock (not individual ones)
* @param {import('typedoc').SignatureReflection[]} model
* @param {{ forceParameterType?: boolean; typeSeparator?: string }} [options]
*/
functionType: (model, options) => {
const defaultOutput = superPartials.functionType(model, options);
const delimiter = this.options.getValue('useCodeBlocks') ? ';\n' : '; ';
const output = defaultOutput
.split(delimiter)
.map(fn =>
fn
// Remove any backticks
.replace(/`/g, '')
// Remove any `<code>` and `</code>` tags
.replace(/<code>/g, '')
.replace(/<\/code>/g, ''),
)
.join(delimiter);
return `<code>${output}</code>`;
},
/**
* Copied from original theme.
* Changes:
* - Remove summaries over tables (TODO: Use elementSummaries instead)
* - Only use one newline between items
* https://github.com/typedoc2md/typedoc-plugin-markdown/blob/7032ebd3679aead224cf23bffd0f3fb98443d16e/packages/typedoc-plugin-markdown/src/theme/context/partials/member.typeDeclarationUnionContainer.ts
* @param {import('typedoc').DeclarationReflection} model
* @param {{ headingLevel: number }} options
*/
typeDeclarationUnionContainer: (model, options) => {
/**
* @type {string[]}
*/
const md = [];
if (model.type instanceof UnionType) {
const elementSummaries = model.type?.elementSummaries;
model.type.types.forEach((type, i) => {
if (type instanceof ReflectionType) {
md.push(this.partials.typeDeclarationContainer(model, type.declaration, options));
} else {
md.push(`${this.partials.someType(type)}`);
}
if (elementSummaries?.[i]) {
md.push(this.helpers.getCommentParts(elementSummaries[i]));
}
});
}
return md.join('\n');
},
};
}
}