chore: format

pull/3482/head
Anthony Fu 2 years ago
parent 38483da052
commit aebcb799df

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

Loading…
Cancel
Save