Files
ComfyUI_frontend/tests-ui/tests/utils/markdownRendererUtil.test.ts
Christian Byrne 89ff8255bd open markdown links in new window/tab (#6229)
## Summary

Changes links in markdown snippets (What's New popup, node info sidebar)
to open the link in a new tab/window rather than directly navigating and
potentially losing unsaved work.



https://github.com/user-attachments/assets/24331bba-e31a-484c-bc11-12cf61805c98



Fixes https://github.com/Comfy-Org/ComfyUI_frontend/issues/6223.

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-6229-open-markdown-links-in-new-window-tab-2956d73d365081edbb1efb21cd0e2ab2)
by [Unito](https://www.unito.io)

---------

Co-authored-by: Alexander Brown <drjkl@comfy.org>
Co-authored-by: GitHub Action <action@github.com>
2025-10-23 13:33:38 -07:00

124 lines
4.1 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { renderMarkdownToHtml } from '@/utils/markdownRendererUtil'
describe('markdownRendererUtil', () => {
describe('renderMarkdownToHtml', () => {
it('should render basic markdown to HTML', () => {
const markdown = '# Hello\n\nThis is a test.'
const html = renderMarkdownToHtml(markdown)
expect(html).toContain('<h1')
expect(html).toContain('Hello')
expect(html).toContain('<p>')
expect(html).toContain('This is a test.')
})
it('should render links with target="_blank" and rel="noopener noreferrer"', () => {
const markdown = '[Click here](https://example.com)'
const html = renderMarkdownToHtml(markdown)
expect(html).toContain('target="_blank"')
expect(html).toContain('rel="noopener noreferrer"')
expect(html).toContain('href="https://example.com"')
expect(html).toContain('Click here')
})
it('should render multiple links with target="_blank"', () => {
const markdown =
'[Link 1](https://example.com) and [Link 2](https://test.com)'
const html = renderMarkdownToHtml(markdown)
const targetBlankMatches = html.match(/target="_blank"/g)
expect(targetBlankMatches).toHaveLength(2)
const relMatches = html.match(/rel="noopener noreferrer"/g)
expect(relMatches).toHaveLength(2)
})
it('should handle relative image paths with baseUrl', () => {
const markdown = '![Alt text](image.png)'
const baseUrl = 'https://cdn.example.com'
const html = renderMarkdownToHtml(markdown, baseUrl)
expect(html).toContain(`src="${baseUrl}/image.png"`)
expect(html).toContain('alt="Alt text"')
})
it('should not modify absolute image URLs', () => {
const markdown = '![Alt text](https://example.com/image.png)'
const baseUrl = 'https://cdn.example.com'
const html = renderMarkdownToHtml(markdown, baseUrl)
expect(html).toContain('src="https://example.com/image.png"')
expect(html).not.toContain(baseUrl)
})
it('should handle empty markdown', () => {
const html = renderMarkdownToHtml('')
expect(html).toBe('')
})
it('should sanitize potentially dangerous HTML', () => {
const markdown = '<script>alert("xss")</script>'
const html = renderMarkdownToHtml(markdown)
expect(html).not.toContain('<script>')
expect(html).not.toContain('alert')
})
it('should allow video tags with proper attributes', () => {
const markdown =
'<video src="video.mp4" controls autoplay loop muted></video>'
const html = renderMarkdownToHtml(markdown)
expect(html).toContain('<video')
expect(html).toContain('src="video.mp4"')
expect(html).toContain('controls')
})
it('should render links with title attribute', () => {
const markdown = '[Link](https://example.com "This is a title")'
const html = renderMarkdownToHtml(markdown)
expect(html).toContain('title="This is a title"')
expect(html).toContain('target="_blank"')
expect(html).toContain('rel="noopener noreferrer"')
})
it('should handle bare URLs (autolinks)', () => {
const markdown = 'Visit https://example.com for more info.'
const html = renderMarkdownToHtml(markdown)
expect(html).toContain('href="https://example.com"')
expect(html).toContain('target="_blank"')
expect(html).toContain('rel="noopener noreferrer"')
})
it('should render complex markdown with links, images, and text', () => {
const markdown = `
# Release Notes
Check out our [documentation](https://docs.example.com) for more info.
![Screenshot](screenshot.png)
Visit our [homepage](https://example.com) to learn more.
`
const baseUrl = 'https://cdn.example.com'
const html = renderMarkdownToHtml(markdown, baseUrl)
// Check links have target="_blank"
const targetBlankMatches = html.match(/target="_blank"/g)
expect(targetBlankMatches).toHaveLength(2)
// Check image has baseUrl prepended
expect(html).toContain(`${baseUrl}/screenshot.png`)
// Check heading
expect(html).toContain('Release Notes')
})
})
})