|
|
|
@ -2,10 +2,18 @@ import type MarkdownIt from 'markdown-it'
|
|
|
|
|
import type { ContainerOptions } from './containers'
|
|
|
|
|
|
|
|
|
|
export const gitHubAlertsPlugin = (
|
|
|
|
|
md: MarkdownIt,
|
|
|
|
|
md: MarkdownIt,
|
|
|
|
|
options?: ContainerOptions
|
|
|
|
|
) => {
|
|
|
|
|
const markers = ['TIP', 'NOTE', 'INFO', 'IMPORTANT', 'WARNING', 'CAUTION', 'DANGER']
|
|
|
|
|
const markers = [
|
|
|
|
|
'TIP',
|
|
|
|
|
'NOTE',
|
|
|
|
|
'INFO',
|
|
|
|
|
'IMPORTANT',
|
|
|
|
|
'WARNING',
|
|
|
|
|
'CAUTION',
|
|
|
|
|
'DANGER'
|
|
|
|
|
]
|
|
|
|
|
const matchCaseSensitive = true
|
|
|
|
|
const titleMark = {
|
|
|
|
|
tip: options?.tipLabel || 'TIP',
|
|
|
|
@ -14,11 +22,14 @@ export const gitHubAlertsPlugin = (
|
|
|
|
|
important: options?.importantLabel || 'IMPORTANT',
|
|
|
|
|
warning: options?.warningLabel || 'WARNING',
|
|
|
|
|
caution: options?.cautionLabel || 'CAUTION',
|
|
|
|
|
danger: options?.dangerLabel || 'DANGER',
|
|
|
|
|
danger: options?.dangerLabel || 'DANGER'
|
|
|
|
|
} as Record<string, string>
|
|
|
|
|
|
|
|
|
|
const markerNameRE = markers.join('|')
|
|
|
|
|
const RE = new RegExp(`^\\[\\!(${markerNameRE})\\]([^\\n\\r]*)`, matchCaseSensitive ? '' : 'i')
|
|
|
|
|
const markerNameRE = markers.join('|')
|
|
|
|
|
const RE = new RegExp(
|
|
|
|
|
`^\\[\\!(${markerNameRE})\\]([^\\n\\r]*)`,
|
|
|
|
|
matchCaseSensitive ? '' : 'i'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
md.core.ruler.after('block', 'github-alerts', (state) => {
|
|
|
|
|
const tokens = state.tokens
|
|
|
|
@ -30,20 +41,22 @@ export const gitHubAlertsPlugin = (
|
|
|
|
|
i += 1
|
|
|
|
|
const close = tokens[i]
|
|
|
|
|
const endIndex = i
|
|
|
|
|
const firstContent = tokens.slice(startIndex, endIndex + 1).find(token => token.type === 'inline')
|
|
|
|
|
if (!firstContent)
|
|
|
|
|
continue
|
|
|
|
|
const firstContent = tokens
|
|
|
|
|
.slice(startIndex, endIndex + 1)
|
|
|
|
|
.find((token) => token.type === 'inline')
|
|
|
|
|
if (!firstContent) continue
|
|
|
|
|
const match = firstContent.content.match(RE)
|
|
|
|
|
if (!match)
|
|
|
|
|
continue
|
|
|
|
|
if (!match) continue
|
|
|
|
|
const type = match[1].toLowerCase()
|
|
|
|
|
const title = match[2].trim() || titleMark[type] || capitalize(type)
|
|
|
|
|
firstContent.content = firstContent.content.slice(match[0].length).trimStart()
|
|
|
|
|
firstContent.content = firstContent.content
|
|
|
|
|
.slice(match[0].length)
|
|
|
|
|
.trimStart()
|
|
|
|
|
open.type = 'github_alert_open'
|
|
|
|
|
open.tag = 'div'
|
|
|
|
|
open.meta = {
|
|
|
|
|
title,
|
|
|
|
|
type,
|
|
|
|
|
type
|
|
|
|
|
}
|
|
|
|
|
close.type = 'github_alert_close'
|
|
|
|
|
close.tag = 'div'
|
|
|
|
@ -57,7 +70,6 @@ export const gitHubAlertsPlugin = (
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function capitalize(str: string) {
|
|
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1)
|
|
|
|
|
}
|
|
|
|
|