Skip to content

Commit

Permalink
fix: social thumbnail assistants (huggingface#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarrazin authored Jan 24, 2024
1 parent 05289fc commit 70a1ace
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "*.ttf" {
const value: ArrayBuffer;
export default value;
}
10 changes: 6 additions & 4 deletions src/routes/assistant/[assistantId]/thumbnail.png/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import type { SvelteComponent } from "svelte";
import { Resvg } from "@resvg/resvg-js";
import satori from "satori";
import { html } from "satori-html";
import { base } from "$app/paths";

export const GET: RequestHandler = (async ({ url, params, fetch }) => {
import InterRegular from "../../../../../static/fonts/Inter-Regular.ttf";
import InterBold from "../../../../../static/fonts/Inter-Bold.ttf";

export const GET: RequestHandler = (async ({ url, params }) => {
const assistant = await collections.assistants.findOne({
_id: new ObjectId(params.assistantId),
});
Expand Down Expand Up @@ -39,12 +41,12 @@ export const GET: RequestHandler = (async ({ url, params, fetch }) => {
fonts: [
{
name: "Inter",
data: await fetch(base + "/fonts/Inter-Regular.ttf").then((r) => r.arrayBuffer()),
data: InterRegular as unknown as ArrayBuffer,
weight: 500,
},
{
name: "Inter",
data: await fetch(base + "/fonts/Inter-Bold.ttf").then((r) => r.arrayBuffer()),
data: InterBold as unknown as ArrayBuffer,
weight: 700,
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
<img class="mr-1.5 h-8 w-8" src={imgUrl} alt="app logo" />
AI assistant
</p>
<h1 class="m-0 {name.length < 38 ? 'text-5xl' : 'text-4xl'} text-balance font-black">
<h1 class="m-0 {name.length < 38 ? 'text-5xl' : 'text-4xl'} font-black">
{name}
</h1>
<p class="mb-8 text-pretty text-2xl">
<p class="mb-8 text-2xl">
{description.slice(0, 160)}
{#if description.length > 160}...{/if}
</p>
Expand Down
18 changes: 17 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";
import { defineConfig, type PluginOption } from "vite";
import Icons from "unplugin-icons/vite";
import { promises } from "fs";

// used to load fonts server side for thumbnail generation
function loadTTFAsArrayBuffer(): PluginOption {
return {
name: "load-ttf-as-array-buffer",
async transform(_src, id) {
if (id.endsWith(".ttf")) {
return `export default new Uint8Array([
${new Uint8Array(await promises.readFile(id))}
]).buffer`;
}
},
};
}

export default defineConfig({
plugins: [
sveltekit(),
Icons({
compiler: "svelte",
}),
loadTTFAsArrayBuffer(),
],
optimizeDeps: {
include: ["browser-image-resizer"],
Expand Down

0 comments on commit 70a1ace

Please sign in to comment.