Skip to content

Page metadata and share images

Metadata and PNG rendering live in optional @dirwell/share-image. Core and themes do not install it. Plain emits no social metadata or image assets by default.

Terminal window
npm install --save-dev @dirwell/core @dirwell/theme-plain @dirwell/share-image
import { generateExplorer } from "@dirwell/core";
import { createPlainTheme } from "@dirwell/theme-plain";
import { createMetadataProvider } from "@dirwell/share-image";
await generateExplorer({
sourceDir: "files",
outputDir: "public/downloads",
theme: createPlainTheme(),
metadata: createMetadataProvider(
{ siteName: "Downloads", siteUrl: "https://example.com/downloads/" },
{ siteName: "Downloads", repositoryName: "you/downloads", imageTheme: "plain" },
),
});

siteUrl is the deployed explorer root, including its prefix. It supplies absolute canonical and share URLs. Use a content name, not just a theme name. Titles identify the current folder; describe direct folder contents separately from archive-wide totals shown on a shared cover.

title and description accept strings or async callbacks. Callbacks receive site archive counts and directory direct counts/path. Omit imageTheme for text-only metadata. image: false disables a preset. A local path, file URL or File supplies a custom image; an object adds outputPath and alt.

When using relative image paths, pass the intended source directory as the third argument to createMetadataProvider. Metadata resolves before output replacement. Image bytes are deduplicated per build and conflicting output paths are rejected.

import { createMetadataProvider, createShareImage, describeContent } from "@dirwell/share-image";
export const metadata = createMetadataProvider(
{
siteName: "Design kit",
siteUrl: "https://example.com/design/",
description: ({ directory }) => describeContent(directory),
image: async ({ directory, site, description }) => ({
source: await createShareImage({
theme: "crosswave",
title: directory.relativePath || site.name,
description,
directoryPath: directory.relativePath,
}),
outputPath: `og/${directory.relativePath || "root"}.png`,
alt: `${directory.relativePath || site.name}: ${description}`,
}),
},
{ siteName: "Design kit", repositoryName: "you/design" },
);

Use this provider as build.metadata, core metadata or adapter metadata. See the dynamic image demo. This is build-time rendering, not a live server image endpoint.

Presets generate 1200 × 630 PNGs using bundled fonts without network/system-font lookup. Unsupported glyphs fail rather than silently becoming missing characters. Supply a licensed local font for other coverage; use a separate renderer for shaping outside the qualified workload. createShareImage returns a PNG File; optional image dependencies stay outside Core/Plain.

Supply fonts: { body: new URL("./fonts/MyLicensedFont.ttf", import.meta.url) } to createShareImage for branded or CJK text. An optional headline font overrides the body fallback. Local paths resolve from cwd; single-face TTF/OTF files must be at most 32 MiB and cover all displayed characters, including preset labels. No font download or system-font fallback occurs. Byte changes invalidate the bounded image cache; custom text measurement and glyph outlines consume the same parsed bytes.

Pass the returned File to the metadata provider, or use the existing async image callback for per-directory text. Missing glyph errors identify the role/code point. Collections and WOFF formats are unsupported. The test-only OFL subset demonstrates Latin/Traditional Chinese/Japanese rendering, and is deliberately not a complete CJK font. Complex-script/emoji rendering needs separate qualification.

Core supplies the planned public page path to the metadata provider, independently of the physical output filename. Canonical and social URLs must agree with the final host URL, including page number and deployment prefix. Metadata remains opt-in; a canonical tag does not implement a redirect. See directory URLs.