|
| 1 | +/** |
| 2 | +* Markdown-it plugin for replacing text placeholders |
| 3 | +* |
| 4 | +* Replaces custom placeholders like |phpversion| with configured values. |
| 5 | +* Supports optional formatting (bold, italic, code, or none). |
| 6 | +*/ |
| 7 | + |
| 8 | +/** |
| 9 | +* Format a value based on the specified format type |
| 10 | +* @param {string} value - The value to format |
| 11 | +* @param {string} format - Format type: 'bold', 'italic', 'code', or 'none' |
| 12 | +* @returns {string} Formatted value |
| 13 | +*/ |
| 14 | +function formatValue(value, format = 'none') { |
| 15 | + switch (format) { |
| 16 | + case 'bold': |
| 17 | + return `**${value}**` |
| 18 | + case 'italic': |
| 19 | + return `*${value}*` |
| 20 | + case 'code': |
| 21 | + return `\`${value}\`` |
| 22 | + default: |
| 23 | + return value |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | +* Create a text substitution plugin |
| 29 | +* @param {Object} md - markdown-it instance |
| 30 | +* @param {Object} options - plugin options |
| 31 | +* @param {Object} options.substitutions - Object with placeholder keys and values |
| 32 | +* @param {string} options.phpversion - Legacy: Current PHP version (deprecated, use substitutions) |
| 33 | +* @param {string} options.minphpversion - Legacy: Minimum PHP version (deprecated, use substitutions) |
| 34 | +* @returns {void} |
| 35 | +*/ |
| 36 | +export function substitutionsReplacer(md, options = {}) { |
| 37 | + let substitutions = {} |
| 38 | + |
| 39 | + // Support new substitutions format |
| 40 | + if (options.substitutions) { |
| 41 | + substitutions = options.substitutions |
| 42 | + } |
| 43 | + // Backward compatibility: convert old phpVersions format |
| 44 | + else if (options.phpversion || options.minphpversion) { |
| 45 | + substitutions = { |
| 46 | + '|phpversion|': { value: options.phpversion || '8.4', format: 'bold' }, |
| 47 | + '|minphpversion|': { value: options.minphpversion || '8.1', format: 'italic' } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Store original render method |
| 52 | + const originalRender = md.render.bind(md) |
| 53 | + |
| 54 | + md.render = function(src, env = {}) { |
| 55 | + // Process each substitution |
| 56 | + for (const [placeholder, config] of Object.entries(substitutions)) { |
| 57 | + // Support both simple string values and objects with format options |
| 58 | + const value = typeof config === 'string' ? config : config.value |
| 59 | + const format = typeof config === 'object' && config.format ? config.format : 'none' |
| 60 | + |
| 61 | + // Create regex to match the placeholder (escape special regex characters) |
| 62 | + const escapedPlaceholder = placeholder.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') |
| 63 | + const regex = new RegExp(escapedPlaceholder, 'g') |
| 64 | + |
| 65 | + // Replace with formatted value |
| 66 | + src = src.replace(regex, formatValue(value, format)) |
| 67 | + } |
| 68 | + |
| 69 | + return originalRender(src, env) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export default substitutionsReplacer |
0 commit comments