|
| 1 | +import markdownItContainer from 'markdown-it-container' |
| 2 | + |
| 3 | +/** |
| 4 | + * Customize the markdown-it instance with additional plugins |
| 5 | + * @param {import('markdown-it')} md - The markdown-it instance |
| 6 | + * @returns {import('markdown-it')} - The modified markdown-it instance |
| 7 | + */ |
| 8 | +export default async function markdownItSettingsOverride (md) { |
| 9 | + // Add custom container for warnings |
| 10 | + md.use(markdownItContainer, 'warning', { |
| 11 | + validate: function (params) { |
| 12 | + return params.trim().match(/^warning\s*(.*)$/) |
| 13 | + }, |
| 14 | + render: function (tokens, idx) { |
| 15 | + const m = tokens[idx].info.trim().match(/^warning\s*(.*)$/) |
| 16 | + if (tokens[idx].nesting === 1) { |
| 17 | + const title = (m && m.length > 1) ? m[1] : 'Warning' |
| 18 | + return '<div class="custom-warning">\n<div class="warning-title">' + md.utils.escapeHtml(title) + '</div>\n<div class="warning-content">\n' |
| 19 | + } else { |
| 20 | + return '</div>\n</div>\n' |
| 21 | + } |
| 22 | + } |
| 23 | + }) |
| 24 | + |
| 25 | + // Add custom container for info boxes |
| 26 | + md.use(markdownItContainer, 'info', { |
| 27 | + validate: function (params) { |
| 28 | + return params.trim().match(/^info\s*(.*)$/) |
| 29 | + }, |
| 30 | + render: function (tokens, idx) { |
| 31 | + const m = tokens[idx].info.trim().match(/^info\s*(.*)$/) |
| 32 | + if (tokens[idx].nesting === 1) { |
| 33 | + const title = (m && m.length > 1) ? m[1] : 'Info' |
| 34 | + return '<div class="custom-info">\n<div class="info-title">' + md.utils.escapeHtml(title) + '</div>\n<div class="info-content">\n' |
| 35 | + } else { |
| 36 | + return '</div>\n</div>\n' |
| 37 | + } |
| 38 | + } |
| 39 | + }) |
| 40 | + |
| 41 | + // Add custom container for collapsible sections |
| 42 | + md.use(markdownItContainer, 'details', { |
| 43 | + validate: function (params) { |
| 44 | + return params.trim().match(/^details\s+(.*)$/) |
| 45 | + }, |
| 46 | + render: function (tokens, idx) { |
| 47 | + const m = tokens[idx].info.trim().match(/^details\s+(.*)$/) |
| 48 | + if (tokens[idx].nesting === 1) { |
| 49 | + return '<details>\n<summary>' + md.utils.escapeHtml(m[1]) + '</summary>\n<div class="details-content">\n' |
| 50 | + } else { |
| 51 | + return '</div>\n</details>\n' |
| 52 | + } |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + // Customize existing renderer - add custom classes to code blocks |
| 57 | + md.renderer.rules.code_block = function (tokens, idx, options, env, renderer) { |
| 58 | + const token = tokens[idx] |
| 59 | + const content = token.content |
| 60 | + const langName = token.info || '' |
| 61 | + |
| 62 | + return `<pre class="custom-code-block"><code class="language-${langName}">${md.utils.escapeHtml(content)}</code></pre>\n` |
| 63 | + } |
| 64 | + |
| 65 | + return md |
| 66 | +} |
0 commit comments