|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { mock, it, describe } from 'node:test'; |
| 3 | + |
| 4 | +import { SKIP } from 'unist-util-visit'; |
| 5 | + |
| 6 | +// Mock dependencies |
| 7 | +mock.module('classNames', { |
| 8 | + defaultExport: (...args) => args.filter(Boolean).join(' '), |
| 9 | +}); |
| 10 | + |
| 11 | +mock.module('hast-util-to-string', { |
| 12 | + namedExports: { |
| 13 | + toString: node => node.children?.[0]?.value || '', |
| 14 | + }, |
| 15 | +}); |
| 16 | + |
| 17 | +mock.module('unist-util-visit', { |
| 18 | + namedExports: { |
| 19 | + visit: (tree, nodeType, visitor) => { |
| 20 | + const traverseNode = (node, index, parent) => { |
| 21 | + if (node.type === nodeType) { |
| 22 | + const result = visitor(node, index, parent); |
| 23 | + if (result?.[0] === SKIP) return true; |
| 24 | + } |
| 25 | + |
| 26 | + if (node.children) { |
| 27 | + for (let i = 0; i < node.children.length; i++) { |
| 28 | + if (traverseNode(node.children[i], i, node)) return true; |
| 29 | + } |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + traverseNode(tree, null, null); |
| 34 | + }, |
| 35 | + SKIP, |
| 36 | + }, |
| 37 | +}); |
| 38 | + |
| 39 | +mock.module('../highlighter', { |
| 40 | + namedExports: { |
| 41 | + highlightToHast: (code, language) => ({ |
| 42 | + children: [ |
| 43 | + { |
| 44 | + type: 'element', |
| 45 | + tagName: 'pre', |
| 46 | + properties: { class: `highlighted-${language}` }, |
| 47 | + children: [ |
| 48 | + { |
| 49 | + type: 'element', |
| 50 | + tagName: 'code', |
| 51 | + properties: {}, |
| 52 | + children: [{ type: 'text', value: `highlighted ${code}` }], |
| 53 | + }, |
| 54 | + ], |
| 55 | + }, |
| 56 | + ], |
| 57 | + }), |
| 58 | + }, |
| 59 | +}); |
| 60 | + |
| 61 | +const { |
| 62 | + getMetaParameter, |
| 63 | + isCodeBlock, |
| 64 | + processCodeTabs, |
| 65 | + processCodeHighlighting, |
| 66 | +} = await import('../shiki'); |
| 67 | + |
| 68 | +describe('rehypeShikiji module', () => { |
| 69 | + describe('Utility functions', () => { |
| 70 | + it('getMetaParameter extracts values from meta strings', () => { |
| 71 | + const testCases = [ |
| 72 | + { |
| 73 | + meta: 'displayName="JavaScript"', |
| 74 | + param: 'displayName', |
| 75 | + expected: 'JavaScript', |
| 76 | + }, |
| 77 | + { |
| 78 | + meta: 'active="true" displayName="TypeScript"', |
| 79 | + param: 'active', |
| 80 | + expected: 'true', |
| 81 | + }, |
| 82 | + { |
| 83 | + meta: 'key="value with spaces"', |
| 84 | + param: 'key', |
| 85 | + expected: 'value with spaces', |
| 86 | + }, |
| 87 | + { meta: 'noQuotes=value', param: 'noQuotes', expected: undefined }, |
| 88 | + { meta: null, param: 'key', expected: undefined }, |
| 89 | + { meta: 'key="value"', param: undefined, expected: undefined }, |
| 90 | + { meta: 'key=""', param: 'key', expected: undefined }, |
| 91 | + { meta: 'otherKey="value"', param: 'key', expected: undefined }, |
| 92 | + ]; |
| 93 | + |
| 94 | + testCases.forEach(({ meta, param, expected }) => { |
| 95 | + assert.equal(getMetaParameter(meta, param), expected); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('isCodeBlock correctly identifies code blocks', () => { |
| 100 | + // Valid code block |
| 101 | + const validBlock = { |
| 102 | + type: 'element', |
| 103 | + tagName: 'pre', |
| 104 | + children: [ |
| 105 | + { |
| 106 | + type: 'element', |
| 107 | + tagName: 'code', |
| 108 | + children: [{ type: 'text', value: 'code' }], |
| 109 | + }, |
| 110 | + ], |
| 111 | + }; |
| 112 | + |
| 113 | + // Invalid cases |
| 114 | + const invalidCases = [ |
| 115 | + { |
| 116 | + type: 'element', |
| 117 | + tagName: 'pre', |
| 118 | + children: [{ type: 'element', tagName: 'span' }], |
| 119 | + }, |
| 120 | + { |
| 121 | + type: 'element', |
| 122 | + tagName: 'div', |
| 123 | + children: [{ type: 'element', tagName: 'code' }], |
| 124 | + }, |
| 125 | + undefined, |
| 126 | + ]; |
| 127 | + |
| 128 | + assert.ok(isCodeBlock(validBlock)); |
| 129 | + invalidCases.forEach(testCase => { |
| 130 | + assert.ok(!isCodeBlock(testCase)); |
| 131 | + }); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('Processing functions', () => { |
| 136 | + it('processCodeTabs groups adjacent code blocks', () => { |
| 137 | + const tree = { |
| 138 | + type: 'root', |
| 139 | + children: [ |
| 140 | + { |
| 141 | + type: 'element', |
| 142 | + tagName: 'pre', |
| 143 | + children: [ |
| 144 | + { |
| 145 | + type: 'element', |
| 146 | + tagName: 'code', |
| 147 | + properties: { className: ['language-javascript'] }, |
| 148 | + data: { meta: 'displayName="JavaScript"' }, |
| 149 | + children: [{ type: 'text', value: 'console.log("hello");' }], |
| 150 | + }, |
| 151 | + ], |
| 152 | + }, |
| 153 | + { |
| 154 | + type: 'element', |
| 155 | + tagName: 'pre', |
| 156 | + children: [ |
| 157 | + { |
| 158 | + type: 'element', |
| 159 | + tagName: 'code', |
| 160 | + properties: { className: ['language-typescript'] }, |
| 161 | + data: { meta: 'displayName="TypeScript" active="true"' }, |
| 162 | + children: [{ type: 'text', value: 'console.log("hello");' }], |
| 163 | + }, |
| 164 | + ], |
| 165 | + }, |
| 166 | + ], |
| 167 | + }; |
| 168 | + |
| 169 | + processCodeTabs(tree); |
| 170 | + |
| 171 | + assert.partialDeepStrictEqual(tree.children[0], { |
| 172 | + tagName: 'CodeTabs', |
| 173 | + properties: { |
| 174 | + languages: 'javascript|typescript', |
| 175 | + displayNames: 'JavaScript|TypeScript', |
| 176 | + defaultTab: '1', |
| 177 | + }, |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + it('processCodeHighlighting applies syntax highlighting', () => { |
| 182 | + const tree = { |
| 183 | + type: 'root', |
| 184 | + children: [ |
| 185 | + { |
| 186 | + type: 'element', |
| 187 | + tagName: 'pre', |
| 188 | + children: [ |
| 189 | + { |
| 190 | + type: 'element', |
| 191 | + tagName: 'code', |
| 192 | + properties: { className: ['language-javascript'] }, |
| 193 | + data: { meta: 'showCopyButton="true"' }, |
| 194 | + children: [{ type: 'text', value: 'console.log("hello");' }], |
| 195 | + }, |
| 196 | + ], |
| 197 | + }, |
| 198 | + ], |
| 199 | + }; |
| 200 | + |
| 201 | + processCodeHighlighting(tree); |
| 202 | + |
| 203 | + assert.partialDeepStrictEqual(tree.children[0], { |
| 204 | + tagName: 'pre', |
| 205 | + properties: { |
| 206 | + class: 'highlighted-javascript language-javascript', |
| 207 | + showCopyButton: 'true', |
| 208 | + }, |
| 209 | + }); |
| 210 | + }); |
| 211 | + }); |
| 212 | +}); |
0 commit comments