LCOV - code coverage report
Current view: top level - src/utils - markdownRendererUtil.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 94.3 % 35 33
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { default as DOMPurify } from 'dompurify'
       2              : import { Renderer, marked } from 'marked'
       3              : import type { Tokens } from 'marked'
       4              : 
       5         2356 : const ALLOWED_TAGS = ['video', 'source']
       6         2356 : const ALLOWED_ATTRS = [
       7              :   'controls',
       8              :   'autoplay',
       9              :   'loop',
      10              :   'muted',
      11              :   'preload',
      12              :   'poster'
      13              : ]
      14              : 
      15              : type RuntimeLinkToken = Omit<Tokens.Link, 'tokens'> & {
      16              :   tokens?: Tokens.Link['tokens']
      17              : }
      18              : 
      19              : // Escapes a value for safe interpolation into an HTML attribute or text
      20              : // node, preventing it from breaking out of a quoted attribute (e.g. a
      21              : // markdown image/link whose href or alt text contains a `"` followed by a
      22              : // new attribute such as `style="..."`).
      23           55 : function escapeHtml(value: string): string {
      24           55 :   return value
      25              :     .replace(/&/g, '&amp;')
      26              :     .replace(/</g, '&lt;')
      27              :     .replace(/>/g, '&gt;')
      28              :     .replace(/"/g, '&quot;')
      29              :     .replace(/'/g, '&#39;')
      30              : }
      31              : 
      32              : // Matches relative src attributes in img, source, and video HTML tags
      33              : // Captures: 1) opening tag with src=", 2) relative path, 3) closing quote
      34              : // Excludes absolute paths (starting with /) and URLs (http:// or https://)
      35         2356 : const MEDIA_SRC_REGEX =
      36              :   /(<(?:img|source|video)[^>]*\ssrc=['"])(?!(?:[/#?]|[a-z][a-z0-9+.-]*:))([^'"\s>]+)(['"])/gi
      37              : 
      38              : // Rooted paths, fragments, queries, and anything carrying a scheme (http,
      39              : // javascript, data, ...) must keep their original form for sanitizing.
      40         2356 : const NON_REBASEABLE_HREF = /^(?:[/#?]|[a-z][a-z0-9+.-]*:)/i
      41         2356 : const COMFY_ORG_HOST = /(?:^|\.)comfy\.org$/
      42              : 
      43           53 : export function resolveMarkdownUrl(href: string, baseUrl: string): string {
      44           53 :   if (!baseUrl) return href
      45            9 :   if (!NON_REBASEABLE_HREF.test(href)) return `${baseUrl}/${href}`
      46              : 
      47           53 :   try {
      48            8 :     const url = new URL(href)
      49           53 :     if (COMFY_ORG_HOST.test(url.hostname) && url.pathname.startsWith('/api/')) {
      50            0 :       return `${baseUrl}${url.pathname.slice(4)}${url.search}${url.hash}`
      51              :     }
      52              :   } catch {
      53            0 :     return href
      54              :   }
      55              : 
      56            8 :   return href
      57              : }
      58              : 
      59              : // Create a marked Renderer that prefixes relative URLs with base
      60           74 : function createMarkdownRenderer(baseUrl?: string): Renderer {
      61           74 :   const normalizedBase = baseUrl ? baseUrl.replace(/\/+$/, '') : ''
      62           74 :   const renderer = new Renderer()
      63           74 :   renderer.image = ({ href, title, text }) => {
      64            2 :     const src = resolveMarkdownUrl(href, normalizedBase)
      65            2 :     const titleAttr = title ? ` title="${escapeHtml(title)}"` : ''
      66            2 :     return `<img src="${escapeHtml(src)}" alt="${escapeHtml(text)}"${titleAttr} />`
      67              :   }
      68           74 :   renderer.link = ({ href, title, tokens, text }: RuntimeLinkToken) => {
      69              :     // For autolinks (bare URLs), tokens may be undefined, so fall back to text
      70           51 :     const target = resolveMarkdownUrl(href, normalizedBase)
      71           51 :     const linkText =
      72              :       text === href
      73              :         ? escapeHtml(target)
      74              :         : tokens
      75              :           ? renderer.parser.parseInline(tokens)
      76              :           : escapeHtml(text)
      77           51 :     const titleAttr = title ? ` title="${escapeHtml(title)}"` : ''
      78           51 :     return `<a href="${escapeHtml(target)}" ${titleAttr} target="_blank" rel="noopener noreferrer">${linkText}</a>`
      79              :   }
      80           74 :   return renderer
      81              : }
      82              : 
      83           74 : export function renderMarkdownToHtml(
      84              :   markdown: string,
      85              :   baseUrl?: string
      86              : ): string {
      87           74 :   if (!markdown) return ''
      88              : 
      89           74 :   let html = marked.parse(markdown, {
      90              :     renderer: createMarkdownRenderer(baseUrl),
      91              :     gfm: true // Enable GitHub Flavored Markdown (including autolinks)
      92              :   }) as string
      93              : 
      94           74 :   if (baseUrl) {
      95           14 :     html = html.replace(
      96              :       MEDIA_SRC_REGEX,
      97              :       `$1${baseUrl.replace(/\/+$/, '')}/$2$3`
      98              :     )
      99              :   }
     100              : 
     101           74 :   return DOMPurify.sanitize(html, {
     102              :     ADD_TAGS: ALLOWED_TAGS,
     103              :     ADD_ATTR: [...ALLOWED_ATTRS, 'target', 'rel']
     104              :   })
     105              : }
        

Generated by: LCOV version 2.0-1