|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | + |
| 4 | +import { u } from 'unist-builder'; |
| 5 | + |
| 6 | +import transformAlerts from '../alerts.mjs'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Builds a blockquote whose first paragraph leads with `markerLine`, mimicking |
| 10 | + * how remark-parse represents `> [!NOTE]\n> body` (a soft break is a `\n` |
| 11 | + * inside the leading text node). |
| 12 | + */ |
| 13 | +const makeTree = (markerLine, body = 'Body text.') => |
| 14 | + u('root', [ |
| 15 | + u('blockquote', [u('paragraph', [u('text', `${markerLine}\n${body}`)])]), |
| 16 | + ]); |
| 17 | + |
| 18 | +const getAttr = (node, name) => |
| 19 | + node.attributes.find(attr => attr.name === name)?.value; |
| 20 | + |
| 21 | +describe('transformAlerts', () => { |
| 22 | + for (const [marker, level, title] of [ |
| 23 | + ['[!NOTE]', 'neutral', 'Note'], |
| 24 | + ['[!TIP]', 'success', 'Tip'], |
| 25 | + ['[!IMPORTANT]', 'info', 'Important'], |
| 26 | + ['[!WARNING]', 'warning', 'Warning'], |
| 27 | + ['[!CAUTION]', 'danger', 'Caution'], |
| 28 | + ]) { |
| 29 | + it(`maps ${marker} to an AlertBox (${level})`, () => { |
| 30 | + const tree = makeTree(marker); |
| 31 | + |
| 32 | + transformAlerts()(tree); |
| 33 | + |
| 34 | + const alert = tree.children[0]; |
| 35 | + |
| 36 | + assert.equal(alert.type, 'mdxJsxFlowElement'); |
| 37 | + assert.equal(alert.name, 'AlertBox'); |
| 38 | + assert.equal(getAttr(alert, 'level'), level); |
| 39 | + assert.equal(getAttr(alert, 'title'), title); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + it('strips the marker but keeps the alert body', () => { |
| 44 | + const tree = makeTree('[!NOTE]', 'Highlights information.'); |
| 45 | + |
| 46 | + transformAlerts()(tree); |
| 47 | + |
| 48 | + const text = tree.children[0].children[0].children[0]; |
| 49 | + |
| 50 | + assert.equal(text.type, 'text'); |
| 51 | + assert.equal(text.value, 'Highlights information.'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('drops the leading paragraph when the marker is alone', () => { |
| 55 | + const tree = u('root', [ |
| 56 | + u('blockquote', [ |
| 57 | + u('paragraph', [u('text', '[!TIP]')]), |
| 58 | + u('paragraph', [u('text', 'Second paragraph.')]), |
| 59 | + ]), |
| 60 | + ]); |
| 61 | + |
| 62 | + transformAlerts()(tree); |
| 63 | + |
| 64 | + const alert = tree.children[0]; |
| 65 | + |
| 66 | + assert.equal(alert.children.length, 1); |
| 67 | + assert.equal(alert.children[0].children[0].value, 'Second paragraph.'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('ignores blockquotes without an alert marker', () => { |
| 71 | + const tree = u('root', [ |
| 72 | + u('blockquote', [u('paragraph', [u('text', 'Just a quote.')])]), |
| 73 | + ]); |
| 74 | + |
| 75 | + transformAlerts()(tree); |
| 76 | + |
| 77 | + assert.equal(tree.children[0].type, 'blockquote'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('ignores unknown markers', () => { |
| 81 | + const tree = makeTree('[!FOOBAR]'); |
| 82 | + |
| 83 | + transformAlerts()(tree); |
| 84 | + |
| 85 | + assert.equal(tree.children[0].type, 'blockquote'); |
| 86 | + }); |
| 87 | +}); |
0 commit comments