Skip to content

Commit 0ab9eb3

Browse files
slusarzcmouse
authored andcommitted
markdown: Fix linking to dovecotlinks pages in CE
This was due to the code ignoring the srcDir config when building links.
1 parent d9a7c78 commit 0ab9eb3

File tree

3 files changed

+24
-29
lines changed

3 files changed

+24
-29
lines changed

.vitepress/local.js.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const watch_paths = []
3535
// This is the same format as VitePress' 'rewrite' setting:
3636
// https://vitepress.dev/reference/site-config#rewrites
3737
//
38-
// Default: {}
38+
// Default: { 'docs/:path(.*)': ':path' }
3939
export const source_path_translations = {}
4040

4141
// A listing of paths containing man files.

lib/markdown.js

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import deflistPlugin from 'markdown-it-deflist'
22
import { createMarkdownRenderer } from 'vitepress'
3-
import { loadData, loadDovecotLinks } from './utility.js'
3+
import { loadData, loadDovecotLinks, resolveURL } from './utility.js'
44

55
export async function dovecotMdExtend(md) {
66
md.use(deflistPlugin)
77
md.use(dovecot_markdown, {
8-
base: globalThis.VITEPRESS_CONFIG.site.base,
98
dovecotlinks: await loadDovecotLinks(),
109
updates: (await loadData('updates')).updates
1110
})
@@ -90,11 +89,6 @@ function dovecot_markdown(md, opts) {
9089
return true
9190
}
9291

93-
function add_base(url) {
94-
return (opts.base.endsWith('/') ? opts.base.slice(0, -1) : opts.base)
95-
+ '/' + url
96-
}
97-
9892
function dovecot_open(tokens, index, mdOpts, env) {
9993
const token = tokens[index]
10094
const mode = token.attrGet('mode')
@@ -133,43 +127,36 @@ function dovecot_markdown(md, opts) {
133127
}
134128

135129
return '<code><a href="' +
136-
add_base('core/summaries/' + page + '.html#' + env.inner) +
130+
resolveURL('core/summaries/' + page + '.html#' + env.inner) +
137131
'">'
138132

139133
case 'link':
140134
let url = '#'
141135
env.inner = false
142136

143-
if (opts.dovecotlinks[parts[1]]) {
144-
const d = opts.dovecotlinks[parts[1]]
145-
env.inner = parts[2] ? parts[2] : (d.text ? d.text : false)
146-
if (d.url) {
147-
if (d.url.startsWith('http')) {
148-
url = d.url
149-
} else {
150-
url = add_base(d.url)
151-
}
152-
}
153-
} else {
137+
if (!opts.dovecotlinks[parts[1]]) {
154138
throw new Error('Dovecot link missing: ' + parts[1])
155139
}
156140

157-
return '<a href="' + url + '">'
141+
const d = opts.dovecotlinks[parts[1]]
142+
env.inner = parts[2] ? parts[2] : (d.text ? d.text : false)
143+
144+
return '<a href="' + d.url + '">'
158145

159146
case 'man':
160147
env.inner = parts[1]
161148
hash = parts[2] ? parts[2] : false;
162149
env.args = parts[3] ? parts[3] : 1;
163150

164151
return '<code><a href="' +
165-
add_base('core/man/' + env.inner + '.' + env.args) +
152+
resolveURL('core/man/' + env.inner + '.' + env.args) +
166153
'.html' + (hash ? '#' + hash : '') + '">'
167154

168155
case 'plugin':
169156
env.inner = parts[1]
170157

171158
return '<a href="' +
172-
add_base('core/plugins/' + env.inner.replaceAll('-', '_') +
159+
resolveURL('core/plugins/' + env.inner.replaceAll('-', '_') +
173160
'.html' + (parts[2] ? '#' + parts[2] : '')) + '">'
174161

175162
case 'removed':
@@ -217,7 +204,7 @@ function dovecot_markdown(md, opts) {
217204
}
218205

219206
return '<code><a href="' +
220-
add_base('core/settings/variables.html' + hash) + '">'
207+
resolveURL('core/settings/variables.html' + hash) + '">'
221208

222209
default:
223210
throw new Error('Unknown dovecot markdown command: ' + mode)

lib/utility.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,20 @@ export async function frontmatterIter(callback) {
8888
const sf = await sourceFiles()
8989
const files = sf.flatMap((x) => fg.sync(x))
9090
let spt = []
91+
let spt_conf = { 'docs/:path(.*)': ':path' }
9192

9293
if (fs.existsSync(local_conf)) {
9394
const data = await import(local_conf)
9495
if (data.source_path_translations) {
95-
spt = Object.entries(data.source_path_translations).map(([from, to]) => ({
96-
toPath: compile(`/${to}`, { validate: false }),
97-
matchUrl: match(from.startsWith('^') ? new RegExp(from) : from)
98-
}))
96+
spt_conf = data.source_path_translations
9997
}
10098
}
10199

100+
spt = Object.entries(spt_conf).map(([from, to]) => ({
101+
toPath: compile(`/${to}`, { validate: false }),
102+
matchUrl: match(from.startsWith('^') ? new RegExp(from) : from)
103+
}))
104+
102105
for (let f of files) {
103106
const str = fs.readFileSync(f, 'utf8')
104107
const data = matter(str).data
@@ -129,7 +132,7 @@ export async function loadDovecotLinks() {
129132
}
130133

131134
links[k] = {
132-
url: f.substring(0, f.lastIndexOf('.')) + '.html'
135+
url: resolveURL(f.substring(0, f.lastIndexOf('.')) + '.html')
133136
}
134137

135138
if ((typeof v) == 'object') {
@@ -148,3 +151,8 @@ export async function loadDovecotLinks() {
148151
/* Merge the two lists together. */
149152
return { ...links, ...data.links_overrides }
150153
}
154+
155+
export function resolveURL(url) {
156+
const base = globalThis.VITEPRESS_CONFIG.site.base
157+
return (base.endsWith('/') ? base.slice(0, -1) : base) + '/' + url
158+
}

0 commit comments

Comments
 (0)