koishi
koishi: render Open Graph (og:image) SVG images from HTML strings and
Lustre element trees, using the npm packages
@comamoca/komeiji
and satori.
Install the npm packages before use (see README):
npm install @comamoca/komeiji satori
Types
A font for satori rendering.
data is the raw font file bytes. satori accepts TTF, OTF, and WOFF
(not WOFF2 — use .woff fixtures such as @fontsource’s .woff files).
pub type Font {
Font(
name: String,
data: BitArray,
weight: Int,
style: FontStyle,
)
}
Constructors
-
Font(name: String, data: BitArray, weight: Int, style: FontStyle)
Error returned when SVG to PNG rendering fails.
pub type PngError {
PngError(message: String)
}
Constructors
-
PngError(message: String)
Options for rendering an SVG to PNG with resvg.
Set width or height to a positive value to scale the output. If both
are zero, the SVG’s intrinsic size is used. background accepts any CSS
color string; use an empty string to keep the default (transparent).
pub type PngOptions {
PngOptions(width: Int, height: Int, background: String)
}
Constructors
-
PngOptions(width: Int, height: Int, background: String)
pub type SatoriError {
SatoriError(message: String)
}
Constructors
-
SatoriError(message: String)
Values
pub fn to_png(
svg: String,
options: PngOptions,
) -> promise.Promise(Result(BitArray, PngError))
Render an SVG string to a PNG image as a BitArray.
Uses the npm package @resvg/resvg-js (Rust resvg bindings).
koishi.to_png(svg, koishi.PngOptions(width: 1200, height: 0, background: ""))
// -> Promise(Result(BitArray, PngError))
pub fn to_svg(
html: String,
options: Options,
) -> promise.Promise(Result(String, SatoriError))
Render an HTML string to an SVG string.
Internally: @comamoca/komeiji’s html() parses the HTML into a
satori-friendly VNode, then satori() renders it to SVG.
koishi.to_svg("<div style=\"color:black;\">hi</div>", options)
// -> Promise(Result(String, SatoriError))