|
| 1 | +import clsx from 'clsx' |
| 2 | +import { blockRegex, Priority } from 'markdown-to-jsx' |
| 3 | +import type { MarkdownToJSX } from 'markdown-to-jsx' |
| 4 | + |
| 5 | +import { |
| 6 | + FluentShieldError20Regular, |
| 7 | + FluentWarning28Regular, |
| 8 | + IonInformation, |
| 9 | +} from '~/components/icons/status' |
| 10 | + |
| 11 | +import { Markdown } from '../Markdown' |
| 12 | + |
| 13 | +const textColorMap = { |
| 14 | + NOTE: 'text-always-blue-500 dark:text-always-blue-400', |
| 15 | + IMPORTANT: 'text-accent', |
| 16 | + WARNING: 'text-amber-500 dark:text-amber-400', |
| 17 | +} as any |
| 18 | + |
| 19 | +const borderColorMap = { |
| 20 | + NOTE: 'border-always-blue-500 dark:border-always-blue-400', |
| 21 | + IMPORTANT: 'border-accent', |
| 22 | + WARNING: 'border-amber-500 dark:border-amber-400', |
| 23 | +} as any |
| 24 | + |
| 25 | +const typedIconMap = { |
| 26 | + NOTE: IonInformation, |
| 27 | + IMPORTANT: FluentWarning28Regular, |
| 28 | + WARNING: FluentShieldError20Regular, |
| 29 | +} as any |
| 30 | + |
| 31 | +/** |
| 32 | + * |
| 33 | + * > [!NOTE] |
| 34 | + * > Highlights information that users should take into account, even when skimming. |
| 35 | + */ |
| 36 | +const ALERT_BLOCKQUOTE_R = |
| 37 | + // /^( *> *\[!(NOTE|IMPORTANT|WARNING)\] *\n(?: *>.*(?:\n|$))+)/m |
| 38 | + // /^( *> *\[!(?:(?<type>NOTE|IMPORTANT|WARNING))\](?<body>.*(\n *>.*?)*))(?=\n *> *\[(NOTE|IMPORTANT|WARNING)\]|$)/ |
| 39 | + // /^(> *\[!(?<type>NOTE|IMPORTANT|WARNING)\](?<body>(?:\n?.*?)*))((?:\n{2,})|$)/ |
| 40 | + // /^*> *\[!(?<type>NOTE|IMPORTANT|WARNING)\](?<body>(?:\n? *>.*?)*?)(?=\n{2,}|$)/ |
| 41 | + // /^( *> *\[!(?<type>NOTE|IMPORTANT|WARNING)\].*?(?:\n(?! *> *\[(NOTE|IMPORTANT|WARNING)\]).*?)*)/ |
| 42 | + |
| 43 | + // /^(> \[!(?<type>NOTE|IMPORTANT|WARNING)\])(?<body>(?:\n? *>.*?(?!\n *> *\[(?:NOTE|IMPORTANT|WARNING)\]))*)/ |
| 44 | + /^(> \[!(?<type>NOTE|IMPORTANT|WARNING)\].*?)(?<body>(?:\n *>.*?)*)(?=\n{2,}|$)/ |
| 45 | + |
| 46 | +export const AlertsRule: MarkdownToJSX.Rule = { |
| 47 | + match: blockRegex(ALERT_BLOCKQUOTE_R), |
| 48 | + order: Priority.HIGH, |
| 49 | + parse(capture) { |
| 50 | + return { |
| 51 | + raw: capture[0], |
| 52 | + parsed: { |
| 53 | + ...capture.groups, |
| 54 | + }, |
| 55 | + } |
| 56 | + }, |
| 57 | + react(node, output, state) { |
| 58 | + const { type, body } = node.parsed |
| 59 | + const bodyClean = body.replace(/^> */gm, '') |
| 60 | + |
| 61 | + const typePrefix = type[0] + type.toLowerCase().slice(1) |
| 62 | + |
| 63 | + const Icon = typedIconMap[type] || typedIconMap.info |
| 64 | + return ( |
| 65 | + <blockquote className={clsx(borderColorMap[type], 'not-italic')}> |
| 66 | + <span |
| 67 | + className={clsx( |
| 68 | + 'text-semibold mb-1 inline-flex items-center', |
| 69 | + textColorMap[type], |
| 70 | + )} |
| 71 | + > |
| 72 | + <Icon |
| 73 | + className={clsx( |
| 74 | + `flex-shrink-0 text-3xl md:mr-2 md:self-start md:text-left`, |
| 75 | + typedIconMap[type] || typedIconMap.info, |
| 76 | + )} |
| 77 | + /> |
| 78 | + |
| 79 | + {typePrefix} |
| 80 | + </span> |
| 81 | + <br /> |
| 82 | + |
| 83 | + <Markdown |
| 84 | + allowsScript |
| 85 | + className="not-prose w-full [&>p:first-child]:mt-0" |
| 86 | + > |
| 87 | + {bodyClean} |
| 88 | + </Markdown> |
| 89 | + </blockquote> |
| 90 | + ) |
| 91 | + }, |
| 92 | +} |
0 commit comments