@barodoc/plugin-og-image 6.0.0 → 7.0.0

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.
@@ -0,0 +1,19 @@
1
+ import * as _barodoc_core from '@barodoc/core';
2
+
3
+ interface OgImagePluginOptions {
4
+ /** Font file path (TTF/OTF) for rendering text. Falls back to a system default. */
5
+ fontPath?: string;
6
+ /** Background color. Default: "#ffffff" */
7
+ background?: string;
8
+ /** Text color. Default: "#0f172a" */
9
+ textColor?: string;
10
+ /** Accent color for the site name. Default: "#0070f3" */
11
+ accentColor?: string;
12
+ /** Image width. Default: 1200 */
13
+ width?: number;
14
+ /** Image height. Default: 630 */
15
+ height?: number;
16
+ }
17
+ declare const _default: (options: OgImagePluginOptions) => _barodoc_core.BarodocPlugin;
18
+
19
+ export { type OgImagePluginOptions, _default as default };
package/dist/index.js ADDED
@@ -0,0 +1,139 @@
1
+ // src/index.ts
2
+ import { definePlugin } from "@barodoc/core";
3
+ import satori from "satori";
4
+ import sharp from "sharp";
5
+ import { readFileSync, writeFileSync, mkdirSync, existsSync } from "fs";
6
+ import { join } from "path";
7
+ var GOOGLE_FONT_CSS = "https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap";
8
+ async function fetchGoogleFont(weight = 400) {
9
+ const cssRes = await fetch(GOOGLE_FONT_CSS, {
10
+ headers: { "User-Agent": "node" }
11
+ });
12
+ const css = await cssRes.text();
13
+ const blocks = css.split("@font-face");
14
+ for (const block of blocks) {
15
+ const wMatch = block.match(/font-weight:\s*(\d+)/);
16
+ if (!wMatch || parseInt(wMatch[1]) !== weight) continue;
17
+ const urlMatch = block.match(/src:\s*url\(([^)]+)\)/);
18
+ if (urlMatch?.[1]) {
19
+ const fontRes = await fetch(urlMatch[1]);
20
+ return fontRes.arrayBuffer();
21
+ }
22
+ }
23
+ throw new Error(`Could not extract font URL for weight ${weight}`);
24
+ }
25
+ function createOgSvg(options) {
26
+ return {
27
+ type: "div",
28
+ props: {
29
+ style: {
30
+ display: "flex",
31
+ flexDirection: "column",
32
+ justifyContent: "center",
33
+ padding: "60px 80px",
34
+ width: `${options.width}px`,
35
+ height: `${options.height}px`,
36
+ backgroundColor: options.background
37
+ },
38
+ children: [
39
+ {
40
+ type: "div",
41
+ props: {
42
+ style: { fontSize: "24px", color: options.accentColor, marginBottom: "16px" },
43
+ children: options.siteName
44
+ }
45
+ },
46
+ {
47
+ type: "div",
48
+ props: {
49
+ style: {
50
+ fontSize: "48px",
51
+ fontWeight: 700,
52
+ color: options.textColor,
53
+ lineHeight: 1.2,
54
+ marginBottom: "20px"
55
+ },
56
+ children: options.title
57
+ }
58
+ },
59
+ {
60
+ type: "div",
61
+ props: {
62
+ style: { fontSize: "24px", color: "#64748b", lineHeight: 1.4 },
63
+ children: options.description
64
+ }
65
+ }
66
+ ]
67
+ }
68
+ };
69
+ }
70
+ var index_default = definePlugin((options = {}) => {
71
+ const width = options.width ?? 1200;
72
+ const height = options.height ?? 630;
73
+ const background = options.background ?? "#ffffff";
74
+ const textColor = options.textColor ?? "#0f172a";
75
+ const accentColor = options.accentColor ?? "#0070f3";
76
+ return {
77
+ name: "@barodoc/plugin-og-image",
78
+ hooks: {
79
+ "build:done": async (buildContext, context) => {
80
+ const { config } = context;
81
+ const outDir = join(buildContext.outDir, "og");
82
+ if (!existsSync(outDir)) mkdirSync(outDir, { recursive: true });
83
+ const fonts = [];
84
+ if (options.fontPath) {
85
+ try {
86
+ const fontData = readFileSync(options.fontPath).buffer;
87
+ fonts.push({ name: "Custom", data: fontData, weight: 400, style: "normal" });
88
+ } catch {
89
+ console.warn("[og-image] Could not load custom font, fetching Inter from Google Fonts");
90
+ }
91
+ }
92
+ if (fonts.length === 0) {
93
+ try {
94
+ const [regular, bold] = await Promise.all([
95
+ fetchGoogleFont(400),
96
+ fetchGoogleFont(700)
97
+ ]);
98
+ fonts.push(
99
+ { name: "Inter", data: regular, weight: 400, style: "normal" },
100
+ { name: "Inter", data: bold, weight: 700, style: "normal" }
101
+ );
102
+ } catch (err) {
103
+ console.warn("[og-image] Could not fetch default font, skipping OG image generation:", err);
104
+ return;
105
+ }
106
+ }
107
+ let count = 0;
108
+ for (const group of config.navigation) {
109
+ for (const page of group.pages) {
110
+ const slug = page.replace(/\//g, "-");
111
+ const title = page.split("/").pop().split("-").map((w) => w.charAt(0).toUpperCase() + w.slice(1)).join(" ");
112
+ const tree = createOgSvg({
113
+ title,
114
+ description: "",
115
+ siteName: config.name,
116
+ width,
117
+ height,
118
+ background,
119
+ textColor,
120
+ accentColor
121
+ });
122
+ try {
123
+ const svg = await satori(tree, { width, height, fonts });
124
+ const png = await sharp(Buffer.from(svg)).png().toBuffer();
125
+ writeFileSync(join(outDir, `${slug}.png`), png);
126
+ count++;
127
+ } catch (err) {
128
+ console.warn(`[og-image] Failed to generate image for ${page}:`, err);
129
+ }
130
+ }
131
+ }
132
+ console.log(`[og-image] Generated ${count} OG images in og/`);
133
+ }
134
+ }
135
+ };
136
+ });
137
+ export {
138
+ index_default as default
139
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barodoc/plugin-og-image",
3
- "version": "6.0.0",
3
+ "version": "7.0.0",
4
4
  "description": "Open Graph image generation plugin for Barodoc",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -20,14 +20,14 @@
20
20
  },
21
21
  "peerDependencies": {
22
22
  "astro": "^5.0.0",
23
- "@barodoc/core": "7.0.0"
23
+ "@barodoc/core": "8.0.0"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/node": "^22.0.0",
27
27
  "astro": "^5.0.0",
28
28
  "tsup": "^8.3.0",
29
29
  "typescript": "^5.7.0",
30
- "@barodoc/core": "7.0.0"
30
+ "@barodoc/core": "8.0.0"
31
31
  },
32
32
  "license": "MIT",
33
33
  "scripts": {