Home / File/ highlight-code.ts — ui Source File

highlight-code.ts — ui Source File

Architecture documentation for highlight-code.ts, a typescript file in the ui codebase. 3 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  15d6f42b_3c16_b1d3_bb31_4653368e3f83["highlight-code.ts"]
  8eb629f2_502c_b1a4_6fec_b6329f097fbe["crypto"]
  15d6f42b_3c16_b1d3_bb31_4653368e3f83 --> 8eb629f2_502c_b1a4_6fec_b6329f097fbe
  e75b0e1f_129c_d7e6_5b85_ee0ca841109b["lru-cache"]
  15d6f42b_3c16_b1d3_bb31_4653368e3f83 --> e75b0e1f_129c_d7e6_5b85_ee0ca841109b
  8af2f07a_2b3a_c980_6d52_d7f80a9ee5a4["shiki"]
  15d6f42b_3c16_b1d3_bb31_4653368e3f83 --> 8af2f07a_2b3a_c980_6d52_d7f80a9ee5a4
  style 15d6f42b_3c16_b1d3_bb31_4653368e3f83 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { createHash } from "crypto"
import { LRUCache } from "lru-cache"
import { codeToHtml } from "shiki"
import type { ShikiTransformer } from "shiki"

// LRU cache for cross-request caching of highlighted code.
// Shiki highlighting is CPU-intensive and deterministic, so caching is safe.
const highlightCache = new LRUCache<string, string>({
  max: 500,
  ttl: 1000 * 60 * 60, // 1 hour.
})

export const transformers = [
  {
    code(node) {
      if (node.tagName === "code") {
        const raw = this.source
        node.properties["__raw__"] = raw

        if (raw.startsWith("npm install")) {
          node.properties["__npm__"] = raw
          node.properties["__yarn__"] = raw.replace("npm install", "yarn add")
          node.properties["__pnpm__"] = raw.replace("npm install", "pnpm add")
          node.properties["__bun__"] = raw.replace("npm install", "bun add")
        }

        if (raw.startsWith("npx create-")) {
          node.properties["__npm__"] = raw
          node.properties["__yarn__"] = raw.replace(
            "npx create-",
            "yarn create "
          )
          node.properties["__pnpm__"] = raw.replace(
            "npx create-",
            "pnpm create "
          )
          node.properties["__bun__"] = raw.replace("npx", "bunx --bun")
        }

        // npm create.
        if (raw.startsWith("npm create")) {
          node.properties["__npm__"] = raw
          node.properties["__yarn__"] = raw.replace("npm create", "yarn create")
          node.properties["__pnpm__"] = raw.replace("npm create", "pnpm create")
          node.properties["__bun__"] = raw.replace("npm create", "bun create")
        }

        // npx.
        if (raw.startsWith("npx")) {
          node.properties["__npm__"] = raw
          node.properties["__yarn__"] = raw.replace("npx", "yarn")
          node.properties["__pnpm__"] = raw.replace("npx", "pnpm dlx")
          node.properties["__bun__"] = raw.replace("npx", "bunx --bun")
        }

        // npm run.
        if (raw.startsWith("npm run")) {
          node.properties["__npm__"] = raw
          node.properties["__yarn__"] = raw.replace("npm run", "yarn")
          node.properties["__pnpm__"] = raw.replace("npm run", "pnpm")
          node.properties["__bun__"] = raw.replace("npm run", "bun")
        }
      }
    },
  },
] as ShikiTransformer[]

export async function highlightCode(code: string, language: string = "tsx") {
  // Create cache key from code content and language.
  const cacheKey = createHash("sha256")
    .update(`${language}:${code}`)
    .digest("hex")

  // Check cache first.
  const cached = highlightCache.get(cacheKey)
  if (cached) {
    return cached
  }

  const html = await codeToHtml(code, {
    lang: language,
    themes: {
      dark: "github-dark",
      light: "github-light",
    },
    transformers: [
      {
        pre(node) {
          node.properties["class"] =
            "no-scrollbar min-w-0 overflow-x-auto overflow-y-auto overscroll-x-contain overscroll-y-auto px-4 py-3.5 outline-none has-[[data-highlighted-line]]:px-0 has-[[data-line-numbers]]:px-0 has-[[data-slot=tabs]]:p-0 !bg-transparent"
        },
        code(node) {
          node.properties["data-line-numbers"] = ""
        },
        line(node) {
          node.properties["data-line"] = ""
        },
      },
    ],
  })

  // Cache the result.
  highlightCache.set(cacheKey, html)

  return html
}

Subdomains

Functions

Dependencies

  • crypto
  • lru-cache
  • shiki

Frequently Asked Questions

What does highlight-code.ts do?
highlight-code.ts is a source file in the ui codebase, written in typescript. It belongs to the DocumentationAtlas domain, Changelog subdomain.
What functions are defined in highlight-code.ts?
highlight-code.ts defines 1 function(s): highlightCode.
What does highlight-code.ts depend on?
highlight-code.ts imports 3 module(s): crypto, lru-cache, shiki.
Where is highlight-code.ts in the architecture?
highlight-code.ts is located at apps/v4/lib/highlight-code.ts (domain: DocumentationAtlas, subdomain: Changelog, directory: apps/v4/lib).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free