From aebcb799df087ec799c0a38b764189710e17c530 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sat, 20 Jan 2024 13:42:11 +0100 Subject: [PATCH] chore: format --- src/node/markdown/plugins/githubAlerts.ts | 38 +++++++++++++++-------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/node/markdown/plugins/githubAlerts.ts b/src/node/markdown/plugins/githubAlerts.ts index 27fbdf07..853a83bd 100644 --- a/src/node/markdown/plugins/githubAlerts.ts +++ b/src/node/markdown/plugins/githubAlerts.ts @@ -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 - 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) }