Skip to content

Commit

Permalink
Add structured WebSite data to index page if possible
Browse files Browse the repository at this point in the history
Resolves #2760
  • Loading branch information
Gerrit0 committed Oct 27, 2024
1 parent 18c4661 commit 713d036
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

### Features

- If `hostedBaseUrl` is set to the root page on a website, TypeDoc will now include `WebSite` structured data, #2760.

### Bug Fixes

- Fix support for ESM config files with Node 23, #2752.
Expand Down
29 changes: 29 additions & 0 deletions src/lib/output/themes/default/layouts/default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,34 @@ import type { PageEvent } from "../../../events";
import { getDisplayName } from "../../lib";
import type { DefaultThemeRenderContext } from "../DefaultThemeRenderContext";

// See #2760
function buildSiteMetadata(context: DefaultThemeRenderContext) {
try {
// We have to know where we are hosted in order to generate this block
const url = new URL(context.options.getValue("hostedBaseUrl"));

// No point in generating this if we aren't the root page on the site
if (url.pathname !== "/") {
return null;
}

return (
<script type="application/ld+json">
<Raw
html={JSON.stringify({
"@context": "https://schema.org",
"@type": "WebSite",
name: context.page.project.name,
url: url.toString(),
})}
/>
</script>
);
} catch {
return null;
}
}

export const defaultLayout = (
context: DefaultThemeRenderContext,
template: RenderTemplate<PageEvent<Reflection>>,
Expand All @@ -20,6 +48,7 @@ export const defaultLayout = (
? getDisplayName(props.model)
: `${getDisplayName(props.model)} | ${getDisplayName(props.project)}`}
</title>
{props.url === "index.html" && buildSiteMetadata(context)}
<meta name="description" content={"Documentation for " + props.project.name} />
<meta name="viewport" content="width=device-width, initial-scale=1" />

Expand Down

0 comments on commit 713d036

Please sign in to comment.