mirror of
https://github.com/tabler/tabler.git
synced 2026-08-07 03:42:27 +04:00
575d68572f
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
// Docs code-block pipeline: js-beautify + shiki (github-dark theme).
|
|
import beautify from 'js-beautify'
|
|
import { createHighlighter, type Highlighter } from 'shiki'
|
|
|
|
let highlighterPromise: Promise<Highlighter> | undefined
|
|
|
|
function getHighlighter(): Promise<Highlighter> {
|
|
highlighterPromise ??= createHighlighter({
|
|
themes: ['github-dark'],
|
|
langs: ['html', 'yaml', 'js', 'ts', 'shell', 'diff', 'scss', 'css'],
|
|
})
|
|
return highlighterPromise
|
|
}
|
|
|
|
export function beautifyHtml(code: string): string {
|
|
return beautify.html(code, {
|
|
indent_size: 2,
|
|
wrap_line_length: 80,
|
|
})
|
|
}
|
|
|
|
export async function highlightCode(code: string, lang = 'html'): Promise<string> {
|
|
const highlighter = await getHighlighter()
|
|
return highlighter.codeToHtml(code, { lang, theme: 'github-dark' })
|
|
}
|
|
|
|
/** Escape text for use in HTML attributes (e.g. data-clipboard-text). */
|
|
export function escapeAttribute(text: string): string {
|
|
return text
|
|
.replace(/&/g, '&')
|
|
.replace(/'/g, ''')
|
|
.replace(/"/g, '"')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/\r\n/g, ' ')
|
|
.replace(/[\r\n]/g, ' ')
|
|
}
|
|
|
|
/** Replace href="#" with javascript:void(0) in example markup. */
|
|
export function removeHref(content: string): string {
|
|
return content.replace(/href="#"/g, 'href="javascript:void(0)"')
|
|
}
|