Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support GitHub-flavored alerts (#3482)
Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type MarkdownIt from 'markdown-it' | ||
import type { ContainerOptions } from './containers' | ||
|
||
const markerRE = | ||
/^\[\!(TIP|NOTE|INFO|IMPORTANT|WARNING|CAUTION|DANGER)\]([^\n\r]*)/i | ||
|
||
export const gitHubAlertsPlugin = ( | ||
md: MarkdownIt, | ||
options?: ContainerOptions | ||
) => { | ||
const titleMark = { | ||
tip: options?.tipLabel || 'TIP', | ||
note: options?.noteLabel || 'NOTE', | ||
info: options?.infoLabel || 'INFO', | ||
important: options?.importantLabel || 'IMPORTANT', | ||
warning: options?.warningLabel || 'WARNING', | ||
caution: options?.cautionLabel || 'CAUTION', | ||
danger: options?.dangerLabel || 'DANGER' | ||
} as Record<string, string> | ||
|
||
md.core.ruler.after('block', 'github-alerts', (state) => { | ||
const tokens = state.tokens | ||
for (let i = 0; i < tokens.length; i++) { | ||
if (tokens[i].type === 'blockquote_open') { | ||
const open = tokens[i] | ||
const startIndex = i | ||
while (tokens[i]?.type !== 'blockquote_close' && i <= tokens.length) | ||
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 match = firstContent.content.match(markerRE) | ||
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() | ||
open.type = 'github_alert_open' | ||
open.tag = 'div' | ||
open.meta = { | ||
title, | ||
type | ||
} | ||
close.type = 'github_alert_close' | ||
close.tag = 'div' | ||
} | ||
} | ||
}) | ||
md.renderer.rules.github_alert_open = function (tokens, idx) { | ||
const { title, type } = tokens[idx].meta | ||
const attrs = '' | ||
return `<div class="${type} custom-block github-alert"${attrs}><p class="custom-block-title">${title}</p>\n` | ||
} | ||
} | ||
|
||
function capitalize(str: string) { | ||
return str.charAt(0).toUpperCase() + str.slice(1) | ||
} |