|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// v8.94.2 — markdown renderer for how-to guides. |
| 4 | +// |
| 5 | +// Two kinds of coverage: focused cases for each construct, and a corpus pass |
| 6 | +// over all 154 shipped guides. The corpus pass is the one that matters — it is |
| 7 | +// the actual input, and it proves both that markdown is converted and that the |
| 8 | +// HTML already present in 133 of those files survives. |
| 9 | + |
| 10 | +const fs = require('fs'); |
| 11 | +const path = require('path'); |
| 12 | +const md = require('../utils/markdown'); |
| 13 | +const { _parseFrontMatter } = require('../services/howto-loader'); |
| 14 | + |
| 15 | +const CONTENT_DIR = path.join(__dirname, '..', 'db', 'howto-content'); |
| 16 | + |
| 17 | +describe('markdown — headings', () => { |
| 18 | + it('renders each level', () => { |
| 19 | + expect(md.render('# One')).toBe('<h1>One</h1>'); |
| 20 | + expect(md.render('## Two')).toBe('<h2>Two</h2>'); |
| 21 | + expect(md.render('###### Six')).toBe('<h6>Six</h6>'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('requires a space, so a bare hash is prose', () => { |
| 25 | + expect(md.render('#hashtag')).toBe('<p>#hashtag</p>'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('does not treat shell comments inside a fence as headings', () => { |
| 29 | + // This is the single most common false positive in the corpus: hundreds of |
| 30 | + // `# comment` lines live inside bash blocks. |
| 31 | + const out = md.render('```bash\n# install docker\napt install docker\n```'); |
| 32 | + expect(out).toContain('<pre><code class="language-bash">'); |
| 33 | + expect(out).toContain('# install docker'); |
| 34 | + expect(out).not.toContain('<h1>'); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe('markdown — code', () => { |
| 39 | + it('escapes HTML inside fences', () => { |
| 40 | + const out = md.render('```\n<script>alert(1)</script>\n```'); |
| 41 | + expect(out).toContain('<script>'); |
| 42 | + expect(out).not.toContain('<script>'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('renders inline code and escapes it', () => { |
| 46 | + expect(md.render('use `docker ps` now')).toBe('<p>use <code>docker ps</code> now</p>'); |
| 47 | + expect(md.render('`a < b`')).toContain('<code>a < b</code>'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('leaves emphasis markers inside code spans alone', () => { |
| 51 | + expect(md.render('`**not bold**`')).toBe('<p><code>**not bold**</code></p>'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('does not turn a standalone number into a code span', () => { |
| 55 | + // Regression: an index-based placeholder made any bare digit a code span. |
| 56 | + expect(md.render('scale to 3 replicas')).toBe('<p>scale to 3 replicas</p>'); |
| 57 | + expect(md.render('`x` and 0 and `y`')).toBe('<p><code>x</code> and 0 and <code>y</code></p>'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('handles an unterminated fence without hanging', () => { |
| 61 | + expect(md.render('```\nstuff')).toContain('<pre><code>stuff</code></pre>'); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('markdown — emphasis and links', () => { |
| 66 | + it('renders bold and italic', () => { |
| 67 | + expect(md.render('**bold**')).toBe('<p><strong>bold</strong></p>'); |
| 68 | + expect(md.render('*italic*')).toBe('<p><em>italic</em></p>'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('leaves underscores in identifiers alone', () => { |
| 72 | + // DD_PROVIDER_X and /var/log/some_file must not become emphasis. |
| 73 | + expect(md.render('set DD_SOME_VAR now')).toBe('<p>set DD_SOME_VAR now</p>'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('renders links as external', () => { |
| 77 | + const out = md.render('[docs](https://example.com/x)'); |
| 78 | + expect(out).toContain('href="https://example.com/x"'); |
| 79 | + expect(out).toContain('rel="noopener"'); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe('markdown — lists', () => { |
| 84 | + it('renders bullets and ordered lists', () => { |
| 85 | + expect(md.render('- a\n- b')).toBe('<ul>\n<li>a</li>\n<li>b</li>\n</ul>'); |
| 86 | + expect(md.render('1. a\n2. b')).toBe('<ol>\n<li>a</li>\n<li>b</li>\n</ol>'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('nests by indentation and closes every level', () => { |
| 90 | + const out = md.render('- a\n - b\n- c'); |
| 91 | + expect(out).toContain('<ul>'); |
| 92 | + expect((out.match(/<ul>/g) || []).length).toBe(2); |
| 93 | + expect((out.match(/<\/ul>/g) || []).length).toBe(2); |
| 94 | + }); |
| 95 | + |
| 96 | + it('applies inline formatting inside items', () => { |
| 97 | + expect(md.render('- run `ls` **now**')).toContain('<li>run <code>ls</code> <strong>now</strong></li>'); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('markdown — tables', () => { |
| 102 | + it('renders a header and body', () => { |
| 103 | + const out = md.render('| A | B |\n|---|---|\n| 1 | 2 |'); |
| 104 | + expect(out).toContain('<th>A</th><th>B</th>'); |
| 105 | + expect(out).toContain('<td>1</td><td>2</td>'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('needs a separator row, otherwise it is prose', () => { |
| 109 | + expect(md.render('| not | a table |')).toContain('<p>'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('supports alignment markers', () => { |
| 113 | + expect(md.render('| A | B |\n|:--|--:|\n| 1 | 2 |')).toContain('<th>A</th>'); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe('markdown — raw HTML passthrough', () => { |
| 118 | + it('leaves an HTML block untouched', () => { |
| 119 | + const html = '<div class="tip">Keep me</div>'; |
| 120 | + expect(md.render(html)).toBe(html); |
| 121 | + }); |
| 122 | + |
| 123 | + it('preserves HTML mixed with markdown in one document', () => { |
| 124 | + const out = md.render('## Title\n\n<p class="lead">HTML here</p>\n\n- item'); |
| 125 | + expect(out).toContain('<h2>Title</h2>'); |
| 126 | + expect(out).toContain('<p class="lead">HTML here</p>'); |
| 127 | + expect(out).toContain('<li>item</li>'); |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +describe('markdown — robustness', () => { |
| 132 | + it('tolerates empty and nullish input', () => { |
| 133 | + expect(md.render('')).toBe(''); |
| 134 | + expect(md.render(null)).toBe(''); |
| 135 | + expect(md.render(undefined)).toBe(''); |
| 136 | + }); |
| 137 | + |
| 138 | + it('normalises CRLF', () => { |
| 139 | + expect(md.render('# A\r\n\r\ntext')).toBe('<h1>A</h1>\n<p>text</p>'); |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +describe('markdown — the shipped how-to corpus', () => { |
| 144 | + const files = fs.existsSync(CONTENT_DIR) |
| 145 | + ? fs.readdirSync(CONTENT_DIR).filter(f => /^[a-z0-9-]+(\.[a-z]{2})?\.md$/.test(f)) |
| 146 | + : []; |
| 147 | + |
| 148 | + it('has guides to check', () => { |
| 149 | + expect(files.length).toBeGreaterThan(100); |
| 150 | + }); |
| 151 | + |
| 152 | + it.each(files)('%s renders with no markdown left over', (file) => { |
| 153 | + const parsed = _parseFrontMatter(fs.readFileSync(path.join(CONTENT_DIR, file), 'utf8')); |
| 154 | + expect(parsed).not.toBeNull(); |
| 155 | + const html = md.render(parsed.body); |
| 156 | + // Code blocks legitimately contain markdown-looking text; everything else |
| 157 | + // must have been converted. |
| 158 | + const outside = html.replace(/<pre>[\s\S]*?<\/pre>/g, ''); |
| 159 | + expect(outside).not.toMatch(/^#{1,6}\s+\S/m); |
| 160 | + expect(outside).not.toMatch(/\*\*[^*\n]+\*\*/); |
| 161 | + expect(outside).not.toMatch(/^\s*[-*+]\s+\S/m); |
| 162 | + expect(outside).not.toMatch(/^\s*\|.+\|\s*$/m); |
| 163 | + }); |
| 164 | + |
| 165 | + it.each(files)('%s keeps the HTML it already contained', (file) => { |
| 166 | + const parsed = _parseFrontMatter(fs.readFileSync(path.join(CONTENT_DIR, file), 'utf8')); |
| 167 | + const html = md.render(parsed.body); |
| 168 | + for (const tag of ['h2', 'h3', 'table', 'ul', 'div']) { |
| 169 | + const before = (parsed.body.match(new RegExp(`<${tag}\\b`, 'gi')) || []).length; |
| 170 | + const after = (html.match(new RegExp(`<${tag}\\b`, 'gi')) || []).length; |
| 171 | + expect(`${file}:${tag}:${after >= before}`).toBe(`${file}:${tag}:true`); |
| 172 | + } |
| 173 | + }); |
| 174 | +}); |
0 commit comments