mirror of
https://github.com/hanzoai/ui.git
synced 2026-11-21 00:18:41 +00:00
The site drew its fences, example sources and module types through @hanzogui/code-to-html (refractor) and coloured the tokens from its own stylesheet. code.ts now highlights them at build with shiki in codeTheme from @hanzo/ui/core, Dracula on dark and GitHub Light on light, the same theme docs.hanzo.ai, hanzo.ai and platform draw. Tokens and the block's ground are written as light-dark(), so the scheme on <html> picks the palette; the .code token rules and code-to-html are gone, and nothing highlights in the browser. The CodeBlock example names codeTheme.dark rather than github-dark. The navigation menu example stops importing navigationMenuTriggerStyle, which 8.27.45 removed and which failed the site build.
36 lines
1.4 KiB
TypeScript
36 lines
1.4 KiB
TypeScript
/**
|
|
* The site's highlighter: shiki, in the code theme every Hanzo surface draws
|
|
* (`codeTheme` — Dracula on dark, GitHub Light on light).
|
|
*
|
|
* Server only, like the catalog and the docs compiler that call it: a page is
|
|
* rendered to HTML at build time and ships no highlighter. Each token is written
|
|
* as `color: light-dark(<light>, <dark>)` and the block's ground the same way,
|
|
* so the page's `color-scheme` — which the scheme provider keeps on <html> —
|
|
* picks the palette, and no stylesheet knows which theme is on.
|
|
*/
|
|
import { codeTheme } from '@hanzo/ui/core'
|
|
import { createHighlighter } from 'shiki'
|
|
|
|
/** The grammars the samples are written in; anything else is drawn as text. */
|
|
const LANGS = ['tsx', 'ts', 'js', 'jsx', 'bash', 'sh', 'css', 'json', 'html', 'yaml', 'diff']
|
|
|
|
const ready = createHighlighter({ themes: [codeTheme.dark, codeTheme.light], langs: LANGS })
|
|
|
|
/** A `pre.code` of `text` in `lang`, as HTML. Resolves once, then highlights synchronously. */
|
|
export async function highlighter(): Promise<(text: string, lang: string) => string> {
|
|
const shiki = await ready
|
|
return (text, lang) =>
|
|
shiki.codeToHtml(text, {
|
|
lang: LANGS.includes(lang) ? lang : 'text',
|
|
themes: codeTheme,
|
|
defaultColor: 'light-dark()',
|
|
transformers: [
|
|
{
|
|
pre(node) {
|
|
this.addClassToHast(node, 'code')
|
|
},
|
|
},
|
|
],
|
|
})
|
|
}
|