|
| 1 | +/** |
| 2 | + * Test custom class prefix option |
| 3 | + */ |
| 4 | + |
| 5 | +import { test } from 'node:test' |
| 6 | +import assert from 'node:assert/strict' |
| 7 | +import { unified } from 'unified' |
| 8 | +import remarkParse from 'remark-parse' |
| 9 | +import remarkRehype from 'remark-rehype' |
| 10 | +import rehypeRaw from 'rehype-raw' |
| 11 | +import rehypeStringify from 'rehype-stringify' |
| 12 | +import remarkNotes from '../index.js' |
| 13 | + |
| 14 | +test('remark-notes: custom classPrefix should apply to all elements', async () => { |
| 15 | + const markdown = '> [!tip]\n> This is a tip' |
| 16 | + |
| 17 | + const file = await unified() |
| 18 | + .use(remarkParse) |
| 19 | + .use(remarkNotes, { classPrefix: 'my' }) |
| 20 | + .use(remarkRehype, { allowDangerousHtml: true }) |
| 21 | + .use(rehypeRaw) |
| 22 | + .use(rehypeStringify) |
| 23 | + .process(markdown) |
| 24 | + |
| 25 | + const output = String(file) |
| 26 | + |
| 27 | + // Container should have both base and type modifier classes with prefix prepended |
| 28 | + assert.ok(output.includes('class="my-remark-note my-remark-note-tip"'), |
| 29 | + 'Container should have prefix prepended to standard remark-note classes') |
| 30 | + |
| 31 | + // Header, icon, title, and content should all use the prefix prepended to remark-note |
| 32 | + assert.ok(output.includes('class="my-remark-note-header"'), |
| 33 | + 'Header should use prefix-remark-note-header pattern') |
| 34 | + assert.ok(output.includes('class="my-remark-note-icon"'), |
| 35 | + 'Icon should use prefix-remark-note-icon pattern') |
| 36 | + assert.ok(output.includes('class="my-remark-note-title"'), |
| 37 | + 'Title should use prefix-remark-note-title pattern') |
| 38 | + assert.ok(output.includes('class="my-remark-note-content"'), |
| 39 | + 'Content should use prefix-remark-note-content pattern') |
| 40 | + |
| 41 | + // Should still contain 'remark-note' as part of the class names |
| 42 | + assert.ok(output.includes('remark-note'), |
| 43 | + 'Should contain remark-note as part of class names') |
| 44 | +}) |
| 45 | + |
| 46 | +test('remark-notes: all note types work with custom prefix', async () => { |
| 47 | + const noteTypes = ['note', 'tip', 'important', 'quote', 'bonus'] |
| 48 | + const customPrefix = 'custom' |
| 49 | + |
| 50 | + for (const type of noteTypes) { |
| 51 | + const markdown = `> [!${type}]\n> Test ${type}` |
| 52 | + |
| 53 | + const file = await unified() |
| 54 | + .use(remarkParse) |
| 55 | + .use(remarkNotes, { classPrefix: customPrefix }) |
| 56 | + .use(remarkRehype, { allowDangerousHtml: true }) |
| 57 | + .use(rehypeRaw) |
| 58 | + .use(rehypeStringify) |
| 59 | + .process(markdown) |
| 60 | + |
| 61 | + const output = String(file) |
| 62 | + |
| 63 | + // Check for prefix prepended to remark-note in container classes |
| 64 | + assert.ok( |
| 65 | + output.includes(`class="${customPrefix}-remark-note ${customPrefix}-remark-note-${type}"`), |
| 66 | + `${type} should have prefix prepended: ${customPrefix}-remark-note ${customPrefix}-remark-note-${type}` |
| 67 | + ) |
| 68 | + |
| 69 | + // Check for prefix prepended to remark-note in all sub-elements |
| 70 | + assert.ok(output.includes(`class="${customPrefix}-remark-note-header"`), |
| 71 | + `${type} header should use ${customPrefix}-remark-note-header`) |
| 72 | + assert.ok(output.includes(`class="${customPrefix}-remark-note-icon"`), |
| 73 | + `${type} icon should use ${customPrefix}-remark-note-icon`) |
| 74 | + assert.ok(output.includes(`class="${customPrefix}-remark-note-title"`), |
| 75 | + `${type} title should use ${customPrefix}-remark-note-title`) |
| 76 | + assert.ok(output.includes(`class="${customPrefix}-remark-note-content"`), |
| 77 | + `${type} content should use ${customPrefix}-remark-note-content`) |
| 78 | + } |
| 79 | +}) |
| 80 | + |
| 81 | +test('remark-notes: injectStyles option controls style injection', async () => { |
| 82 | + const markdown = '> [!note]\n> Test note' |
| 83 | + |
| 84 | + // Test with injectStyles: true (default) |
| 85 | + const fileWithStyles = await unified() |
| 86 | + .use(remarkParse) |
| 87 | + .use(remarkNotes, { injectStyles: true }) |
| 88 | + .use(remarkRehype, { allowDangerousHtml: true }) |
| 89 | + .use(rehypeStringify, { allowDangerousHtml: true }) |
| 90 | + .process(markdown) |
| 91 | + |
| 92 | + const outputWithStyles = String(fileWithStyles) |
| 93 | + assert.ok(outputWithStyles.includes('<style>'), |
| 94 | + 'Styles should be injected when injectStyles is true') |
| 95 | + |
| 96 | + // Test with injectStyles: false |
| 97 | + const fileWithoutStyles = await unified() |
| 98 | + .use(remarkParse) |
| 99 | + .use(remarkNotes, { injectStyles: false }) |
| 100 | + .use(remarkRehype, { allowDangerousHtml: true }) |
| 101 | + .use(rehypeStringify, { allowDangerousHtml: true }) |
| 102 | + .process(markdown) |
| 103 | + |
| 104 | + const outputWithoutStyles = String(fileWithoutStyles) |
| 105 | + assert.ok(!outputWithoutStyles.includes('<style>'), |
| 106 | + 'Styles should NOT be injected when injectStyles is false') |
| 107 | +}) |
| 108 | + |
| 109 | +test('remark-notes: combining custom prefix with injectStyles false', async () => { |
| 110 | + const markdown = '> [!important]\n> Important message' |
| 111 | + |
| 112 | + const file = await unified() |
| 113 | + .use(remarkParse) |
| 114 | + .use(remarkNotes, { |
| 115 | + classPrefix: 'alert', |
| 116 | + injectStyles: false |
| 117 | + }) |
| 118 | + .use(remarkRehype, { allowDangerousHtml: true }) |
| 119 | + .use(rehypeRaw) |
| 120 | + .use(rehypeStringify) |
| 121 | + .process(markdown) |
| 122 | + |
| 123 | + const output = String(file) |
| 124 | + |
| 125 | + // Should have prefix prepended to remark-note classes |
| 126 | + assert.ok(output.includes('class="alert-remark-note alert-remark-note-important"'), |
| 127 | + 'Should use prefix prepended to remark-note classes') |
| 128 | + |
| 129 | + // Should NOT have styles |
| 130 | + assert.ok(!output.includes('<style>'), |
| 131 | + 'Should not inject styles') |
| 132 | +}) |
| 133 | + |
| 134 | +console.log('\n✅ All custom prefix tests completed!') |
0 commit comments