You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
k0r.386/.vitepress/theme/useFiglet.ts

32 lines
827 B
TypeScript

import { FLFParser, FontLayoutManager } from '@figlet-ts/lib'
import * as fonts from '@figlet-ts/fonts/dist/fonts'
function findFont(needle: string) {
needle = needle.toLowerCase()
for (let categoryName in fonts) {
const category = fonts[categoryName]
for (let name in category) {
if (name.toLowerCase() === needle) return category[name]
}
}
}
function getFont(name: string) {
const font = findFont(name)
if (!font) throw new Error(`Cannot find font "${name}"!`)
const flf = FLFParser.parse(atob(font._contents))
return flf.font
}
export default function useFiglet() {
const flm = new FontLayoutManager()
function render(text: string, fontName: string) {
const figFont = getFont(fontName)
const output = flm.renderText(text, figFont)
return output
}
return { render }
}