pengzhanbo 49672724eb
feat(theme)!: migrate plugin-md-enhance to official plugins (#206)
* feat(theme)!: migrate `plugin-markdown-hint`

* chore: tweak

* chore: tweak

* chore: tweak

* chore: tweak

* chore: tweak

* chore: tweak

* chore: tweak

* chore: tweak

* chore: tweak

* chore: tweak

* fix(theme): improve `home-blog` styles in mobile, close #210

* chore: tweak

* chore: tweak
2024-09-26 00:06:23 +08:00

49 lines
1.4 KiB
TypeScript

import { defaultFile, defaultFolder, definitions } from './definitions.js'
export function getFileIcon(fileName: string, type: 'file' | 'folder' = 'file'): string {
const name = getFileIconName(fileName, type)
if (!name)
return type === 'file' ? defaultFile : defaultFolder
return name
}
export function getFileIconName(fileName: string, type: 'file' | 'folder' = 'file'): string | undefined {
if (type === 'folder') {
const icon = definitions.folders[fileName]
if (icon)
return icon
if (fileName.includes('/'))
return definitions.folders[fileName.slice(fileName.lastIndexOf('/') + 1)]
return
}
let icon: string | undefined = definitions.named[fileName] || definitions.files[fileName]
if (icon)
return icon
icon = getFileIconTypeFromExtension(fileName)
if (icon)
return icon
for (const [partial, partialIcon] of Object.entries(definitions.partials)) {
if (fileName.includes(partial))
return partialIcon
}
return icon
}
function getFileIconTypeFromExtension(fileName: string): string | undefined {
const firstDotIndex = fileName.indexOf('.')
if (firstDotIndex === -1)
return
let extension = fileName.slice(firstDotIndex)
while (extension !== '') {
const icon = definitions.extensions[extension]
if (icon)
return icon
const nextDotIndex = extension.indexOf('.', 1)
if (nextDotIndex === -1)
return
extension = extension.slice(nextDotIndex)
}
return undefined
}