|
| 1 | +import 'reflect-metadata'; |
| 2 | +import assert from 'assert'; |
| 3 | + |
| 4 | +import { MarkdownRender } from './markdown-render.js'; |
| 5 | + |
| 6 | +// Declare jest globals to avoid compilation errors if types are missing |
| 7 | +declare const describe: any; |
| 8 | +declare const it: any; |
| 9 | +declare const beforeEach: any; |
| 10 | +declare const jest: any; |
| 11 | + |
| 12 | +// Mock ConfigurationService |
| 13 | +const mockConfigService = { |
| 14 | + get: jest.fn(), |
| 15 | +}; |
| 16 | + |
| 17 | +describe('MarkdownRender', () => { |
| 18 | + let markdownRender: MarkdownRender; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + // Reset mock and set default behavior |
| 22 | + mockConfigService.get.mockReset(); |
| 23 | + mockConfigService.get.mockResolvedValue(false); |
| 24 | + |
| 25 | + markdownRender = new MarkdownRender(); |
| 26 | + // Inject mock manually |
| 27 | + (markdownRender as any).configurationService = mockConfigService; |
| 28 | + // Manually call init to trigger postConstruct logic |
| 29 | + markdownRender.init(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should render basic markdown', () => { |
| 33 | + const md = '# Hello'; |
| 34 | + const html = markdownRender.render(md); |
| 35 | + // h1 id="hello" comes from anchor plugin |
| 36 | + assert.ok(html.includes('<h1 id="hello">Hello</h1>')); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should highlight code blocks', () => { |
| 40 | + const md = '```javascript\nconst a = 1;\n```'; |
| 41 | + const html = markdownRender.render(md); |
| 42 | + |
| 43 | + // Check for language class added by markdown-it/highlight.js |
| 44 | + assert.ok(html.includes('language-javascript')); |
| 45 | + |
| 46 | + // Check for highlight.js specific classes (indicating highlighting actually happened) |
| 47 | + // "const" is a keyword |
| 48 | + assert.ok(html.includes('hljs-keyword')); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should sanitize html', () => { |
| 52 | + const md = '<script>alert(1)</script>'; |
| 53 | + const html = markdownRender.render(md); |
| 54 | + assert.ok(!html.includes('<script>')); |
| 55 | + assert.ok(!html.includes('alert(1)')); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should allow span tags with class attributes (needed for highlighting)', () => { |
| 59 | + // Manually construct a highlighted-like span to ensure sanitizer doesn't strip it |
| 60 | + // Note: markdown-it render output is sanitized. |
| 61 | + // If we input raw HTML, it might be stripped depending on settings. |
| 62 | + // But highlight.js output is generated internally. |
| 63 | + // Let's test with a code block that generates spans. |
| 64 | + |
| 65 | + const md = '```javascript\nconst a = 1;\n```'; |
| 66 | + const html = markdownRender.render(md); |
| 67 | + |
| 68 | + // Check that span tags are preserved |
| 69 | + assert.ok(html.includes('<span class="hljs-keyword">const</span>')); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should support target="_blank" configuration', async () => { |
| 73 | + mockConfigService.get.mockResolvedValue(true); |
| 74 | + |
| 75 | + const renderer = new MarkdownRender(); |
| 76 | + (renderer as any).configurationService = mockConfigService; |
| 77 | + renderer.init(); |
| 78 | + |
| 79 | + // Wait for promise resolution in init (microtask) |
| 80 | + // Increase wait time to ensure the promise chain in init() completes |
| 81 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 82 | + |
| 83 | + const md = '[link](http://example.com)'; |
| 84 | + const html = renderer.render(md); |
| 85 | + assert.ok(html.includes('target="_blank"')); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should NOT add target="_blank" when disabled', async () => { |
| 89 | + mockConfigService.get.mockResolvedValue(false); |
| 90 | + |
| 91 | + const renderer = new MarkdownRender(); |
| 92 | + (renderer as any).configurationService = mockConfigService; |
| 93 | + renderer.init(); |
| 94 | + |
| 95 | + // Wait for promise resolution in init |
| 96 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 97 | + |
| 98 | + const md = '[link](http://example.com)'; |
| 99 | + const html = renderer.render(md); |
| 100 | + assert.ok(!html.includes('target="_blank"')); |
| 101 | + }); |
| 102 | +}); |
0 commit comments