Skip to content

Commit

Permalink
feat: automd integration
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 20, 2024
1 parent 00761a3 commit c860629
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
1 change: 1 addition & 0 deletions docs/.config/docs.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
4 changes: 4 additions & 0 deletions docs/1.guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ Build for production:
## Components

:read-more{to="/components"}

<!-- automd:with-automd -->

<!-- /automd -->
9 changes: 8 additions & 1 deletion layers/core/app/modules/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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: {
Expand Down
35 changes: 24 additions & 11 deletions layers/core/app/modules/content/source.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineDriver } from 'unstorage'
import fsDriver from 'unstorage/drivers/fs'
import { transform } from 'automd'

export default (opts) => {
const _fs = fsDriver({
Expand All @@ -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
},
})
Expand Down
1 change: 1 addition & 0 deletions schema/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface DocsConfig {
github?: string
themeColor?: string
redirects?: Record<string, string>
automd?: any
landing?:
| false
| {
Expand Down
4 changes: 4 additions & 0 deletions schema/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c860629

Please sign in to comment.