Files
shadcn-ui/apps/v4/lib/highlight-code.ts
shadcn 1aa35048a5 feat: v4 updates (#7499)
* feat(v4): update home page

* fix

* fix: cards

* feat(v4): charts page

* feat: update pages

* feat: colors

* fix

* feat: add docs

* feat: mdx work

* fix

* fix

* fix: sidebar

* fix: lint

* feat: updates

* feat: update components

* feat: fix docs

* fix: responsive

* feat: implement cmdk

* fix: update navigation menu demo

* fix: code style

* fix: themes

* feat: implement blocks page

* fix: docs config

* refactor

* fix: outputFileTracingIncludes

* fix

* fix: output

* fix

* fix: registry

* refactor: move docs

* debug: docs

* debug

* revert

* fix: mjs

* deps: pin fumadocs

* debug

* fix: downgrade next

* fix: index page

* refactor: move mdx components

* fix: remove copy button

* fix

* was it zod

* yes it was

* remove copy page

* fix: color page

* fix: colors page

* fix: meta colors

* fix: copy button

* feat: sync registry

* fix: registry build script

* feat: update port

* feat: clean up examples

* fix

* fix: mobile nav

* fix: blur for mobile

* fix: sidebar nav

* feat: update examples

* fix: build scripts

* feat: update components

* feat: restyle

* fix: types

* fix: styles

* fix: margins

* fix: screenshots

* fix

* feat: update theme

* fix: charts nav

* fix: image

* feat: optimize images

* fix: menu

* fix: card

* fix: border

* check

* feat: implement charts page

* fix: charts

* fix: og images

* feat: extend touch

* fix: static

* fix: sizing

* fix: mobile screenshots

* fix: page nav

* fix

* feat: update favicon

* fix: theme selector

* fix: feedback

* fix: sink

* docs: update

* fix: styles

* chore: update registry

* fix: command

* fix

* fix: minor updates

* fix: typography on smaller devices

* fix: format

* fix: remove unused icon

* feat: update favicon

* fix: typography

* docs: typography page

* fix: steps
2025-05-30 11:35:16 +04:00

84 lines
2.7 KiB
TypeScript

import { codeToHtml } from "shiki"
import type { ShikiTransformer } from "shiki"
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") {
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 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"] = ""
},
},
],
})
return html
}