feat: default figlet font config

Norman Köhring 5 months ago
parent 12678fe115
commit 544daa6f5d

@ -22,6 +22,7 @@ export default defineConfigWithTheme<ThemeConfig>({
['meta', { content: "width=device-width,initial-scale=1.0", name: "viewport" }], ['meta', { content: "width=device-width,initial-scale=1.0", name: "viewport" }],
], ],
themeConfig: { themeConfig: {
defaultHeaderFont: 'basic',
commands: [{ commands: [{
command: 'about', command: 'about',
aliases: ['info'], aliases: ['info'],

@ -12,5 +12,6 @@ export type SimpleCommand = {
} }
export interface ThemeConfig { export interface ThemeConfig {
defaultHeaderFont: string
commands: SimpleCommand[] commands: SimpleCommand[]
} }

@ -16,13 +16,7 @@ const prompt = '\n$> '
const textArea = ref<HTMLTextAreaElement | null>(null) const textArea = ref<HTMLTextAreaElement | null>(null)
const footer = ref([]) const footer = ref([])
const figlet = useFiglet() const figlet = useFiglet(site.value.themeConfig.defaultHeaderFont)
function getHeaderArt(header: string, font: string) {
// Why is that so simple to approximate? Pretty sure, there is a mistake somewhere...
const maxWidth = Math.round(textArea.value?.getBoundingClientRect().width / 10) - 2
return figlet.render(header, font, maxWidth)
}
function parsedContent(src: string) { function parsedContent(src: string) {
const pieces = src.split('---').map(s => s.trim()) const pieces = src.split('---').map(s => s.trim())
@ -44,13 +38,13 @@ function getCurrentPage(title: string) {
console.error('☠️ current page not found in the list. This should never happen.') console.error('☠️ current page not found in the list. This should never happen.')
return { return {
title: 'not_found', title: 'not_found',
headerArt: getHeaderArt('404', 'chunky'), headerArt: figlet.render('404'),
content: 'The page could not be found.', content: 'The page could not be found.',
uris: [], uris: [],
} }
} }
const { header, headerFont, uris } = page.frontmatter const { header, headerFont, uris } = page.frontmatter
const headerArt = getHeaderArt(header, headerFont ?? 'chunky') const headerArt = figlet.render(header, headerFont)
const content = parsedContent(page.src) const content = parsedContent(page.src)
return { title, headerArt, content, uris: uris ?? [] } return { title, headerArt, content, uris: uris ?? [] }
@ -62,6 +56,7 @@ onMounted(() => {
return return
} }
figlet.setInputElement(textArea.value)
const { addText, addLine, clear, footerLinks, setFooter } = useTerminal(textArea.value, commands.value, pages) const { addText, addLine, clear, footerLinks, setFooter } = useTerminal(textArea.value, commands.value, pages)
watch(page, () => { watch(page, () => {

@ -2,6 +2,8 @@ import { FLFParser, FontLayoutManager } from '@figlet-ts/lib'
import * as fonts from '@figlet-ts/fonts/dist/fonts' import * as fonts from '@figlet-ts/fonts/dist/fonts'
function findFont(needle: string) { function findFont(needle: string) {
if (!needle) return
needle = needle.toLowerCase() needle = needle.toLowerCase()
for (let categoryName in fonts) { for (let categoryName in fonts) {
const category = fonts[categoryName] const category = fonts[categoryName]
@ -9,24 +11,37 @@ function findFont(needle: string) {
if (name.toLowerCase() === needle) return category[name] if (name.toLowerCase() === needle) return category[name]
} }
} }
console.error(`Cannot find font "${needle}"!`)
} }
function getFont(name: string) { function getFont(name: string, fallback: string) {
const font = findFont(name) const font = findFont(name) ?? findFont(fallback) ?? fonts.Core.standard
if (!font) throw new Error(`Cannot find font "${name}"!`)
const flf = FLFParser.parse(atob(font._contents)) const flf = FLFParser.parse(atob(font._contents))
return flf.font return flf.font
} }
export default function useFiglet() { export default function useFiglet(defaultFontName = 'standard', htmlElement?: HTMLElement) {
const flm = new FontLayoutManager() const flm = new FontLayoutManager()
function render(text: string, fontName: string, maxWidth: number) { let inputElement: HTMLElement | null = null
flm.width.set(maxWidth)
const figFont = getFont(fontName) function calcMaxWidth() {
// 1150px default width / 10px per char - 2 char padding
if (inputElement === null) return 113
const elWidth = inputElement.getBoundingClientRect().width
return Math.round(elWidth / 10) - 2
}
function setInputElement(el: HTMLElement) {
inputElement = el
}
function render(text: string, fontName?: string) {
flm.width.set(calcMaxWidth())
const figFont = getFont(fontName, defaultFontName)
const output = flm.renderText(text, figFont) const output = flm.renderText(text, figFont)
return output return output
} }
return { render } return { render, setInputElement }
} }

@ -0,0 +1 @@
index.md

@ -1,7 +1,6 @@
--- ---
title: 'resume' title: 'resume'
header: 'Resume' header: 'Resume'
headerFont: 'Broadway'
uris: [ uris: [
{ label: 'CodeGaia', uri: 'https://codegaia.io/' }, { label: 'CodeGaia', uri: 'https://codegaia.io/' },
{ label: 'Coursedog', uri: 'https://coursedog.com/' }, { label: 'Coursedog', uri: 'https://coursedog.com/' },

Loading…
Cancel
Save