Skip to content

Commit 713d036

Browse files
committed
Add structured WebSite data to index page if possible
Resolves #2760
1 parent 18c4661 commit 713d036

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

CHANGELOG.md

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

3+
### Features
4+
5+
- If `hostedBaseUrl` is set to the root page on a website, TypeDoc will now include `WebSite` structured data, #2760.
6+
37
### Bug Fixes
48

59
- Fix support for ESM config files with Node 23, #2752.

src/lib/output/themes/default/layouts/default.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@ import type { PageEvent } from "../../../events";
55
import { getDisplayName } from "../../lib";
66
import type { DefaultThemeRenderContext } from "../DefaultThemeRenderContext";
77

8+
// See #2760
9+
function buildSiteMetadata(context: DefaultThemeRenderContext) {
10+
try {
11+
// We have to know where we are hosted in order to generate this block
12+
const url = new URL(context.options.getValue("hostedBaseUrl"));
13+
14+
// No point in generating this if we aren't the root page on the site
15+
if (url.pathname !== "/") {
16+
return null;
17+
}
18+
19+
return (
20+
<script type="application/ld+json">
21+
<Raw
22+
html={JSON.stringify({
23+
"@context": "https://schema.org",
24+
"@type": "WebSite",
25+
name: context.page.project.name,
26+
url: url.toString(),
27+
})}
28+
/>
29+
</script>
30+
);
31+
} catch {
32+
return null;
33+
}
34+
}
35+
836
export const defaultLayout = (
937
context: DefaultThemeRenderContext,
1038
template: RenderTemplate<PageEvent<Reflection>>,
@@ -20,6 +48,7 @@ export const defaultLayout = (
2048
? getDisplayName(props.model)
2149
: `${getDisplayName(props.model)} | ${getDisplayName(props.project)}`}
2250
</title>
51+
{props.url === "index.html" && buildSiteMetadata(context)}
2352
<meta name="description" content={"Documentation for " + props.project.name} />
2453
<meta name="viewport" content="width=device-width, initial-scale=1" />
2554

0 commit comments

Comments
 (0)