49 lines
1.9 KiB
JavaScript
49 lines
1.9 KiB
JavaScript
import fs from 'node:fs'
|
|||
|
|
import path from 'node:path'
|
||
|
|
|
||
|
|
const webUiDir = path.resolve(import.meta.dirname, '..')
|
||
|
|
|
||
|
|
/** @typedef {{ name: string, icon: string }} ProjectIconConfig */
|
||
|
|
/** @typedef {[string, Record<string, string | number>]} IconNode */
|
||
|
|
|
||
|
|
/** @type {Record<string, ProjectIconConfig>} */
|
||
|
|
const projectIcons = JSON.parse(fs.readFileSync(path.join(webUiDir, 'project-icons.json'), 'utf8'))
|
||
|
|
const lucideIconsDir = path.join(webUiDir, 'node_modules', '@lucide', 'svelte', 'dist', 'icons')
|
||
|
|
|
||
|
|
/** @param {unknown} value */
|
||
|
|
function escapeAttribute(value) {
|
||
|
|
return String(value)
|
||
|
|
.replaceAll('&', '&')
|
||
|
|
.replaceAll('"', '"')
|
||
|
|
.replaceAll('<', '<')
|
||
|
|
.replaceAll('>', '>')
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @param {string} iconName @returns {IconNode[]} */
|
||
|
|
function getIconNode(iconName) {
|
||
|
|
const source = fs.readFileSync(path.join(lucideIconsDir, `${iconName}.svelte`), 'utf8')
|
||
|
|
const match = source.match(/const iconNode = (\[[\s\S]*?\]);/)
|
||
|
|
if (!match) throw new Error(`Unable to read Lucide icon node for ${iconName}`)
|
||
|
|
return /** @type {IconNode[]} */ (JSON.parse(match[1]))
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @param {IconNode} node */
|
||
|
|
function renderIconNode([tag, attributes]) {
|
||
|
|
const renderedAttributes = Object.entries(attributes)
|
||
|
|
.map(([name, value]) => `${name}="${escapeAttribute(value)}"`)
|
||
|
|
.join(' ')
|
||
|
|
return ` <${tag} ${renderedAttributes} />`
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @param {string} appId */
|
||
|
|
export function renderProjectFavicon(appId) {
|
||
|
|
const project = projectIcons[appId]
|
||
|
|
if (!project) throw new Error(`Unknown MediaCollection project: ${appId}`)
|
||
|
|
|
||
|
|
const iconNode = getIconNode(project.icon)
|
||
|
|
return `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#5b8cff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" role="img" aria-labelledby="favicon-title">
|
||
|
|
<title id="favicon-title">${escapeAttribute(project.name)}</title>
|
||
|
|
${iconNode.map(renderIconNode).join('\n')}
|
||
|
|
</svg>
|
||
|
|
`
|
||
|
|
}
|