|
| 1 | +/** |
| 2 | + * Diagnostic test to reproduce the 3-block code-tabs grouping bug. |
| 3 | + * Run with: npx vitest run src/lib/markdown/plugins/rehype-code-tabs/__tests__/three-block-repro.spec.ts |
| 4 | + */ |
| 5 | +import { describe, it, expect } from 'vitest' |
| 6 | +import type { Root, Element } from 'hast' |
| 7 | +import { remark } from 'remark' |
| 8 | +import remarkRehype from 'remark-rehype' |
| 9 | +import rehypeStringify from 'rehype-stringify' |
| 10 | +import remarkCodeTabs from '@lib/markdown/plugins/remark-code-tabs' |
| 11 | +import rehypeCodeTabs from '@lib/markdown/plugins/rehype-code-tabs' |
| 12 | +import { unified } from 'unified' |
| 13 | + |
| 14 | +describe('3-block code-tabs grouping', () => { |
| 15 | + it('groups 3 consecutive same-group blocks (remark pipeline)', async () => { |
| 16 | + const md = [ |
| 17 | + '```typescript [g1:TypeScript]', |
| 18 | + 'const x = 1', |
| 19 | + '```', |
| 20 | + '```php [g1:PHP]', |
| 21 | + '<?php echo 1;', |
| 22 | + '```', |
| 23 | + '```python [g1:Python]', |
| 24 | + 'print(1)', |
| 25 | + '```', |
| 26 | + ].join('\n') |
| 27 | + |
| 28 | + const html = String( |
| 29 | + await remark() |
| 30 | + .use(remarkCodeTabs) |
| 31 | + .use(remarkRehype) |
| 32 | + .use(rehypeCodeTabs) |
| 33 | + .use(rehypeStringify) |
| 34 | + .process(md) |
| 35 | + ) |
| 36 | + |
| 37 | + console.log('HTML output:', html) |
| 38 | + |
| 39 | + // All 3 should be inside a single <code-tabs> |
| 40 | + const codeTabsCount = (html.match(/<code-tabs/g) || []).length |
| 41 | + expect(codeTabsCount, 'Should have exactly 1 <code-tabs> wrapper').toBe(1) |
| 42 | + }) |
| 43 | + |
| 44 | + it('groups 3 consecutive same-group HAST blocks directly', async () => { |
| 45 | + const makePre = (tab: string): Element => ({ |
| 46 | + type: 'element', |
| 47 | + tagName: 'pre', |
| 48 | + properties: {}, |
| 49 | + children: [{ |
| 50 | + type: 'element', |
| 51 | + tagName: 'code', |
| 52 | + properties: { |
| 53 | + 'data-code-tabs-group': 'g1', |
| 54 | + 'data-code-tabs-tab': tab, |
| 55 | + }, |
| 56 | + children: [{ type: 'text', value: `code for ${tab}` }], |
| 57 | + }], |
| 58 | + }) |
| 59 | + |
| 60 | + const tree: Root = { |
| 61 | + type: 'root', |
| 62 | + children: [ |
| 63 | + makePre('TypeScript'), |
| 64 | + { type: 'text', value: '\n' }, |
| 65 | + makePre('PHP'), |
| 66 | + { type: 'text', value: '\n' }, |
| 67 | + makePre('Python'), |
| 68 | + ], |
| 69 | + } |
| 70 | + |
| 71 | + const processor = unified().use(rehypeCodeTabs) |
| 72 | + await processor.run(tree as never) |
| 73 | + |
| 74 | + const elements = tree.children.filter( |
| 75 | + c => (c as Element).type === 'element' |
| 76 | + ) as Element[] |
| 77 | + |
| 78 | + console.log('Elements:', elements.map(e => `${e.tagName}(${e.children?.length})`)) |
| 79 | + |
| 80 | + expect(elements.length, 'Should have exactly 1 top-level element').toBe(1) |
| 81 | + expect(elements[0].tagName).toBe('code-tabs') |
| 82 | + expect(elements[0].children.length, 'code-tabs should contain all 3 pre blocks').toBe(3) |
| 83 | + }) |
| 84 | + |
| 85 | + it('unwraps figure-wrapped grouped code blocks and groups them', async () => { |
| 86 | + const makePre = (tab: string, lang: string): Element => ({ |
| 87 | + type: 'element', |
| 88 | + tagName: 'pre', |
| 89 | + properties: {}, |
| 90 | + children: [{ |
| 91 | + type: 'element', |
| 92 | + tagName: 'code', |
| 93 | + properties: { |
| 94 | + className: [`language-${lang}`], |
| 95 | + 'data-code-tabs-group': 'g1', |
| 96 | + 'data-code-tabs-tab': tab, |
| 97 | + }, |
| 98 | + children: [{ type: 'text', value: `code for ${tab}` }], |
| 99 | + }], |
| 100 | + }) |
| 101 | + |
| 102 | + // Simulate remark-captions wrapping the 3rd code block in <figure> |
| 103 | + const tree: Root = { |
| 104 | + type: 'root', |
| 105 | + children: [ |
| 106 | + makePre('TypeScript', 'typescript'), |
| 107 | + { type: 'text', value: '\n' }, |
| 108 | + makePre('PHP', 'php'), |
| 109 | + { type: 'text', value: '\n' }, |
| 110 | + { |
| 111 | + type: 'element', |
| 112 | + tagName: 'figure', |
| 113 | + properties: {}, |
| 114 | + children: [ |
| 115 | + makePre('Python', 'python'), |
| 116 | + { |
| 117 | + type: 'element', |
| 118 | + tagName: 'figcaption', |
| 119 | + properties: {}, |
| 120 | + children: [{ type: 'text', value: 'Different admission control strategies.' }], |
| 121 | + }, |
| 122 | + ], |
| 123 | + } as Element, |
| 124 | + ], |
| 125 | + } |
| 126 | + |
| 127 | + const processor = unified().use(rehypeCodeTabs) |
| 128 | + await processor.run(tree as never) |
| 129 | + |
| 130 | + const elements = tree.children.filter( |
| 131 | + c => (c as Element).type === 'element' |
| 132 | + ) as Element[] |
| 133 | + |
| 134 | + expect(elements.length, 'Should have exactly 1 top-level element').toBe(1) |
| 135 | + expect(elements[0].tagName).toBe('code-tabs') |
| 136 | + expect(elements[0].children.length, 'code-tabs should contain all 3 pre blocks').toBe(3) |
| 137 | + // Verify all children are <pre> (not figure) |
| 138 | + expect(elements[0].children.every( |
| 139 | + (c: Element) => c.tagName === 'pre' |
| 140 | + )).toBe(true) |
| 141 | + }) |
| 142 | + |
| 143 | + it('does NOT unwrap figures containing non-grouped code blocks', async () => { |
| 144 | + const tree: Root = { |
| 145 | + type: 'root', |
| 146 | + children: [ |
| 147 | + { |
| 148 | + type: 'element', |
| 149 | + tagName: 'figure', |
| 150 | + properties: {}, |
| 151 | + children: [ |
| 152 | + { |
| 153 | + type: 'element', |
| 154 | + tagName: 'pre', |
| 155 | + properties: {}, |
| 156 | + children: [{ |
| 157 | + type: 'element', |
| 158 | + tagName: 'code', |
| 159 | + properties: { className: ['language-python'] }, |
| 160 | + children: [{ type: 'text', value: 'print(1)' }], |
| 161 | + }], |
| 162 | + }, |
| 163 | + { |
| 164 | + type: 'element', |
| 165 | + tagName: 'figcaption', |
| 166 | + properties: {}, |
| 167 | + children: [{ type: 'text', value: 'A standalone caption.' }], |
| 168 | + }, |
| 169 | + ], |
| 170 | + } as Element, |
| 171 | + ], |
| 172 | + } |
| 173 | + |
| 174 | + const processor = unified().use(rehypeCodeTabs) |
| 175 | + await processor.run(tree as never) |
| 176 | + |
| 177 | + const elements = tree.children.filter( |
| 178 | + c => (c as Element).type === 'element' |
| 179 | + ) as Element[] |
| 180 | + |
| 181 | + // The figure should be left alone (not unwrapped) |
| 182 | + // The pre inside gets standalone-wrapped in code-tabs |
| 183 | + expect(elements.length).toBe(1) |
| 184 | + // The figure stays as-is, and the standalone wrapping happens inside it |
| 185 | + expect(elements[0].tagName).toBe('figure') |
| 186 | + }) |
| 187 | +}) |
0 commit comments