From 133242aaeb5680ecd2cf90cca600593fc0c2136e Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Fri, 20 Jun 2025 13:26:18 -0700 Subject: [PATCH] add methods to generate blank svgs and svgs using satori --- core/syntaxHighlighting/SyntaxHighlighter.ts | 103 ++++++++++++++++++- 1 file changed, 100 insertions(+), 3 deletions(-) diff --git a/core/syntaxHighlighting/SyntaxHighlighter.ts b/core/syntaxHighlighting/SyntaxHighlighter.ts index c0886f078..d826984f7 100644 --- a/core/syntaxHighlighting/SyntaxHighlighter.ts +++ b/core/syntaxHighlighting/SyntaxHighlighter.ts @@ -1,6 +1,7 @@ import * as fs from "fs/promises"; import { JSDOM } from "jsdom"; import path from "path"; +import satori from "satori"; // import puppeteer, { Browser, ScreenshotOptions } from "puppeteer"; import { BundledTheme, codeToHtml } from "shiki"; import { kebabOfStr } from "../util/text"; @@ -188,6 +189,37 @@ export class SyntaxHighlighter { // return { data: screenshot, dimensions: dims }; // } + async convertToBlankSVG( + dimensions: Dimensions, + options: ConversionOptions, + ): Promise { + // const svg = ` + // ${guts} + // `; + + // const tempPage = await this.browser.newPage(); + // await tempPage.setContent(svg); + + // const dimensions = await tempPage.evaluate(() => { + // const svg = document.querySelector("svg"); + // if (!svg) return { width: 0, height: 0 }; + // const bbox = svg.getBBox(); + // return { + // width: Math.ceil(bbox.width), + // height: Math.ceil(bbox.height), + // }; + // }); + + // await tempPage.close(); + + const svg = ` + TEST TEXT THIS IS +`; + + console.log(svg); + return Buffer.from(svg, "utf8"); + } + async convertToSVG( code: string, language: string = "javascript", @@ -198,8 +230,8 @@ export class SyntaxHighlighter { ): Promise { const highlightedCodeHtml = await this.highlightCode(code, language); - console.log(code); - console.log(highlightedCodeHtml); + // console.log(code); + // console.log(highlightedCodeHtml); const guts = this.convertShikiHtmlToSvgGut( highlightedCodeHtml, @@ -227,13 +259,57 @@ export class SyntaxHighlighter { // await tempPage.close(); const svg = ` - ${guts} + ${guts} `; console.log(svg); return Buffer.from(svg, "utf8"); } + async convertToSVGWithSatori( + code: string, + language: string = "javascript", + fontSize: number, + dimensions: Dimensions, + lineHeight: number, + options: ConversionOptions, + ): Promise { + const highlightedCodeHtml = await this.highlightCode(code, language); + + console.log(code); + console.log(highlightedCodeHtml); + + // Convert to SVG using Satori + const fontData = await this.loadFont(); + const svg = await satori(highlightedCodeHtml, { + width: dimensions.width, // You'll need to determine appropriate width + height: dimensions.height, // Dynamic height based on content + fonts: [ + { + name: "YourMonospaceFontName", + data: fontData, + style: "normal", + }, + ], + }); + + console.log(svg); + return Buffer.from(svg, "utf8"); + } + + // Function to load your font + async loadFont(): Promise { + // Option 1: If running in Node.js, you can load from the filesystem + console.log(process.cwd()); + return Buffer.from( + await fs.readFile( + "/home/jacob/continue/continue/fonts/Cascadia_Mono/static/CascadiaMono-Regular.ttf", + "binary", + ), + "binary", + ); + } + convertShikiHtmlToSvgGut( shikiHtml: string, fontSize: number, @@ -318,6 +394,27 @@ export class SyntaxHighlighter { } } + async getBlankDataUri( + dimensions: Dimensions, + options: ConversionOptions, + ): Promise { + switch (options.imageType) { + // case "png": + // const pngBuffer = await this.convertToPNG( + // code, + // language, + // fontSize, + // dimensions, + // lineHeight, + // options, + // ); + // return `data:image/png;base64,${pngBuffer.data.toString("base64")}`; + case "svg": + const svgBuffer = await this.convertToBlankSVG(dimensions, options); + return `data:image/svg+xml;base64,${svgBuffer.toString("base64")}`; + } + } + // Utility method to list available themes async getAvailableThemes(): Promise { try {