Skip to content

Commit 27dfa9c

Browse files
committed
vitepress: Require initDovecotMd() to be called before configuration
1 parent 1430477 commit 27dfa9c

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

.vitepress/config.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path'
33
import { defineConfig } from 'vitepress'
44
import { pagefindPlugin } from 'vitepress-plugin-pagefind'
55
import { generateSidebar } from 'vitepress-sidebar'
6-
import { dovecotMdExtend } from '../lib/markdown.js'
6+
import { dovecotMdExtend, initDovecotMd } from '../lib/markdown.js'
77
import { frontmatterIter } from '../lib/utility.js'
88
import { checkExternalLinks, outputBrokenLinks } from '../lib/link_checker.js'
99

@@ -18,6 +18,11 @@ await frontmatterIter(function (f, data) {
1818
}
1919
})
2020

21+
// Need to bootstrap configuration for Dovecot markdown driver (specifically,
22+
// loading all data files to allow existence checking), or else the markdown
23+
// processing will begin before Dovecot link markup is enabled
24+
await initDovecotMd(base)
25+
2126
export default defineConfig({
2227
title: "Dovecot CE",
2328
description: "Dovecot CE Documentation",
@@ -103,7 +108,7 @@ export default defineConfig({
103108
},
104109

105110
markdown: {
106-
config: async (md) => await dovecotMdExtend(md),
111+
config: (md) => dovecotMdExtend(md),
107112
image: {
108113
lazyLoading: true,
109114
},

lib/markdown.js

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@ import path from 'path'
55
import { createMarkdownRenderer } from 'vitepress'
66
import { loadData, loadDovecotLinks, manFiles, pluginFiles, resolveURL } from './utility.js'
77

8-
export async function dovecotMdExtend(md) {
9-
md.use(containerPlugin, 'todo', {
10-
render: function(tokens, idx) {
11-
if (tokens[idx].nesting === 1) {
12-
return '<div class="caution custom-block">\n<p class="custom-block-title">⚠️ TODO</p>'
13-
} else {
14-
return '</div>\n'
15-
}
16-
}
17-
})
18-
md.use(deflistPlugin)
19-
md.use(dovecot_markdown, {
8+
let md_conf = null
9+
export async function initDovecotMd(base) {
10+
if (md_conf !== null) {
11+
return
12+
}
13+
14+
md_conf = {
15+
base: base,
2016
doveadm: (await loadData('doveadm')).doveadm,
21-
dovecotlinks: await loadDovecotLinks(),
17+
dovecotlinks: await loadDovecotLinks(base),
2218
events: (await loadData('events')).events,
2319
man: (await manFiles()).flatMap((x) => {
2420
return fg.sync(x).map((y) => {
@@ -31,18 +27,36 @@ export async function dovecotMdExtend(md) {
3127
),
3228
settings: (await loadData('settings')).settings,
3329
updates: (await loadData('updates')).updates
30+
}
31+
}
32+
33+
export function dovecotMdExtend(md) {
34+
md.use(containerPlugin, 'todo', {
35+
render: function(tokens, idx) {
36+
if (tokens[idx].nesting === 1) {
37+
return '<div class="caution custom-block">\n<p class="custom-block-title">⚠️ TODO</p>'
38+
} else {
39+
return '</div>\n'
40+
}
41+
}
3442
})
43+
md.use(deflistPlugin)
44+
45+
if (md_conf === null) {
46+
throw new Error('Must call initDovecotMd() before calling this function!')
47+
}
48+
md.use(dovecot_markdown, md_conf)
3549

3650
return md
3751
}
3852

3953
let vitepress_md = null
40-
4154
export async function getVitepressMd() {
4255
if (vitepress_md === null) {
4356
const config = globalThis.VITEPRESS_CONFIG
4457

45-
vitepress_md = await dovecotMdExtend(await createMarkdownRenderer(
58+
await initDovecotMd(config.site.base)
59+
vitepress_md = dovecotMdExtend(await createMarkdownRenderer(
4660
config.srcDir,
4761
config.markdown,
4862
config.site.base,
@@ -173,8 +187,8 @@ function dovecot_markdown(md, opts) {
173187
}
174188

175189
return '<code><a href="' +
176-
resolveURL('core/summaries/' + page + '.html#' + env.inner) +
177-
'">'
190+
resolveURL('core/summaries/' + page + '.html#' + env.inner,
191+
opts.base) + '">'
178192

179193
case 'link':
180194
let url = '#'
@@ -199,8 +213,8 @@ function dovecot_markdown(md, opts) {
199213
}
200214

201215
return '<code><a href="' +
202-
resolveURL('core/man/' + env.inner + '.' + env.args) +
203-
'.html' + (hash ? '#' + hash : '') + '">'
216+
resolveURL('core/man/' + env.inner + '.' + env.args,
217+
opts.base) + '.html' + (hash ? '#' + hash : '') + '">'
204218

205219
case 'plugin':
206220
env.inner = parts[1]
@@ -212,7 +226,7 @@ function dovecot_markdown(md, opts) {
212226

213227
return '<a href="' +
214228
resolveURL('core/plugins/' + plugin + '.html' +
215-
(parts[2] ? '#' + parts[2] : '')) + '">'
229+
(parts[2] ? '#' + parts[2] : ''), opts.base) + '">'
216230

217231
case 'removed':
218232
env.args = parts[1]
@@ -264,7 +278,8 @@ function dovecot_markdown(md, opts) {
264278
}
265279

266280
return '<code><a href="' +
267-
resolveURL('core/settings/variables.html' + hash) + '">'
281+
resolveURL('core/settings/variables.html' + hash,
282+
opts.base) + '">'
268283

269284
default:
270285
throw new Error('Unknown dovecot markdown command: ' + mode)

lib/utility.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export async function frontmatterIter(callback) {
132132
}
133133
}
134134

135-
export async function loadDovecotLinks() {
135+
export async function loadDovecotLinks(base) {
136136
const links = {}
137137

138138
await frontmatterIter(function (f, data) {
@@ -146,7 +146,7 @@ export async function loadDovecotLinks() {
146146
}
147147

148148
links[k] = {
149-
url: resolveURL(f.substring(0, f.lastIndexOf('.')) + '.html')
149+
url: resolveURL(f.substring(0, f.lastIndexOf('.')) + '.html', base)
150150
}
151151

152152
if ((typeof v) == 'object') {
@@ -166,8 +166,7 @@ export async function loadDovecotLinks() {
166166
return { ...links, ...data.links_overrides }
167167
}
168168

169-
export function resolveURL(url) {
170-
const base = globalThis.VITEPRESS_CONFIG.site.base
169+
export function resolveURL(url, base) {
171170
return (base.endsWith('/') ? base.slice(0, -1) : base) + '/' + url
172171
}
173172

0 commit comments

Comments
 (0)