|
| 1 | +import MarkdownIt from 'markdown-it' |
| 2 | + |
| 3 | +export interface AvailableSinceParams { |
| 4 | + rails?: string |
| 5 | + core?: string |
| 6 | + description?: string |
| 7 | +} |
| 8 | + |
| 9 | +function parseAvailableSinceParams(info: string): AvailableSinceParams { |
| 10 | + const basicMatch = info.trim().match(/^available_since(?:\s+(.*))?$/) |
| 11 | + if (!basicMatch) return {} |
| 12 | + |
| 13 | + const allParams = basicMatch[1] || '' |
| 14 | + const params: AvailableSinceParams = {} |
| 15 | + |
| 16 | + // Parse out key=value pairs first |
| 17 | + const keyValueMatches = [ |
| 18 | + ...allParams.matchAll(/([a-z]+)(?:=("[^"]*"|[^\s"]+))?/g), |
| 19 | + ] |
| 20 | + for (const [, key, value] of keyValueMatches) { |
| 21 | + let cleanValue = value ? value.replace(/^"|"$/g, '') : true |
| 22 | + |
| 23 | + if (key === 'rails') params.rails = cleanValue as string |
| 24 | + if (key === 'core') params.core = cleanValue as string |
| 25 | + if (key === 'description') params.description = cleanValue as string |
| 26 | + } |
| 27 | + |
| 28 | + return params |
| 29 | +} |
| 30 | + |
| 31 | +export function availableSinceMarkdownPlugin(md: MarkdownIt) { |
| 32 | + md.block.ruler.before( |
| 33 | + 'paragraph', |
| 34 | + 'available_since_oneliner', |
| 35 | + (state, start, end, silent) => { |
| 36 | + const line = state.getLines(start, start + 1, 0, false).trim() |
| 37 | + |
| 38 | + const match = line.match(/^@available_since\s+(.+)$/) |
| 39 | + if (!match) return false |
| 40 | + |
| 41 | + if (silent) return true |
| 42 | + |
| 43 | + const params = parseAvailableSinceParams(`available_since ${match[1]}`) |
| 44 | + const token = state.push('available_since_oneliner', '', 0) |
| 45 | + |
| 46 | + token.content = renderAvailableSince(params, md) |
| 47 | + token.map = [start, start + 1] |
| 48 | + |
| 49 | + state.line = start + 1 |
| 50 | + return true |
| 51 | + }, |
| 52 | + ) |
| 53 | + |
| 54 | + // Render the one-liner available_since token |
| 55 | + md.renderer.rules.available_since_oneliner = (tokens, idx) => { |
| 56 | + return tokens[idx].content + '\n' |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +function renderAvailableSince( |
| 61 | + params: AvailableSinceParams, |
| 62 | + md: MarkdownIt, |
| 63 | +): string { |
| 64 | + const railsAttr = params.rails |
| 65 | + ? `rails="${md.utils.escapeHtml(params.rails)}"` |
| 66 | + : '' |
| 67 | + const coreAttr = params.core |
| 68 | + ? `core="${md.utils.escapeHtml(params.core)}"` |
| 69 | + : '' |
| 70 | + const descriptionAttr = params.description |
| 71 | + ? `description="${md.utils.escapeHtml(params.description)}"` |
| 72 | + : '' |
| 73 | + |
| 74 | + return `<AvailableSince ${railsAttr} ${coreAttr} ${descriptionAttr} />` |
| 75 | +} |
0 commit comments