|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { load } from 'cheerio' |
| 3 | + |
| 4 | +import { renderContent } from '@/content-render/index' |
| 5 | + |
| 6 | +const flowchart = ` |
| 7 | +\`\`\`mermaid |
| 8 | +flowchart LR |
| 9 | + accTitle: Publishing flow |
| 10 | + accDescr: A draft goes to review, then gets published |
| 11 | + A[Draft] --> B[Review] --> C[Published] |
| 12 | +\`\`\` |
| 13 | +` |
| 14 | + |
| 15 | +describe('mermaid', () => { |
| 16 | + test('renders a mermaid fence to inline SVG', async () => { |
| 17 | + const $ = load(await renderContent(flowchart)) |
| 18 | + const svg = $('.mermaid-diagram > svg.pele') |
| 19 | + expect(svg.length).toBe(1) |
| 20 | + expect(svg.attr('viewBox')).toMatch(/^0 0 \d+/) |
| 21 | + expect(svg.find('title').text()).toBe('Publishing flow') |
| 22 | + expect($('pre').length).toBe(0) |
| 23 | + }) |
| 24 | + |
| 25 | + test('flips wide flowcharts to vertical so they fit the article column', async () => { |
| 26 | + const wide = Array.from({ length: 12 }, (_, i) => ` N${i}[Step number ${i}]`).join(' -->\n') |
| 27 | + const $ = load(await renderContent(`\`\`\`mermaid\nflowchart LR\n${wide}\n\`\`\``)) |
| 28 | + const [, , width] = ($('svg.pele').attr('viewBox') || '').split(' ').map(Number) |
| 29 | + expect(width).toBeLessThanOrEqual(720) |
| 30 | + }) |
| 31 | + |
| 32 | + test('keeps the source direction for flowcharts that fit', async () => { |
| 33 | + const $ = load(await renderContent(flowchart)) |
| 34 | + const [, , width, height] = ($('svg.pele').attr('viewBox') || '').split(' ').map(Number) |
| 35 | + expect(width).toBeGreaterThan(height) |
| 36 | + }) |
| 37 | + |
| 38 | + test('gives each diagram distinct accessibility IDs', async () => { |
| 39 | + const $ = load(await renderContent(`${flowchart}\n${flowchart}`)) |
| 40 | + const ids = $('svg.pele title') |
| 41 | + .map((_, el) => $(el).attr('id')) |
| 42 | + .get() |
| 43 | + expect(ids.length).toBe(2) |
| 44 | + expect(new Set(ids).size).toBe(2) |
| 45 | + }) |
| 46 | + |
| 47 | + test('keeps mermaid source nested in a text fence as code', async () => { |
| 48 | + const $ = load(await renderContent(`\`\`\`\`text\n${flowchart}\n\`\`\`\``)) |
| 49 | + expect($('svg.pele').length).toBe(0) |
| 50 | + expect($('pre').text()).toContain('flowchart LR') |
| 51 | + }) |
| 52 | + |
| 53 | + test('keeps invalid diagrams as code blocks', async () => { |
| 54 | + const $ = load(await renderContent('```mermaid\nflowchart LR\n A -->\n```')) |
| 55 | + expect($('svg.pele').length).toBe(0) |
| 56 | + expect($('pre code.language-mermaid').text()).toContain('A -->') |
| 57 | + }) |
| 58 | + |
| 59 | + test('renders hexagon nodes escaped from Liquid', async () => { |
| 60 | + const source = 'flowchart LR\n A{% raw %}{{Decide}}{% endraw %} --> B[Ship]' |
| 61 | + const $ = load(await renderContent(`\`\`\`mermaid\n${source}\n\`\`\``)) |
| 62 | + expect($('svg.pele').length).toBe(1) |
| 63 | + expect($('svg.pele').text()).toContain('Decide') |
| 64 | + }) |
| 65 | + |
| 66 | + test('does not render images inside diagrams', async () => { |
| 67 | + const source = 'flowchart LR\n A@{ img: "https://example.com/logo.png", label: "Logo" }' |
| 68 | + const $ = load(await renderContent(`\`\`\`mermaid\n${source}\n\`\`\``)) |
| 69 | + expect($('svg.pele').length).toBe(1) |
| 70 | + expect($('image').length).toBe(0) |
| 71 | + }) |
| 72 | + |
| 73 | + test('does not render links inside diagrams', async () => { |
| 74 | + const source = 'flowchart LR\n A[Docs]\n click A "https://example.com"' |
| 75 | + const $ = load(await renderContent(`\`\`\`mermaid\n${source}\n\`\`\``)) |
| 76 | + expect($('svg.pele').length).toBe(1) |
| 77 | + expect($('svg.pele a').length).toBe(0) |
| 78 | + }) |
| 79 | +}) |
0 commit comments