Skip to content

Commit

Permalink
fix: read automd config in runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 21, 2024
1 parent acdd047 commit 0e6c530
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
9 changes: 6 additions & 3 deletions app/modules/content/source.mjs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { defineDriver } from 'unstorage'
import fsDriver from 'unstorage/drivers/fs'
import { transform } from 'automd'
import { transform, loadConfig } from 'automd'

export default (opts) => {
const _fs = fsDriver({
base: opts.docsConfig.dir,
})

let automdConfig

return defineDriver({
..._fs,
name: 'content',
async getItem(key) {
const val = await _fs.getItem(key)

// Landing
if (opts.docsConfig.landing !== false) {
if (opts.docsConfig.automd) {
if (!val && key === 'index.json') {
return await import('./landing.mjs').then(({ genLanding }) => genLanding(opts.docsConfig))
}
Expand All @@ -26,7 +28,8 @@ export default (opts) => {
// Automd transform
if (opts.docsConfig.automd) {
if (key.endsWith('.md') && typeof val === 'string') {
const res = await transform(val, opts.docsConfig.automd)
if (!automdConfig) { automdConfig = await loadConfig(opts.docsConfig.dir, opts.docsConfig.automd) }
const res = await transform(val, automdConfig)
if (res.hasChanged) {
_fs.setItem(key, res.contents).catch(console.error)
}
Expand Down
14 changes: 14 additions & 0 deletions docs/.config/automd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineGenerator, type Config } from 'automd'

export default <Config>{
generators: {
test: defineGenerator({
name: 'test',
async generate() {
return {
contents: `automd works!`,
}
},
}),
},
}
6 changes: 2 additions & 4 deletions docs/1.guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ Build for production:

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

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

---

_🤖 auto updated with [automd](https://automd.unjs.io) (last updated: Wed Feb 21 2024)_
automd works!

<!-- /automd -->

0 comments on commit 0e6c530

Please sign in to comment.