All files / src/utils richText.ts

100% Statements 37/37
97.56% Branches 40/41
100% Functions 5/5
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83                    10x             10x               24x 23x 23x 23x   2x         15x 15x 7x 3x 3x 2x 2x                 4x 3x     15x       32x 17x 16x 16x 2x 2x   14x 1x   13x 13x       27x         44x 43x 32x    
// @implements UC-SPACE-010.1
/**
 * Renders a page's rich text (a TipTap JSON document, limited by flows-api
 * utils/spacePages.js) as Vue nodes: each allowed node becomes its element,
 * text stays text. Never HTML, so nothing stored can inject markup; anything
 * unknown renders as its children only, and unsafe links as plain text.
 */
import { h, type VNode, type VNodeArrayChildren } from 'vue'
import type { RichTextNode } from '@/utils/spacePages'
 
const ELEMENTS: Record<string, string> = {
  paragraph: 'p',
  bulletList: 'ul',
  orderedList: 'ol',
  listItem: 'li'
}
 
const MARK_ELEMENTS: Record<string, string> = {
  bold: 'strong',
  italic: 'em',
  underline: 'u'
}
 
/** `https:` and `mailto:` links only */
export function safeHref(href: unknown): string | null {
  if (typeof href !== 'string') return null
  try {
    const url = new URL(href)
    return url.protocol === 'https:' || url.protocol === 'mailto:' ? url.href : null
  } catch {
    return null
  }
}
 
function renderText(node: RichTextNode): VNode | string {
  let rendered: VNode | string = node.text || ''
  for (const mark of node.marks || []) {
    if (mark.type === 'link') {
      const href = safeHref(mark.attrs?.href)
      if (!href) continue
      const external = href.startsWith('https:')
      rendered = h(
        'a',
        {
          href,
          rel: 'noopener noreferrer nofollow',
          ...(external && { target: '_blank' })
        },
        [rendered]
      )
    } else if (MARK_ELEMENTS[mark.type]) {
      rendered = h(MARK_ELEMENTS[mark.type], [rendered])
    }
  }
  return rendered
}
 
function renderNode(node: RichTextNode): VNode | string | VNodeArrayChildren {
  if (node.type === 'text') return renderText(node)
  if (node.type === 'hardBreak') return h('br')
  const children = renderNodes(node.content)
  if (node.type === 'heading') {
    const level = node.attrs?.level === 3 ? 3 : 2
    return h(`h${level}`, children)
  }
  if (node.type === 'orderedList' && Number.isInteger(node.attrs?.start) && node.attrs!.start > 1) {
    return h('ol', { start: node.attrs!.start }, children)
  }
  const element = ELEMENTS[node.type]
  return element ? h(element, children) : children
}
 
export function renderNodes(nodes: RichTextNode[] | undefined): VNodeArrayChildren {
  return (nodes || []).map(renderNode)
}
 
/** Whether a document holds any text (an empty editor gives an empty paragraph) */
export function hasText(doc: RichTextNode | undefined | null): boolean {
  if (!doc) return false
  if (doc.type === 'text') return !!doc.text
  return (doc.content || []).some(hasText)
}