prerender-server.ts
This file
Path
apps/web/src/features/code/prerender-server.ts
132 lines · 4.0 KiB · TypeScript
Last changed by
Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
History of this fileTake it
Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
line 1why, one step back1/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 2–5why, one step back2 * Shiki runs on its JavaScript engine (a Worker compiles no WebAssembly), and3 * the Worker's build resolves `shiki` to core alone (`vite.config.ts`), so only4 * the grammars named here are bundled. Pierre will not resolve a grammar or a5 * theme by name in a Worker's global scope, so each is attached to itsMerge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 6–103why, one step back6 * highlighter directly, once per isolate.7 */8import {9 attachResolvedLanguages,10 attachResolvedThemes,11 getSharedHighlighter,12} from "@pierre/diffs";13import type {14 FileContents,15 FileDiffMetadata,16 LanguageRegistration,17} from "@pierre/diffs";18import { preloadDiffHTML, preloadFile } from "@pierre/diffs/ssr";19import { Effect, Option } from "effect";20import { normalizeTheme } from "shiki/core";2122import type { DrawnFile } from "./diff-model.ts";23import {24 codeTheme,25 diffOptionsFor,26 diffsOptions,27 fileOptions,28} from "./pierre.ts";29import { serverLanguageOf } from "./prerender.ts";30import type { ServerLanguage } from "./prerender.ts";3132type Grammar = () => Promise<{ readonly default: LanguageRegistration[] }>;3334const GRAMMARS: Record<ServerLanguage, Grammar> = {35 css: () => import("shiki/langs/css.mjs"),36 html: () => import("shiki/langs/html.mjs"),37 javascript: () => import("shiki/langs/javascript.mjs"),38 json: () => import("shiki/langs/json.mjs"),39 jsx: () => import("shiki/langs/jsx.mjs"),40 markdown: () => import("shiki/langs/markdown.mjs"),41 toml: () => import("shiki/langs/toml.mjs"),42 tsx: () => import("shiki/langs/tsx.mjs"),43 typescript: () => import("shiki/langs/typescript.mjs"),44 yaml: () => import("shiki/langs/yaml.mjs"),45 yml: () => import("shiki/langs/yaml.mjs"),46 zsh: () => import("shiki/langs/shellscript.mjs"),47};4849const highlighterFor = (language: ServerLanguage) =>50 Effect.gen(function* () {51 const highlighter = yield* Effect.tryPromise(() =>52 getSharedHighlighter({53 langs: [],54 preferredHighlighter: "shiki-js",55 themes: [],56 })57 );5859 const grammar = yield* Effect.tryPromise(GRAMMARS[language]);6061 yield* Effect.try(() => {62 attachResolvedThemes(normalizeTheme(codeTheme()), highlighter);63 attachResolvedLanguages(64 { data: grammar.default, name: language },65 highlighter66 );67 });68 });6970const draw = <E1, E2>(71 language: Option.Option<ServerLanguage>,72 highlighted: Effect.Effect<string, E1>,73 plain: Effect.Effect<string, E2>74): Effect.Effect<string | undefined> =>75 Option.match(language, {76 onNone: () => plain,77 onSome: (name) =>78 highlighterFor(name).pipe(79 Effect.andThen(highlighted),80 Effect.catchCause(() => plain)81 ),82 }).pipe(Effect.orElseSucceed((): string | undefined => undefined));8384const fileHtml = (file: FileContents) =>85 Effect.tryPromise(() => preloadFile({ file, options: fileOptions })).pipe(86 Effect.map(({ prerenderedHTML }) => prerenderedHTML)87 );8889export const prerenderFile = (file: {90 readonly path: string;91 readonly sha: string;92 readonly text: string;93}) => {94 const contents = { cacheKey: file.sha, contents: file.text, name: file.path };9596 return draw(97 serverLanguageOf(file.path),98 fileHtml(contents),99 fileHtml({ ...contents, lang: "text" })100 );101};102103/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 104–106why, one step back104 * Pierre drops the code's top gap only after a header, and takes a scrollbar's105 * width from the bottom gap only once its script has measured one; this keeps a106 * headerless body where the browser's drawing puts it.Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 107–132why, one step back107 */108const UNDER_HEADER_CSS =109 "[data-code] { padding-top: 0; padding-bottom: var(--diffs-gap-block, var(--diffs-gap-fallback)); }";110111const diffHtml = (fileDiff: FileDiffMetadata) =>112 Effect.tryPromise(() =>113 preloadDiffHTML({114 fileDiff,115 options: {116 ...diffOptionsFor(false),117 disableFileHeader: true,118 unsafeCSS: `${diffsOptions.unsafeCSS}\n${UNDER_HEADER_CSS}`,119 },120 })121 );122123export const prerenderDiff = (file: DrawnFile) =>124 Option.match(file.diff, {125 onNone: () => Effect.sync((): string | undefined => undefined),126 onSome: (diff) =>127 draw(128 serverLanguageOf(file.file.path),129 diffHtml(diff),130 diffHtml({ ...diff, lang: "text" })131 ),132 });