Schedule automatic content for publishing

This commit is contained in:
Loma Bot Ops
2026-11-28 19:54:22 +07:00
parent 279c6eb956
commit a6e3d47b7c
13 changed files with 139 additions and 33 deletions
+22 -3
View File
@@ -1,4 +1,9 @@
import sharp from "sharp";
import path from "node:path";
const notoSansThaiFiles = path.join(process.cwd(), "node_modules", "@fontsource", "noto-sans-thai", "files");
const notoSansThaiSemiBold = path.join(notoSansThaiFiles, "noto-sans-thai-thai-600-normal.woff");
const notoSansThaiBold = path.join(notoSansThaiFiles, "noto-sans-thai-thai-700-normal.woff");
function escapeXml(value: string) {
return value.replace(/[&<>"']/g, (char) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&apos;" })[char]!);
@@ -25,8 +30,22 @@ export async function addArticleHeadline(image: Buffer, headline: string, accent
<defs><linearGradient id="shade" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#101820" stop-opacity="0"/><stop offset="0.28" stop-color="#101820" stop-opacity="0.82"/><stop offset="1" stop-color="#101820" stop-opacity="0.97"/></linearGradient></defs>
<rect x="0" y="500" width="1024" height="524" fill="url(#shade)"/>
<rect x="64" y="590" width="112" height="9" rx="4" fill="${escapeXml(accent)}"/>
<text x="64" y="650" fill="#FFFFFF" font-family="Noto Sans Thai, sans-serif" font-size="29" font-weight="600">AI UPDATE</text>
${lines.map((line, index) => `<text x="64" y="${735 + index * 88}" fill="#FFFFFF" font-family="Noto Sans Thai, sans-serif" font-size="64" font-weight="700">${escapeXml(line)}</text>`).join("")}
</svg>`;
return sharp(image, { limitInputPixels: 40_000_000 }).resize(1024, 1024, { fit: "cover" }).composite([{ input: Buffer.from(svg) }]).jpeg({ quality: 90 }).toBuffer();
const label = await sharp({
text: { text: "AI UPDATE", font: "Noto Sans Thai SemiBold 29px", fontfile: notoSansThaiSemiBold, rgba: true },
}).tint("#FFFFFF").png().toBuffer();
const headlineLayers = await Promise.all(lines.map((line) => sharp({
text: { text: escapeXml(line), font: "Noto Sans Thai Bold 64px", fontfile: notoSansThaiBold, rgba: true },
}).tint("#FFFFFF").png().toBuffer()));
return sharp(image, { limitInputPixels: 40_000_000 })
.resize(1024, 1024, { fit: "cover" })
.composite([
{ input: Buffer.from(svg) },
{ input: label, left: 64, top: 620 },
...headlineLayers.map((input, index) => ({ input, left: 64, top: 680 + index * 88 })),
])
.jpeg({ quality: 90 })
.toBuffer();
}