@soonit/rspress-plugin-og 0.1.0 → 0.1.2
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/dist/index.mjs +62 -4
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -54,17 +54,74 @@ const baseImages = /* @__PURE__ */ new Map();
|
|
|
54
54
|
function escapeHtml(unsafe) {
|
|
55
55
|
return unsafe.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
56
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Wrap a title string into at most three lines with a best-effort “do not break words” strategy.
|
|
59
|
+
*
|
|
60
|
+
* The input is first split by explicit newlines into paragraphs (empty lines are ignored). Then each
|
|
61
|
+
* paragraph is wrapped into lines whose length does not exceed {@link maxSizePerLine} when possible:
|
|
62
|
+
*
|
|
63
|
+
* - If the paragraph contains whitespace, it is tokenized by whitespace and tokens are joined with a
|
|
64
|
+
* single space.
|
|
65
|
+
* - If the paragraph contains no whitespace (e.g. CJK text), it is tokenized by individual characters.
|
|
66
|
+
* - If a single token is longer than the line limit, it is hard-split into fixed-size chunks.
|
|
67
|
+
*
|
|
68
|
+
* Wrapping stops once three lines have been produced in total.
|
|
69
|
+
*
|
|
70
|
+
* @param input - The title text to wrap. `null`/`undefined`/empty (after trimming) results in `[]`.
|
|
71
|
+
* @param maxSizePerLine - Maximum allowed character count per line. Values `<= 0` are treated as `1`.
|
|
72
|
+
* @returns An array of wrapped lines, with a maximum length of 3.
|
|
73
|
+
*/
|
|
74
|
+
function wrapTitleToLines(input, maxSizePerLine) {
|
|
75
|
+
const text = (input ?? "").trim();
|
|
76
|
+
if (!text) return [];
|
|
77
|
+
const max = Math.max(1, maxSizePerLine || 1);
|
|
78
|
+
const paragraphs = text.split(/\r?\n+/).map((s) => s.trim()).filter(Boolean);
|
|
79
|
+
const out = [];
|
|
80
|
+
for (const para of paragraphs) {
|
|
81
|
+
if (out.length >= 3) break;
|
|
82
|
+
const tokens = /\s/.test(para) ? para.split(/\s+/).filter(Boolean) : [...para];
|
|
83
|
+
let line = "";
|
|
84
|
+
for (const token of tokens) {
|
|
85
|
+
if (out.length >= 3) break;
|
|
86
|
+
const next = line ? /\s/.test(para) ? `${line} ${token}` : `${line}${token}` : token;
|
|
87
|
+
if (next.length <= max) {
|
|
88
|
+
line = next;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (line) {
|
|
92
|
+
out.push(line);
|
|
93
|
+
line = "";
|
|
94
|
+
if (out.length >= 3) break;
|
|
95
|
+
}
|
|
96
|
+
if (token.length > max) {
|
|
97
|
+
const chunks = token.match(new RegExp(`.{1,${max}}`, "g")) ?? [];
|
|
98
|
+
for (const chunk of chunks) {
|
|
99
|
+
if (out.length >= 3) break;
|
|
100
|
+
if (chunk.length === max) out.push(chunk);
|
|
101
|
+
else line = chunk;
|
|
102
|
+
}
|
|
103
|
+
} else line = token;
|
|
104
|
+
}
|
|
105
|
+
if (out.length >= 3) break;
|
|
106
|
+
if (line) out.push(line);
|
|
107
|
+
}
|
|
108
|
+
return out.slice(0, 3);
|
|
109
|
+
}
|
|
57
110
|
async function generateOgImage({ title }, output, options) {
|
|
58
111
|
if (existsSync(output)) return;
|
|
59
112
|
if (!templates.has(options.ogTemplate)) templates.set(options.ogTemplate, readFileSync(options.ogTemplate, "utf-8"));
|
|
60
113
|
const ogTemplate = templates.get(options.ogTemplate);
|
|
61
114
|
if (!baseImages.has(options.ogTemplate)) {
|
|
62
115
|
const baseSvg = ogTemplate.replace(/\{\{[^}]+\}\}/g, "").replace(/<text[^>]*>[\s\S]*?<\/text>/g, "");
|
|
63
|
-
const baseImageBuffer =
|
|
116
|
+
const baseImageBuffer = sharp(Buffer.from(baseSvg)).resize(1200, 630).png().toBuffer();
|
|
64
117
|
baseImages.set(options.ogTemplate, baseImageBuffer);
|
|
65
118
|
}
|
|
66
119
|
mkdirSync(dirname(output), { recursive: true });
|
|
67
|
-
const lines = title
|
|
120
|
+
const lines = wrapTitleToLines(title, options.maxTitleSizePerLine);
|
|
121
|
+
if (lines.length === 0) {
|
|
122
|
+
console.warn("[plugin-og] Missing page title, skip og image generation.");
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
68
125
|
const textLayerBuffer = new Resvg(createTextLayerSvg(ogTemplate, {
|
|
69
126
|
line1: lines[0] ? escapeHtml(lines[0]) : "",
|
|
70
127
|
line2: lines[1] ? escapeHtml(lines[1]) : "",
|
|
@@ -81,7 +138,7 @@ async function generateOgImage({ title }, output, options) {
|
|
|
81
138
|
dpi: 96,
|
|
82
139
|
...options.resvgOptions
|
|
83
140
|
}).render().asPng();
|
|
84
|
-
await sharp(baseImages.get(options.ogTemplate)).composite([{
|
|
141
|
+
await sharp(await baseImages.get(options.ogTemplate)).composite([{
|
|
85
142
|
input: textLayerBuffer,
|
|
86
143
|
blend: "over"
|
|
87
144
|
}]).png().toFile(output);
|
|
@@ -395,7 +452,8 @@ function src_default(userOptions) {
|
|
|
395
452
|
imageUrl: joinURL(options.domain, options.outDir, imageName)
|
|
396
453
|
});
|
|
397
454
|
},
|
|
398
|
-
async afterBuild(config) {
|
|
455
|
+
async afterBuild(config, isProd) {
|
|
456
|
+
if (!isProd) return;
|
|
399
457
|
const outputFolder = join(cwd(), config.outDir ?? "doc_build", options.outDir);
|
|
400
458
|
src_logger.info(`${LOG_PREFIX} Generating OG images to ${relative(cwd(), outputFolder)} ...`);
|
|
401
459
|
const start = performance.now();
|
package/package.json
CHANGED