Skip to content

Commit

Permalink
fix: autogen icons based on last segments (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpreston321 committed Mar 7, 2024
1 parent 6543833 commit 9ddaa14
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions app/server/plugins/content.ts
@@ -1,13 +1,13 @@
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('content:file:afterParse' as any, async (file) => {
nitroApp.hooks.hook('content:file:afterParse' as any, (file) => {
// Filter out non-markdown files
if (!file._id.endsWith('.md')) {
return
}

// Set the icon for the file if it is not already set
if (!file.icon) {
file.icon = await resolveIcon(file._path)
file.icon = resolveIcon(file._path)
}

// Remove first h1 from markdown files as it is added to front-matter as title
Expand Down Expand Up @@ -85,9 +85,15 @@ const commonIcons = [
]

function resolveIcon(path: string) {
for (const icon of commonIcons) {
if (path.includes(icon.pattern)) {
return icon.icon
// Split the path into parts and reverse it
const paths = path.slice(1).split('/').reverse()

// Search for icons in reverse order
for (const p of paths) {
for (const icon of commonIcons) {
if (p.includes(icon.pattern)) {
return icon.icon
}
}
}
}

0 comments on commit 9ddaa14

Please sign in to comment.