Skip to content

Commit

Permalink
feat: autogen section icons (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpreston321 committed Mar 4, 2024
1 parent 84e4b68 commit c770991
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
43 changes: 42 additions & 1 deletion app/server/plugins/content.ts
@@ -1,10 +1,15 @@
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('content:file:afterParse', (file) => {
nitroApp.hooks.hook('content:file:afterParse' as any, async (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)
}

// Remove first h1 from markdown files as it is added to front-matter as title
if (file.body?.children?.[0]?.tag === 'h1') {
const text = getTextContents(file.body.children[0].children)
Expand Down Expand Up @@ -50,3 +55,39 @@ function getTextContents(children) {
})
.join('')
}

// A set of common icons
const commonIcons = [
{
pattern: 'guide',
icon: 'ph:book-open-duotone',
},
{
pattern: 'components',
icon: 'bxs:component',
},
{
pattern: 'config',
icon: 'ri:settings-3-line',
},
{
pattern: 'configuration',
icon: 'ri:settings-3-line',
},
{
pattern: 'examples',
icon: 'ph:code',
},
{
pattern: 'utils',
icon: 'ph:function-bold',
},
]

function resolveIcon(path: string) {
for (const icon of commonIcons) {
if (path.includes(icon.pattern)) {
return icon.icon
}
}
}
4 changes: 0 additions & 4 deletions docs/1.guide/1.index.md
@@ -1,7 +1,3 @@
---
icon: ph:book-open-duotone
---

# Getting Started

> [!IMPORTANT]
Expand Down
4 changes: 0 additions & 4 deletions docs/1.guide/components.md
@@ -1,7 +1,3 @@
---
icon: bxs:component
---

# Components

> Discover the components you can use in your markdown files.
Expand Down
4 changes: 0 additions & 4 deletions docs/2.config/1.index.md
@@ -1,7 +1,3 @@
---
icon: ri:settings-3-line
---

# Configuration

## Example
Expand Down

0 comments on commit c770991

Please sign in to comment.