Skip to content

Commit

Permalink
markdown: Add ability to extend Dovecot-specific markdown with additi…
Browse files Browse the repository at this point in the history
…onal labels
  • Loading branch information
slusarz committed Jul 22, 2024
1 parent 0b2e1dc commit a1dde8f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
21 changes: 20 additions & 1 deletion .vitepress/local.js.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* This file allows configuration overwrite of default paths. */
/* This file allows configuration overrides of various core settings. */

// Allows custom mapping of data sources.
// It is used by the VitePress data loaders to determine what data to
Expand Down Expand Up @@ -53,3 +53,22 @@ export const man_paths = []
//
// Default: [ 'docs/core/plugins/*.md' ]
export const plugin_paths = []

// Enable additional labels to support in Dovecot-specific markdown
// processing (i.e. [[xyz,...]]).
export const markdown_extend = {

// Init function. Return value is configuration options to add to markdown
// object.
// init: () => { return {} },

// Opening tag function. Returns opening tag.
// open: (mode, parts, opts, env) => { return '' },

// Body function. Returns body text.
// body: (mode, env) => { return '' },

// Close tag function. Returns closing tag.
// close: (mode, env) => { return '' },

}
49 changes: 32 additions & 17 deletions lib/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fg from 'fast-glob'
import deflistPlugin from 'markdown-it-deflist'
import path from 'path'
import { createMarkdownRenderer } from 'vitepress'
import { loadData, loadDovecotLinks, manFiles, pluginFiles, resolveURL } from './utility.js'
import { loadData, loadDovecotLinks, manFiles, markdownExtension, pluginFiles, resolveURL } from './utility.js'

let md_conf = null
export async function initDovecotMd(base) {
Expand All @@ -12,21 +12,24 @@ export async function initDovecotMd(base) {
}

md_conf = {
base: base,
doveadm: (await loadData('doveadm')).doveadm,
dovecotlinks: await loadDovecotLinks(base),
events: (await loadData('events')).events,
man: (await manFiles()).flatMap((x) => {
return fg.sync(x).map((y) => {
const str = path.basename(y)
return str.substring(0, str.indexOf('.'))
})
}),
plugins: (await pluginFiles()).flatMap((x) =>
fg.sync(x).map((y) => path.basename(y, '.md'))
),
settings: (await loadData('settings')).settings,
updates: (await loadData('updates')).updates
...{
base: base,
doveadm: (await loadData('doveadm')).doveadm,
dovecotlinks: await loadDovecotLinks(base),
events: (await loadData('events')).events,
man: (await manFiles()).flatMap((x) => {
return fg.sync(x).map((y) => {
const str = path.basename(y)
return str.substring(0, str.indexOf('.'))
})
}),
plugins: (await pluginFiles()).flatMap((x) =>
fg.sync(x).map((y) => path.basename(y, '.md'))
),
settings: (await loadData('settings')).settings,
updates: (await loadData('updates')).updates
},
...(await markdownExtension())
}
}

Expand Down Expand Up @@ -282,7 +285,8 @@ function dovecot_markdown(md, opts) {
opts.base) + '">'

default:
throw new Error('Unknown dovecot markdown command: ' + mode)
return handle_default(mode,
opts.markdown?.open?.(mode, parts, opts, env))
}
}

Expand Down Expand Up @@ -324,6 +328,9 @@ function dovecot_markdown(md, opts) {

case 'variable':
return env.inner

default:
return handle_default(mode, opts.markdown?.body?.(mode, env))
}
}

Expand All @@ -349,9 +356,17 @@ function dovecot_markdown(md, opts) {
case 'plugin':
case 'rfc':
return '</a>'

default:
return handle_default(mode, opts.markdown?.close?.(mode, env))
}
}

function handle_default(mode, res) {
if (res) { return res }
throw new Error('Unknown dovecot markdown command: ' + mode)
}

md.inline.ruler.after('emphasis', 'dovecot_brackets', process_brackets)
md.renderer.rules.dovecot_open = dovecot_open
md.renderer.rules.dovecot_body = dovecot_body
Expand Down
11 changes: 11 additions & 0 deletions lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ async function loadLocalConf() {
return local_conf
}

export async function markdownExtension() {
const lconf = await loadLocalConf()

return {
...{
markdown: lconf.markdown_extend ?? {},
},
...(lconf.markdown_extend?.init?.() ?? {})
}
}

export async function loadData(id) {
/* Check for config override file. */
const lconf = await loadLocalConf()
Expand Down

0 comments on commit a1dde8f

Please sign in to comment.