|
| 1 | +import rehypeParse from 'rehype-parse'; |
| 2 | +import rehypeRemark from 'rehype-remark'; |
| 3 | +import remarkGfm from 'remark-gfm'; |
| 4 | +import remarkStringify from 'remark-stringify'; |
| 5 | +import {describe, expect, it} from 'vitest'; |
| 6 | +import {unified} from 'unified'; |
| 7 | +import {remove} from 'unist-util-remove'; |
| 8 | + |
| 9 | +import {rehypeExpandCodeTabs} from './rehype-expand-code-tabs.mjs'; |
| 10 | + |
| 11 | +function htmlToMarkdown(html) { |
| 12 | + return String( |
| 13 | + unified() |
| 14 | + .use(rehypeParse) |
| 15 | + .use(rehypeExpandCodeTabs) |
| 16 | + .use(rehypeRemark, { |
| 17 | + document: false, |
| 18 | + handlers: { |
| 19 | + button() {}, |
| 20 | + }, |
| 21 | + }) |
| 22 | + .use(() => tree => remove(tree, {type: 'inlineCode', value: ''})) |
| 23 | + .use(remarkGfm) |
| 24 | + .use(remarkStringify) |
| 25 | + .processSync(html) |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +function buildCodeTabsHTML(tabs) { |
| 30 | + const firstTab = tabs[0]; |
| 31 | + |
| 32 | + const codeTabsRendered = |
| 33 | + '<div>' + |
| 34 | + tabs.map((t, i) => `<button data-active="${i === 0}">${t.title}</button>`).join('') + |
| 35 | + '<div class="code-block">' + |
| 36 | + `<code class="filename">${firstTab.filename || ''}</code>` + |
| 37 | + `<pre class="language-${firstTab.lang}"><code>${firstTab.code}</code></pre>` + |
| 38 | + '</div>' + |
| 39 | + '</div>'; |
| 40 | + |
| 41 | + const exportBlocks = tabs |
| 42 | + .map(t => { |
| 43 | + const filenameAttr = t.filename ? ` data-code-tab-filename="${t.filename}"` : ''; |
| 44 | + return ( |
| 45 | + `<div hidden data-code-tab-title="${t.title}"${filenameAttr}>` + |
| 46 | + `<pre class="language-${t.lang}"><code>${t.code}</code></pre>` + |
| 47 | + '</div>' |
| 48 | + ); |
| 49 | + }) |
| 50 | + .join(''); |
| 51 | + |
| 52 | + return `<div class="code-tabs-wrapper">${codeTabsRendered}${exportBlocks}</div>`; |
| 53 | +} |
| 54 | + |
| 55 | +describe('rehypeExpandCodeTabs', () => { |
| 56 | + it('outputs one fenced code block per tab with "[Title] filename" headings', () => { |
| 57 | + const html = buildCodeTabsHTML([ |
| 58 | + { |
| 59 | + title: 'Cloudflare Workers', |
| 60 | + filename: 'index.ts', |
| 61 | + lang: 'typescript', |
| 62 | + code: 'import { sentry } from "@sentry/hono/cloudflare";', |
| 63 | + }, |
| 64 | + { |
| 65 | + title: 'Node.js', |
| 66 | + filename: 'app.ts', |
| 67 | + lang: 'typescript', |
| 68 | + code: 'import { sentry } from "@sentry/hono/node";', |
| 69 | + }, |
| 70 | + { |
| 71 | + title: 'Bun', |
| 72 | + filename: 'index.ts', |
| 73 | + lang: 'typescript', |
| 74 | + code: 'import { sentry } from "@sentry/hono/bun";', |
| 75 | + }, |
| 76 | + ]); |
| 77 | + |
| 78 | + const md = htmlToMarkdown(html); |
| 79 | + |
| 80 | + const codeBlocks = md.match(/```[\s\S]*?```/g); |
| 81 | + expect(codeBlocks).toHaveLength(3); |
| 82 | + expect(codeBlocks[0]).toContain('@sentry/hono/cloudflare'); |
| 83 | + expect(codeBlocks[1]).toContain('@sentry/hono/node'); |
| 84 | + expect(codeBlocks[2]).toContain('@sentry/hono/bun'); |
| 85 | + expect(md).toContain('**\\[Cloudflare Workers] index.ts**'); |
| 86 | + expect(md).toContain('**\\[Node.js] app.ts**'); |
| 87 | + expect(md).toContain('**\\[Bun] index.ts**'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('removes the CodeTabs-rendered active tab to avoid duplication', () => { |
| 91 | + const html = buildCodeTabsHTML([ |
| 92 | + {title: 'ESM', filename: 'instrument.mjs', lang: 'javascript', code: 'import init'}, |
| 93 | + {title: 'CJS', filename: 'instrument.js', lang: 'javascript', code: 'require init'}, |
| 94 | + ]); |
| 95 | + |
| 96 | + const md = htmlToMarkdown(html); |
| 97 | + |
| 98 | + expect(md).not.toContain('`instrument.mjs`'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('uses tab title alone when filename is absent', () => { |
| 102 | + const html = buildCodeTabsHTML([ |
| 103 | + {title: 'Cloudflare Workers', lang: 'javascript', code: 'workers();'}, |
| 104 | + {title: 'Bun', lang: 'javascript', code: 'bun();'}, |
| 105 | + ]); |
| 106 | + |
| 107 | + const md = htmlToMarkdown(html); |
| 108 | + |
| 109 | + const headings = md.match(/\*\*.*?\*\*/g); |
| 110 | + expect(headings).toHaveLength(2); |
| 111 | + expect(headings[0]).toBe('**Cloudflare Workers**'); |
| 112 | + expect(headings[1]).toBe('**Bun**'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('treats empty filename attribute the same as missing filename', () => { |
| 116 | + const html = |
| 117 | + '<div class="code-tabs-wrapper">' + |
| 118 | + '<div><button>Tab</button><div class="code-block"><code class="filename"></code>' + |
| 119 | + '<pre class="language-js"><code>active()</code></pre></div></div>' + |
| 120 | + '<div hidden data-code-tab-title="JavaScript" data-code-tab-filename="">' + |
| 121 | + '<pre class="language-js"><code>hello();</code></pre></div>' + |
| 122 | + '</div>'; |
| 123 | + |
| 124 | + const md = htmlToMarkdown(html); |
| 125 | + |
| 126 | + const headings = md.match(/\*\*.*?\*\*/g); |
| 127 | + expect(headings).toHaveLength(1); |
| 128 | + expect(headings[0]).toBe('**JavaScript**'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('does not modify code blocks outside tab wrappers', () => { |
| 132 | + const html = |
| 133 | + '<div><pre class="language-bash"><code>curl -sL https://sentry.io/get-cli/ | bash</code></pre></div>'; |
| 134 | + |
| 135 | + const md = htmlToMarkdown(html); |
| 136 | + |
| 137 | + const codeBlocks = md.match(/```[\s\S]*?```/g); |
| 138 | + expect(codeBlocks).toHaveLength(1); |
| 139 | + expect(codeBlocks[0]).toContain('curl -sL'); |
| 140 | + expect(md).not.toMatch(/\*\*.*\*\*\n/); |
| 141 | + }); |
| 142 | + |
| 143 | + it('preserves standalone code blocks when mixed with tab groups', () => { |
| 144 | + const standalone = |
| 145 | + '<pre class="language-bash"><code>npm install @sentry/node</code></pre>'; |
| 146 | + const tabs = buildCodeTabsHTML([ |
| 147 | + { |
| 148 | + title: 'Node.js', |
| 149 | + filename: 'instrument.mjs', |
| 150 | + lang: 'javascript', |
| 151 | + code: 'Sentry.init();', |
| 152 | + }, |
| 153 | + {title: 'Bun', lang: 'javascript', code: 'init();'}, |
| 154 | + ]); |
| 155 | + |
| 156 | + const md = htmlToMarkdown(`<div>${standalone}${tabs}</div>`); |
| 157 | + |
| 158 | + const codeBlocks = md.match(/```[\s\S]*?```/g); |
| 159 | + expect(codeBlocks).toHaveLength(3); |
| 160 | + expect(codeBlocks[0]).toContain('npm install'); |
| 161 | + expect(md).toContain('**\\[Node.js] instrument.mjs**'); |
| 162 | + expect(md).toContain('**Bun**'); |
| 163 | + }); |
| 164 | + |
| 165 | + it('expands multiple tab groups independently on the same page', () => { |
| 166 | + const group1 = buildCodeTabsHTML([ |
| 167 | + {title: 'ESM', filename: 'instrument.mjs', lang: 'javascript', code: 'import init'}, |
| 168 | + {title: 'CJS', filename: 'instrument.js', lang: 'javascript', code: 'require init'}, |
| 169 | + ]); |
| 170 | + const group2 = buildCodeTabsHTML([ |
| 171 | + {title: 'Python', filename: 'main.py', lang: 'python', code: 'import sentry_sdk'}, |
| 172 | + {title: 'Ruby', filename: 'config.rb', lang: 'ruby', code: 'require "sentry-ruby"'}, |
| 173 | + ]); |
| 174 | + |
| 175 | + const md = htmlToMarkdown(`<div>${group1}${group2}</div>`); |
| 176 | + |
| 177 | + const codeBlocks = md.match(/```[\s\S]*?```/g); |
| 178 | + expect(codeBlocks).toHaveLength(4); |
| 179 | + expect(codeBlocks[0]).toContain('import init'); |
| 180 | + expect(codeBlocks[1]).toContain('require init'); |
| 181 | + expect(codeBlocks[2]).toContain('import sentry_sdk'); |
| 182 | + expect(codeBlocks[3]).toContain('require "sentry-ruby"'); |
| 183 | + }); |
| 184 | + |
| 185 | + it('drops export blocks that contain no pre element', () => { |
| 186 | + const html = |
| 187 | + '<div class="code-tabs-wrapper">' + |
| 188 | + '<div><pre class="language-js"><code>active tab</code></pre></div>' + |
| 189 | + '<div hidden data-code-tab-title="broken"><p>Not a code block</p></div>' + |
| 190 | + '<div hidden data-code-tab-title="ok"><pre class="language-js"><code>works();</code></pre></div>' + |
| 191 | + '</div>'; |
| 192 | + |
| 193 | + const md = htmlToMarkdown(html); |
| 194 | + |
| 195 | + const codeBlocks = md.match(/```[\s\S]*?```/g); |
| 196 | + expect(codeBlocks).toHaveLength(1); |
| 197 | + expect(codeBlocks[0]).toContain('works()'); |
| 198 | + expect(md).toContain('**ok**'); |
| 199 | + expect(md).not.toContain('broken'); |
| 200 | + expect(md).not.toContain('active tab'); |
| 201 | + }); |
| 202 | + |
| 203 | + it('leaves wrapper unchanged when it has no export blocks', () => { |
| 204 | + const html = |
| 205 | + '<div class="code-tabs-wrapper">' + |
| 206 | + '<div><button>Only Tab</button>' + |
| 207 | + '<div class="code-block"><pre class="language-js"><code>solo();</code></pre></div>' + |
| 208 | + '</div>' + |
| 209 | + '</div>'; |
| 210 | + |
| 211 | + const md = htmlToMarkdown(html); |
| 212 | + |
| 213 | + const codeBlocks = md.match(/```[\s\S]*?```/g); |
| 214 | + expect(codeBlocks).toHaveLength(1); |
| 215 | + expect(codeBlocks[0]).toContain('solo()'); |
| 216 | + }); |
| 217 | +}); |
0 commit comments