Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: autogen icons based on last segments #93

Merged
merged 1 commit into from Mar 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally kept it async to prepare for your external resolver πŸ˜…

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add it back! πŸ˜…

}

// 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
}
}
}
}