mirror of https://github.com/requarks/wiki
pull/7766/head
parent
1c7b34462c
commit
f81ed87b89
@ -0,0 +1,74 @@
|
||||
// ------------------------------------
|
||||
// Markdown - GitHub-style Alerts (Client-side)
|
||||
// ------------------------------------
|
||||
|
||||
function githubAlertsPlugin(md, options = {}) {
|
||||
const alertTypes = {
|
||||
'note': { class: 'is-info' },
|
||||
'info': { class: 'is-info' },
|
||||
'tip': { class: 'is-success' },
|
||||
'warning': { class: 'is-warning' },
|
||||
'important': { class: 'is-info' },
|
||||
'caution': { class: 'is-danger' }
|
||||
}
|
||||
|
||||
// Store original renderer - will handle line number injection if it exists
|
||||
const defaultRender = md.renderer.rules.blockquote_open || function(tokens, idx, options, env, slf) {
|
||||
return slf.renderToken(tokens, idx, options)
|
||||
}
|
||||
|
||||
// Override blockquote_open renderer to handle both alerts and line numbers
|
||||
md.renderer.rules.blockquote_open = function(tokens, idx, opts, env, slf) {
|
||||
const nextToken = tokens[idx + 1]
|
||||
|
||||
// Check if the next token is a paragraph containing inline content
|
||||
if (nextToken && nextToken.type === 'paragraph_open') {
|
||||
const inlineToken = tokens[idx + 2] // The inline token after paragraph_open
|
||||
|
||||
if (inlineToken && inlineToken.type === 'inline' && inlineToken.children) {
|
||||
const firstChild = inlineToken.children[0]
|
||||
|
||||
if (firstChild && firstChild.type === 'text') {
|
||||
const content = firstChild.content
|
||||
const alertMatch = content.match(/^\[!(NOTE|INFO|TIP|WARNING|IMPORTANT|CAUTION)\](?:\s+(.+?))?$/i)
|
||||
|
||||
if (alertMatch) {
|
||||
const alertType = alertMatch[1].toLowerCase()
|
||||
const customTitle = alertMatch[2] ? alertMatch[2].trim() : null
|
||||
|
||||
const alertConfig = alertTypes[alertType]
|
||||
if (alertConfig) {
|
||||
// Add CSS class to blockquote
|
||||
tokens[idx].attrJoin('class', alertConfig.class)
|
||||
|
||||
// Handle custom title or remove alert tag
|
||||
if (customTitle && options.customTitle !== false) {
|
||||
// Replace with bold custom title
|
||||
firstChild.content = `**${customTitle}**`
|
||||
} else if (customTitle && options.customTitle === false) {
|
||||
// Custom titles disabled but title provided - remove the entire alert line
|
||||
firstChild.content = ''
|
||||
} else {
|
||||
// No custom title - remove the alert tag
|
||||
firstChild.content = ''
|
||||
}
|
||||
|
||||
// If we emptied the first text node, remove it
|
||||
if (firstChild.content === '') {
|
||||
inlineToken.children.shift()
|
||||
// Also remove the following softbreak if it exists
|
||||
if (inlineToken.children[0] && inlineToken.children[0].type === 'softbreak') {
|
||||
inlineToken.children.shift()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return defaultRender(tokens, idx, opts, env, slf)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = githubAlertsPlugin
|
||||
Loading…
Reference in new issue