Skip to content

Commit e15e839

Browse files
committed
Optimize HTML output
See #2287
1 parent c5d1ec5 commit e15e839

File tree

4 files changed

+49
-30
lines changed

4 files changed

+49
-30
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Unreleased
22

3+
### Features
4+
5+
- TypeDoc's `--pretty` option now also controls whether generated HTML contains line breaks, #2287.
6+
- Optimized icon caching to reduce file size in generated HTML documentation, #2287.
7+
38
## v0.24.7 (2023-05-08)
49

510
### Features

src/lib/output/renderer.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ export class Renderer extends ChildableComponent<
201201
@BindOption("darkHighlightTheme")
202202
darkTheme!: ShikiTheme;
203203

204+
/** @internal */
205+
@BindOption("pretty")
206+
pretty!: boolean;
207+
204208
renderStartTime = -1;
205209

206210
/**
@@ -227,6 +231,8 @@ export class Renderer extends ChildableComponent<
227231
project: ProjectReflection,
228232
outputDirectory: string
229233
): Promise<void> {
234+
setRenderSettings({ pretty: this.pretty });
235+
230236
const momento = this.hooks.saveMomento();
231237
this.renderStartTime = Date.now();
232238
await loadHighlighter(this.lightTheme, this.darkTheme);
@@ -399,3 +405,4 @@ export class Renderer extends ChildableComponent<
399405
// HACK: THIS HAS TO STAY DOWN HERE
400406
// if you try to move it up to the top of the file, then you'll run into stuff being used before it has been defined.
401407
import "./plugins";
408+
import { setRenderSettings } from "../utils/jsx";

src/lib/output/themes/default/partials/icon.tsx

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,23 @@ function cachedPart(key: string, svgPart: JSX.Element) {
2323
}
2424

2525
const kindIcon = (kind: ReflectionKind, letterPath: JSX.Element, color: string, circular = false) => (
26-
<svg class="tsd-kind-icon" width="24" height="24" viewBox="0 0 24 24">
26+
<svg class="tsd-kind-icon" viewBox="0 0 24 24">
2727
{cachedPart(
28-
`${kind}-path`,
29-
<rect
30-
fill="var(--color-icon-background)"
31-
stroke={color}
32-
stroke-width="1.5"
33-
x="1"
34-
y="1"
35-
width="22"
36-
height="22"
37-
rx={circular ? "12" : "6"}
38-
/>
28+
`${kind}`,
29+
<g>
30+
<rect
31+
fill="var(--color-icon-background)"
32+
stroke={color}
33+
stroke-width="1.5"
34+
x="1"
35+
y="1"
36+
width="22"
37+
height="22"
38+
rx={circular ? "12" : "6"}
39+
/>
40+
{letterPath}
41+
</g>
3942
)}
40-
{cachedPart(`${kind}-text`, letterPath)}
4143
</svg>
4244
);
4345

@@ -191,10 +193,13 @@ export const icons: Record<
191193
),
192194
chevronDown: () => (
193195
<svg width="20" height="20" viewBox="0 0 24 24" fill="none">
194-
<path
195-
d="M4.93896 8.531L12 15.591L19.061 8.531L16.939 6.409L12 11.349L7.06098 6.409L4.93896 8.531Z"
196-
fill="var(--color-text)"
197-
/>
196+
{cachedPart(
197+
"chevronDown",
198+
<path
199+
d="M4.93896 8.531L12 15.591L19.061 8.531L16.939 6.409L12 11.349L7.06098 6.409L4.93896 8.531Z"
200+
fill="var(--color-text)"
201+
/>
202+
)}
198203
</svg>
199204
),
200205
chevronSmall: () => (
@@ -234,18 +239,15 @@ export const icons: Record<
234239
</svg>
235240
),
236241
anchor: () => (
237-
<svg
238-
class="icon icon-tabler icon-tabler-link"
239-
viewBox="0 0 24 24"
240-
stroke-width="2"
241-
stroke="currentColor"
242-
fill="none"
243-
stroke-linecap="round"
244-
stroke-linejoin="round"
245-
>
246-
{cachedPart("anchor-a", <path stroke="none" d="M0 0h24v24H0z" fill="none" />)}
247-
{cachedPart("anchor-b", <path d="M10 14a3.5 3.5 0 0 0 5 0l4 -4a3.5 3.5 0 0 0 -5 -5l-.5 .5" />)}
248-
{cachedPart("anchor-c", <path d="M14 10a3.5 3.5 0 0 0 -5 0l-4 4a3.5 3.5 0 0 0 5 5l.5 -.5" />)}
242+
<svg viewBox="0 0 24 24">
243+
{cachedPart(
244+
"anchor",
245+
<g stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
246+
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
247+
<path d="M10 14a3.5 3.5 0 0 0 5 0l4 -4a3.5 3.5 0 0 0 -5 -5l-.5 .5" />
248+
<path d="M14 10a3.5 3.5 0 0 0 -5 0l-4 4a3.5 3.5 0 0 0 5 5l.5 -.5" />
249+
</g>
250+
)}
249251
</svg>
250252
),
251253
};

src/lib/utils/jsx.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ export function createElement(
101101
return { tag, props, children };
102102
}
103103

104+
let renderPretty = true;
105+
export function setRenderSettings(options: { pretty: boolean }) {
106+
renderPretty = options.pretty;
107+
}
108+
104109
export function renderElement(element: JsxElement | null | undefined): string {
105110
if (!element) {
106111
return "";
@@ -118,7 +123,7 @@ export function renderElement(element: JsxElement | null | undefined): string {
118123
const html: string[] = [];
119124

120125
if (tag !== Fragment) {
121-
if (blockElements.has(tag)) {
126+
if (blockElements.has(tag) && renderPretty) {
122127
html.push("\n");
123128
}
124129
html.push("<", tag);

0 commit comments

Comments
 (0)