From c7709914707ea2d3ab217899498fdcfa98469ed6 Mon Sep 17 00:00:00 2001 From: Christian Preston Date: Mon, 4 Mar 2024 11:49:34 -0500 Subject: [PATCH] feat: autogen section icons (#72) --- app/server/plugins/content.ts | 43 ++++++++++++++++++++++++++++++++++- docs/1.guide/1.index.md | 4 ---- docs/1.guide/components.md | 4 ---- docs/2.config/1.index.md | 4 ---- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/app/server/plugins/content.ts b/app/server/plugins/content.ts index e637f5b..40394df 100644 --- a/app/server/plugins/content.ts +++ b/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) @@ -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 + } + } +} diff --git a/docs/1.guide/1.index.md b/docs/1.guide/1.index.md index 4ea7825..9beb9a7 100644 --- a/docs/1.guide/1.index.md +++ b/docs/1.guide/1.index.md @@ -1,7 +1,3 @@ ---- -icon: ph:book-open-duotone ---- - # Getting Started > [!IMPORTANT] diff --git a/docs/1.guide/components.md b/docs/1.guide/components.md index ad03025..b1739ed 100644 --- a/docs/1.guide/components.md +++ b/docs/1.guide/components.md @@ -1,7 +1,3 @@ ---- -icon: bxs:component ---- - # Components > Discover the components you can use in your markdown files. diff --git a/docs/2.config/1.index.md b/docs/2.config/1.index.md index 00cd075..5590be6 100644 --- a/docs/2.config/1.index.md +++ b/docs/2.config/1.index.md @@ -1,7 +1,3 @@ ---- -icon: ri:settings-3-line ---- - # Configuration ## Example