From c8606293206d9687b3364eb2c78489cfc004a4b7 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 20 Feb 2024 21:18:38 +0100 Subject: [PATCH] feat: automd integration --- docs/.config/docs.jsonc | 1 + docs/1.guide/index.md | 4 +++ layers/core/app/modules/content/index.ts | 9 +++++- layers/core/app/modules/content/source.mjs | 35 +++++++++++++++------- schema/config.d.ts | 1 + schema/config.json | 4 +++ 6 files changed, 42 insertions(+), 12 deletions(-) diff --git a/docs/.config/docs.jsonc b/docs/.config/docs.jsonc index 0fbbea7..c235c7e 100644 --- a/docs/.config/docs.jsonc +++ b/docs/.config/docs.jsonc @@ -8,6 +8,7 @@ "redirects": { "/docs": "/docs/getting-started", }, + "automd": true, "landing": { // "heroLinks": { // "stackblitz": { "icon": "i-heroicons-play", "to": "https://stackblitz.com/github/unjs/docs/tree/main/template" } diff --git a/docs/1.guide/index.md b/docs/1.guide/index.md index 0e43f11..739ef91 100644 --- a/docs/1.guide/index.md +++ b/docs/1.guide/index.md @@ -29,3 +29,7 @@ Build for production: ## Components :read-more{to="/components"} + + + + diff --git a/layers/core/app/modules/content/index.ts b/layers/core/app/modules/content/index.ts index d4d04e4..13b7e01 100644 --- a/layers/core/app/modules/content/index.ts +++ b/layers/core/app/modules/content/index.ts @@ -4,7 +4,7 @@ import type { ModuleOptions as ContentOptions } from '@nuxt/content' import type { DocsConfig } from '../../../../../schema/config' export default defineNuxtModule({ - setup(_, nuxt) { + async setup(_, nuxt) { if (nuxt.options._prepare) { return } @@ -21,6 +21,13 @@ export default defineNuxtModule({ }) } + if (docsConfig.automd) { + const { loadConfig } = await import('automd') + docsConfig.automd = loadConfig(docsConfig.dir, { + ...docsConfig.automd, + }) + } + contentConfig.sources = { ...contentConfig.sources, content: { diff --git a/layers/core/app/modules/content/source.mjs b/layers/core/app/modules/content/source.mjs index d55673b..9f48632 100644 --- a/layers/core/app/modules/content/source.mjs +++ b/layers/core/app/modules/content/source.mjs @@ -1,5 +1,6 @@ import { defineDriver } from 'unstorage' import fsDriver from 'unstorage/drivers/fs' +import { transform } from 'automd' export default (opts) => { const _fs = fsDriver({ @@ -11,25 +12,37 @@ export default (opts) => { name: 'content', async getItem(key) { const val = await _fs.getItem(key) - if (opts.docsConfig.landing === false) { - return val - } - if (!val && key === 'index.json') { - return await import('./landing.mjs').then(({ genLanding }) => genLanding(opts.docsConfig)) + + // Landing + if (opts.docsConfig.landing !== false) { + if (!val && key === 'index.json') { + return await import('./landing.mjs').then(({ genLanding }) => genLanding(opts.docsConfig)) + } + if (!val && key === 'index.json$') { + return { mtime: new Date() } + } } - if (!val && key === 'index.json$') { - return { mtime: new Date() } + + // Automd transform + if (opts.docsConfig.automd) { + if (key.endsWith('.md') && typeof val === 'string') { + const res = await transform(val, opts.docsConfig.automd) + return res.contents + } else if (key.endsWith('.md$')) { + return { mtime: new Date() } + } } + return val }, async getKeys(prefix) { const keys = await _fs.getKeys(prefix) - if (opts.docsConfig.landing === false) { - return keys - } - if (!keys.some((key) => /^index\.\w+$/.test(key))) { + + // Landing + if (opts.docsConfig.landing !== false && !keys.some((key) => /^index\.\w+$/.test(key))) { keys.push('index.json') } + return keys }, }) diff --git a/schema/config.d.ts b/schema/config.d.ts index 186e3be..b920ee3 100644 --- a/schema/config.d.ts +++ b/schema/config.d.ts @@ -7,6 +7,7 @@ export interface DocsConfig { github?: string themeColor?: string redirects?: Record + automd?: any landing?: | false | { diff --git a/schema/config.json b/schema/config.json index 9312056..a1a28a2 100644 --- a/schema/config.json +++ b/schema/config.json @@ -34,6 +34,10 @@ "type": "string", "description": "The theme color of the documentation site.\nIt will be used as the `theme-color` meta tag and a full palette of colors will be generated from it." }, + "automd": { + "type": "boolean", + "description": "Enable integration with https://automd.unjs.io" + }, "redirects": { "description": "Redirects for the documentation site.", "type": "object",