@writedocs/generator 0.10.16 → 0.10.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -2
- package/src/assets/fonts/LICENSE.txt +93 -0
- package/src/assets/fonts/inter-latin-400-normal.woff +0 -0
- package/src/assets/fonts/inter-latin-600-normal.woff +0 -0
- package/src/assets/fonts/inter-latin-ext-400-normal.woff +0 -0
- package/src/assets/fonts/inter-latin-ext-600-normal.woff +0 -0
- package/src/layout/BaseLayout.astro +3 -1
- package/src/lib/og-image.js +158 -0
- package/src/lib/seo-tags.js +4 -1
- package/src/pages/[...slug].astro +4 -0
- package/src/pages/og/[...slug].png.ts +63 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@writedocs/generator",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.18",
|
|
4
4
|
"description": "Static site generator for docs — a writedocs.json + MDX folder in, a static site out.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"bugs": {
|
|
43
43
|
"url": "https://github.com/writedocs/writedocs/issues"
|
|
44
44
|
},
|
|
45
|
-
"author": "Gabriel Raeder",
|
|
45
|
+
"author": "Gabriel Raeder Gonçalves",
|
|
46
46
|
"license": "ISC",
|
|
47
47
|
"publishConfig": {
|
|
48
48
|
"access": "public"
|
|
@@ -64,6 +64,7 @@
|
|
|
64
64
|
"@iconify-json/simple-icons": "1.2.93",
|
|
65
65
|
"@iconify-json/tabler": "1.2.38",
|
|
66
66
|
"@mdx-js/mdx": "3.1.1",
|
|
67
|
+
"@resvg/resvg-wasm": "2.6.2",
|
|
67
68
|
"@shikijs/transformers": "4.4.3",
|
|
68
69
|
"@tailwindcss/vite": "4.3.3",
|
|
69
70
|
"astro": "7.2.9",
|
|
@@ -79,6 +80,7 @@
|
|
|
79
80
|
"react-dom": "19.2.8",
|
|
80
81
|
"rehype-katex": "7.0.1",
|
|
81
82
|
"remark-math": "6.0.0",
|
|
83
|
+
"satori": "0.44.0",
|
|
82
84
|
"shiki": "4.4.3",
|
|
83
85
|
"tailwindcss": "4.3.3",
|
|
84
86
|
"zod": "4.4.3"
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Copyright 2016 The Inter Project Authors (https://github.com/rsms/inter) Inter-Italic[opsz,wght].ttf: Copyright 2016 The Inter Project Authors (https://github.com/rsms/inter)
|
|
2
|
+
|
|
3
|
+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
|
4
|
+
This license is copied below, and is also available with a FAQ at:
|
|
5
|
+
http://scripts.sil.org/OFL
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
-----------------------------------------------------------
|
|
9
|
+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
|
10
|
+
-----------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
PREAMBLE
|
|
13
|
+
The goals of the Open Font License (OFL) are to stimulate worldwide
|
|
14
|
+
development of collaborative font projects, to support the font creation
|
|
15
|
+
efforts of academic and linguistic communities, and to provide a free and
|
|
16
|
+
open framework in which fonts may be shared and improved in partnership
|
|
17
|
+
with others.
|
|
18
|
+
|
|
19
|
+
The OFL allows the licensed fonts to be used, studied, modified and
|
|
20
|
+
redistributed freely as long as they are not sold by themselves. The
|
|
21
|
+
fonts, including any derivative works, can be bundled, embedded,
|
|
22
|
+
redistributed and/or sold with any software provided that any reserved
|
|
23
|
+
names are not used by derivative works. The fonts and derivatives,
|
|
24
|
+
however, cannot be released under any other type of license. The
|
|
25
|
+
requirement for fonts to remain under this license does not apply
|
|
26
|
+
to any document created using the fonts or their derivatives.
|
|
27
|
+
|
|
28
|
+
DEFINITIONS
|
|
29
|
+
"Font Software" refers to the set of files released by the Copyright
|
|
30
|
+
Holder(s) under this license and clearly marked as such. This may
|
|
31
|
+
include source files, build scripts and documentation.
|
|
32
|
+
|
|
33
|
+
"Reserved Font Name" refers to any names specified as such after the
|
|
34
|
+
copyright statement(s).
|
|
35
|
+
|
|
36
|
+
"Original Version" refers to the collection of Font Software components as
|
|
37
|
+
distributed by the Copyright Holder(s).
|
|
38
|
+
|
|
39
|
+
"Modified Version" refers to any derivative made by adding to, deleting,
|
|
40
|
+
or substituting -- in part or in whole -- any of the components of the
|
|
41
|
+
Original Version, by changing formats or by porting the Font Software to a
|
|
42
|
+
new environment.
|
|
43
|
+
|
|
44
|
+
"Author" refers to any designer, engineer, programmer, technical
|
|
45
|
+
writer or other person who contributed to the Font Software.
|
|
46
|
+
|
|
47
|
+
PERMISSION & CONDITIONS
|
|
48
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
49
|
+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
|
50
|
+
redistribute, and sell modified and unmodified copies of the Font
|
|
51
|
+
Software, subject to the following conditions:
|
|
52
|
+
|
|
53
|
+
1) Neither the Font Software nor any of its individual components,
|
|
54
|
+
in Original or Modified Versions, may be sold by itself.
|
|
55
|
+
|
|
56
|
+
2) Original or Modified Versions of the Font Software may be bundled,
|
|
57
|
+
redistributed and/or sold with any software, provided that each copy
|
|
58
|
+
contains the above copyright notice and this license. These can be
|
|
59
|
+
included either as stand-alone text files, human-readable headers or
|
|
60
|
+
in the appropriate machine-readable metadata fields within text or
|
|
61
|
+
binary files as long as those fields can be easily viewed by the user.
|
|
62
|
+
|
|
63
|
+
3) No Modified Version of the Font Software may use the Reserved Font
|
|
64
|
+
Name(s) unless explicit written permission is granted by the corresponding
|
|
65
|
+
Copyright Holder. This restriction only applies to the primary font name as
|
|
66
|
+
presented to the users.
|
|
67
|
+
|
|
68
|
+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
|
69
|
+
Software shall not be used to promote, endorse or advertise any
|
|
70
|
+
Modified Version, except to acknowledge the contribution(s) of the
|
|
71
|
+
Copyright Holder(s) and the Author(s) or with their explicit written
|
|
72
|
+
permission.
|
|
73
|
+
|
|
74
|
+
5) The Font Software, modified or unmodified, in part or in whole,
|
|
75
|
+
must be distributed entirely under this license, and must not be
|
|
76
|
+
distributed under any other license. The requirement for fonts to
|
|
77
|
+
remain under this license does not apply to any document created
|
|
78
|
+
using the Font Software.
|
|
79
|
+
|
|
80
|
+
TERMINATION
|
|
81
|
+
This license becomes null and void if any of the above conditions are
|
|
82
|
+
not met.
|
|
83
|
+
|
|
84
|
+
DISCLAIMER
|
|
85
|
+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
86
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
|
87
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
|
88
|
+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
|
89
|
+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
90
|
+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
|
91
|
+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
92
|
+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
|
93
|
+
OTHER DEALINGS IN THE FONT SOFTWARE.
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -158,6 +158,9 @@ const showTocCol = mode === "default";
|
|
|
158
158
|
// link, and the description, robots, Open Graph and Twitter metas, with
|
|
159
159
|
// `seo.metatags` replacing or adding to them.
|
|
160
160
|
const siteUrl = resolveSiteUrl(config);
|
|
161
|
+
// The site's logo - the light one, or the dark one when that's all there is -
|
|
162
|
+
// for the organization in the structured data.
|
|
163
|
+
const siteLogo = typeof config.styles.logo === "string" ? config.styles.logo : (config.styles.logo?.light ?? config.styles.logo?.dark);
|
|
161
164
|
const head = seoHead({
|
|
162
165
|
title,
|
|
163
166
|
siteName: config.name,
|
|
@@ -170,7 +173,6 @@ const head = seoHead({
|
|
|
170
173
|
// hreflang: only on a page search engines index, and only with full URLs.
|
|
171
174
|
const languageLinks = seo.noindex ? [] : hreflangLinks(siteUrl, alternates);
|
|
172
175
|
// Structured data - see lib/json-ld.js for what a page gets.
|
|
173
|
-
const siteLogo = typeof config.styles.logo === "string" ? config.styles.logo : config.styles.logo?.light;
|
|
174
176
|
const structuredData = jsonLd({
|
|
175
177
|
siteUrl,
|
|
176
178
|
siteName: config.name,
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// The picture a link to a page shows when it's shared - generated for every
|
|
2
|
+
// page that has none of its own: 1200x630, with the site's logo, the page's
|
|
3
|
+
// title and description, and a band of the site's primary color.
|
|
4
|
+
//
|
|
5
|
+
// Two steps, both without a browser or a system font:
|
|
6
|
+
// 1. satori lays the card out (flexbox, text wrapping) and draws every
|
|
7
|
+
// letter as a path, from the Inter files in src/assets/fonts/. Because
|
|
8
|
+
// the letters are paths, the result looks the same on any machine - a
|
|
9
|
+
// build server has no fonts installed to rely on.
|
|
10
|
+
// 2. resvg (WebAssembly, so no binary per platform) turns that SVG into a
|
|
11
|
+
// PNG. Social networks and chat apps don't show SVG previews.
|
|
12
|
+
//
|
|
13
|
+
// src/pages/og/[...slug].png.ts serves the result at /og/<page>.png;
|
|
14
|
+
// [...slug].astro points a page's og:image there when it has no
|
|
15
|
+
// `seo.ogImage`.
|
|
16
|
+
//
|
|
17
|
+
// Plain JavaScript, for scripts/og-image.test.js.
|
|
18
|
+
import fs from 'node:fs';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import { createRequire } from 'node:module';
|
|
21
|
+
import { fileURLToPath } from 'node:url';
|
|
22
|
+
import { imageSize } from './image-size.js';
|
|
23
|
+
import { plainLine } from './meta-description.js';
|
|
24
|
+
|
|
25
|
+
export const OG_WIDTH = 1200;
|
|
26
|
+
export const OG_HEIGHT = 630;
|
|
27
|
+
|
|
28
|
+
/** Whether a page gets a generated picture: it has none of its own (`seo`
|
|
29
|
+
* is the site's with the page's merged over it), search engines index it,
|
|
30
|
+
* and it's a page - not a link to somewhere else (`url`). */
|
|
31
|
+
export function wantsGeneratedCard(seo, data = {}) {
|
|
32
|
+
return !seo?.ogImage && !seo?.noindex && !data.url;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The address of a page's generated picture: /og/<page id>.png. */
|
|
36
|
+
export function ogImagePath(pageId) {
|
|
37
|
+
return `/og/${pageId}.png`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// The font files, by the package's own folder when the CLI says where it is
|
|
41
|
+
// (WRITEDOCS_PACKAGE_ROOT, src/cli/run-astro.js): inside a build this module
|
|
42
|
+
// is part of a bundle somewhere else, and a path from its own location
|
|
43
|
+
// would miss them.
|
|
44
|
+
const fontsDir = process.env.WRITEDOCS_PACKAGE_ROOT
|
|
45
|
+
? path.join(process.env.WRITEDOCS_PACKAGE_ROOT, 'src', 'assets', 'fonts')
|
|
46
|
+
: path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'assets', 'fonts');
|
|
47
|
+
const fontFile = (name) => fs.readFileSync(path.join(fontsDir, name));
|
|
48
|
+
|
|
49
|
+
let fonts = null;
|
|
50
|
+
/** Inter, regular and semibold. The extended-Latin files go under a name of
|
|
51
|
+
* their own, second in the card's font-family: satori takes a letter from
|
|
52
|
+
* the first family that has it, and only one file per family and weight. */
|
|
53
|
+
function loadFonts() {
|
|
54
|
+
fonts ??= [
|
|
55
|
+
{ name: 'Inter', weight: 400, style: 'normal', data: fontFile('inter-latin-400-normal.woff') },
|
|
56
|
+
{ name: 'Inter', weight: 600, style: 'normal', data: fontFile('inter-latin-600-normal.woff') },
|
|
57
|
+
{ name: 'Inter Ext', weight: 400, style: 'normal', data: fontFile('inter-latin-ext-400-normal.woff') },
|
|
58
|
+
{ name: 'Inter Ext', weight: 600, style: 'normal', data: fontFile('inter-latin-ext-600-normal.woff') },
|
|
59
|
+
];
|
|
60
|
+
return fonts;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// The two renderers are loaded when the first card is drawn, not with this
|
|
64
|
+
// module: every page imports it for wantsGeneratedCard(), and most builds of
|
|
65
|
+
// a page never draw anything.
|
|
66
|
+
let renderer = null;
|
|
67
|
+
function loadRenderer() {
|
|
68
|
+
renderer ??= (async () => {
|
|
69
|
+
const [{ default: satori }, resvg] = await Promise.all([import('satori'), import('@resvg/resvg-wasm')]);
|
|
70
|
+
// Once per process: a second initWasm() throws.
|
|
71
|
+
await resvg.initWasm(fs.readFileSync(createRequire(import.meta.url).resolve('@resvg/resvg-wasm/index_bg.wasm')));
|
|
72
|
+
return { satori, Resvg: resvg.Resvg };
|
|
73
|
+
})();
|
|
74
|
+
return renderer;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const MIME = { '.svg': 'image/svg+xml', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp' };
|
|
78
|
+
|
|
79
|
+
/** The logo file as something the card can draw: a data URI and the size to
|
|
80
|
+
* draw it at - `height` tall, as wide as its proportions say, and no wider
|
|
81
|
+
* than `maxWidth`. Null for a file that can't be read or measured. */
|
|
82
|
+
export function logoForCard(file, height = 64, maxWidth = 520) {
|
|
83
|
+
if (!file) return null;
|
|
84
|
+
const type = MIME[path.extname(file).toLowerCase()];
|
|
85
|
+
const size = type && imageSize(file);
|
|
86
|
+
if (!size) return null;
|
|
87
|
+
let width = (size.width / size.height) * height;
|
|
88
|
+
let drawn = height;
|
|
89
|
+
if (width > maxWidth) {
|
|
90
|
+
drawn = (maxWidth / width) * height;
|
|
91
|
+
width = maxWidth;
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
return { src: `data:${type};base64,${fs.readFileSync(file).toString('base64')}`, width: Math.round(width), height: Math.round(drawn) };
|
|
95
|
+
} catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const cut = (value, max) => (value.length > max ? `${value.slice(0, max - 1).replace(/\s+\S*$/, '')}…` : value);
|
|
101
|
+
const el = (type, style, children, props = {}) => ({ type, props: { style, children, ...props } });
|
|
102
|
+
|
|
103
|
+
/** The card as satori's element tree. A long title gets smaller letters
|
|
104
|
+
* rather than being cut short; the description is clamped to three lines. */
|
|
105
|
+
export function ogCard({ title, description, siteName, primary = '#0029F5', logo = null }) {
|
|
106
|
+
const heading = cut(plainLine(title), 110);
|
|
107
|
+
const text = description ? cut(plainLine(description), 200) : '';
|
|
108
|
+
const titleSize = heading.length > 70 ? 52 : heading.length > 40 ? 62 : 72;
|
|
109
|
+
return el(
|
|
110
|
+
'div',
|
|
111
|
+
{ width: OG_WIDTH, height: OG_HEIGHT, display: 'flex', flexDirection: 'column', backgroundColor: '#ffffff', fontFamily: 'Inter, Inter Ext', color: '#18191d' },
|
|
112
|
+
[
|
|
113
|
+
el(
|
|
114
|
+
'div',
|
|
115
|
+
{ display: 'flex', flexDirection: 'column', flexGrow: 1, padding: '72px 80px 0 80px' },
|
|
116
|
+
[
|
|
117
|
+
// The logo - or, for a site without one (or with one on another
|
|
118
|
+
// server, which a build shouldn't wait for), its name.
|
|
119
|
+
logo
|
|
120
|
+
? el('img', { width: logo.width, height: logo.height }, undefined, { src: logo.src, width: logo.width, height: logo.height })
|
|
121
|
+
: el('div', { display: 'flex', fontSize: 34, fontWeight: 600, color: primary }, siteName),
|
|
122
|
+
el(
|
|
123
|
+
'div',
|
|
124
|
+
{ display: 'flex', flexDirection: 'column', flexGrow: 1, justifyContent: 'center' },
|
|
125
|
+
[
|
|
126
|
+
el('div', { display: 'flex', fontSize: titleSize, fontWeight: 600, lineHeight: 1.12, letterSpacing: '-0.02em' }, heading),
|
|
127
|
+
text ? el('div', { display: 'flex', marginTop: 28, fontSize: 30, lineHeight: 1.4, color: '#404045', lineClamp: 3 }, text) : null,
|
|
128
|
+
].filter(Boolean)
|
|
129
|
+
),
|
|
130
|
+
]
|
|
131
|
+
),
|
|
132
|
+
el(
|
|
133
|
+
'div',
|
|
134
|
+
{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '0 80px 44px 80px', fontSize: 24, color: '#6b6b76' },
|
|
135
|
+
// With a logo, the name says whose docs these are; without one the
|
|
136
|
+
// name is already at the top.
|
|
137
|
+
[el('div', { display: 'flex' }, logo ? siteName : '')]
|
|
138
|
+
),
|
|
139
|
+
el('div', { display: 'flex', width: OG_WIDTH, height: 14, backgroundColor: primary }, undefined),
|
|
140
|
+
]
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The card as SVG. */
|
|
145
|
+
export async function ogImageSvg(options) {
|
|
146
|
+
const { satori } = await loadRenderer();
|
|
147
|
+
return satori(ogCard(options), { width: OG_WIDTH, height: OG_HEIGHT, fonts: loadFonts() });
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The card as a PNG: a Buffer, 1200x630. */
|
|
151
|
+
export async function ogImagePng(options) {
|
|
152
|
+
const svg = await ogImageSvg(options);
|
|
153
|
+
const { Resvg } = await loadRenderer();
|
|
154
|
+
const rendered = new Resvg(svg, { fitTo: { mode: 'width', value: OG_WIDTH } }).render();
|
|
155
|
+
const png = Buffer.from(rendered.asPng());
|
|
156
|
+
rendered.free?.();
|
|
157
|
+
return png;
|
|
158
|
+
}
|
package/src/lib/seo-tags.js
CHANGED
|
@@ -96,8 +96,11 @@ export function seoHead({ title, siteName, description, siteDescription, seo = {
|
|
|
96
96
|
const metaDescription = seo.description?.trim() || description || siteDescription;
|
|
97
97
|
const canonical = canonicalUrl(siteUrl, path, seo.canonical);
|
|
98
98
|
const custom = seo.metatags ?? {};
|
|
99
|
+
// The picture in a link preview: the page's or the site's `ogImage` -
|
|
100
|
+
// or, set by [...slug].astro when there is neither, the card generated
|
|
101
|
+
// for the page (lib/og-image.js).
|
|
99
102
|
const image = seo.ogImage ? absoluteUrl(siteUrl, seo.ogImage) : undefined;
|
|
100
|
-
// The large card needs a picture -
|
|
103
|
+
// The large card needs a picture - that one, or a tag set by hand.
|
|
101
104
|
const hasImage = Boolean(image || custom['og:image'] || custom['twitter:image']);
|
|
102
105
|
const robots = typeof seo.robots === 'string' && seo.robots.trim() ? seo.robots.trim() : seo.noindex ? 'noindex, nofollow' : undefined;
|
|
103
106
|
|
|
@@ -25,6 +25,7 @@ import {
|
|
|
25
25
|
import { writedocsTempDir } from "../lib/writedocs-temp-dir.js";
|
|
26
26
|
import { parseOpenApiRef } from "../lib/openapi-ref.js";
|
|
27
27
|
import { languageAlternates } from "../lib/item-pages.js";
|
|
28
|
+
import { wantsGeneratedCard, ogImagePath } from "../lib/og-image.js";
|
|
28
29
|
import BaseLayout from "../layout/BaseLayout.astro";
|
|
29
30
|
import Sidebar from "../layout/components/Sidebar.astro";
|
|
30
31
|
import TableOfContents from "../layout/components/TableOfContents.astro";
|
|
@@ -483,6 +484,9 @@ const globalDropdowns = buildGlobalDropdowns(
|
|
|
483
484
|
// than recomputing it, since it already encodes the same
|
|
484
485
|
// frontmatter-slug-override-aware routing logic.
|
|
485
486
|
const pageSeo = mergeSeo(config.seo, entry.data.seo);
|
|
487
|
+
// A page with no picture of its own for link previews gets the generated
|
|
488
|
+
// card (lib/og-image.js, served by src/pages/og/[...slug].png.ts).
|
|
489
|
+
if (wantsGeneratedCard(pageSeo, entry.data)) pageSeo.ogImage = ogImagePath(normalizeEntryId(entry.id));
|
|
486
490
|
const currentPath = hrefForSlug(currentFileId);
|
|
487
491
|
// This page in each language, for hreflang (languageAlternates() in
|
|
488
492
|
// lib/item-pages.js) - a hidden page belongs to no language level.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { getCollection, type CollectionEntry } from 'astro:content';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import type { APIRoute } from 'astro';
|
|
5
|
+
import { loadDocsConfig, normalizeEntryId, findAllPages, mergeSeo } from '../../lib/config';
|
|
6
|
+
import { writedocsTempDir } from '../../lib/writedocs-temp-dir.js';
|
|
7
|
+
import { ogImagePng, logoForCard, wantsGeneratedCard } from '../../lib/og-image.js';
|
|
8
|
+
import { imageFileFor } from '../../lib/image-size.js';
|
|
9
|
+
|
|
10
|
+
// /og/<page>.png - the picture a link to a page shows when it's shared, for
|
|
11
|
+
// every page with no `seo.ogImage` of its own: the site's logo, the page's
|
|
12
|
+
// title and description, the site's primary color (lib/og-image.js).
|
|
13
|
+
// [...slug].astro points the page's og:image here under the same condition
|
|
14
|
+
// (wantsGeneratedCard()), so a card is only built for a page that uses it -
|
|
15
|
+
// a site with `seo.ogImage` in writedocs.json builds none at all.
|
|
16
|
+
//
|
|
17
|
+
// A static route like [...slug].md.ts next to it: one file per page at build
|
|
18
|
+
// time, and built on request in `writedocs dev`.
|
|
19
|
+
|
|
20
|
+
type DocsEntry = CollectionEntry<'pages'> | CollectionEntry<'generatedDocs'>;
|
|
21
|
+
|
|
22
|
+
export async function getStaticPaths() {
|
|
23
|
+
const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
|
|
24
|
+
const config = loadDocsConfig(contentDir);
|
|
25
|
+
const hasGeneratedDocs = fs.existsSync(path.join(writedocsTempDir(contentDir), 'generated-docs'));
|
|
26
|
+
const hasPages = findAllPages(contentDir).length > 0;
|
|
27
|
+
const [pagesEntries, generatedDocsEntries] = await Promise.all([
|
|
28
|
+
hasPages ? getCollection('pages') : Promise.resolve([]),
|
|
29
|
+
hasGeneratedDocs ? getCollection('generatedDocs') : Promise.resolve([]),
|
|
30
|
+
]);
|
|
31
|
+
const entries: DocsEntry[] = [...pagesEntries, ...generatedDocsEntries];
|
|
32
|
+
|
|
33
|
+
// The logo is read and encoded once, not once per page.
|
|
34
|
+
const logoSetting = config.styles.logo;
|
|
35
|
+
const logoPath = typeof logoSetting === 'string' ? logoSetting : (logoSetting?.light ?? logoSetting?.dark);
|
|
36
|
+
const logo = logoForCard(logoPath ? imageFileFor(logoPath, contentDir) : null);
|
|
37
|
+
|
|
38
|
+
return entries
|
|
39
|
+
.filter((entry) => wantsGeneratedCard(mergeSeo(config.seo, entry.data.seo), entry.data))
|
|
40
|
+
.map((entry) => ({
|
|
41
|
+
params: { slug: normalizeEntryId(entry.id) },
|
|
42
|
+
props: {
|
|
43
|
+
title: entry.data.title,
|
|
44
|
+
description: entry.data.seo?.description ?? entry.data.description ?? config.description,
|
|
45
|
+
siteName: config.name,
|
|
46
|
+
primary: config.styles.colors.primary,
|
|
47
|
+
logo,
|
|
48
|
+
},
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface Props {
|
|
53
|
+
title: string;
|
|
54
|
+
description?: string;
|
|
55
|
+
siteName: string;
|
|
56
|
+
primary: string;
|
|
57
|
+
logo: { src: string; width: number; height: number } | null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const GET: APIRoute = async ({ props }) => {
|
|
61
|
+
const png = await ogImagePng(props as Props);
|
|
62
|
+
return new Response(png, { headers: { 'Content-Type': 'image/png' } });
|
|
63
|
+
};
|