All files / web/src/components/flowchart MathDisplay.tsx

0% Statements 0/178
0% Branches 0/1
0% Functions 0/1
0% Lines 0/178

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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179                                                                                                                                                                                                                                                                                                                                                                     
'use client'

import { css } from '../../../styled-system/css'

interface MathDisplayProps {
  /** Math expression string to render (e.g., "3 2/9 − 1 1/2" or "2x = 12") */
  expression: string
  /** Font size for the display */
  size?: 'sm' | 'md' | 'lg' | 'xl'
}

/**
 * Math font stack optimized for MathML rendering.
 * STIX Two Math is the gold standard for web math typography.
 * Fallbacks include system math fonts and common alternatives.
 */
const mathFontFamily = [
  '"STIX Two Math"',
  '"Cambria Math"',
  '"Latin Modern Math"',
  '"XITS Math"',
  'math',
  'serif',
].join(', ')

/**
 * Renders math expressions using native MathML.
 * Parses strings like "3 2/9 − 1 1/2" and renders proper semantic math.
 *
 * Browser support: 94%+ (Chrome 109+, Firefox, Safari 10+, Edge 109+)
 */
export function MathDisplay({ expression, size = 'lg' }: MathDisplayProps) {
  const fontSize = {
    sm: '1.25rem',
    md: '1.5rem',
    lg: '1.75rem',
    xl: '2.25rem',
  }[size]

  // Parse the expression into tokens
  const tokens = parseExpression(expression)

  return (
    <math
      data-testid="math-display"
      data-expression={expression}
      data-size={size}
      data-token-count={tokens.length}
      className={css({
        display: 'inline-flex',
        alignItems: 'center',
        verticalAlign: 'middle',
      })}
      style={{ fontSize, fontFamily: mathFontFamily }}
    >
      <mrow>{tokens.map((token, idx) => renderToken(token, idx))}</mrow>
    </math>
  )
}

type Token =
  | { type: 'number'; value: string }
  | { type: 'fraction'; numerator: string; denominator: string }
  | { type: 'mixed'; whole: string; numerator: string; denominator: string }
  | { type: 'operator'; value: string }
  | { type: 'variable'; name: string }
  | { type: 'term'; coefficient: string; variable: string }
  | { type: 'text'; value: string }

function parseExpression(expr: string): Token[] {
  const tokens: Token[] = []
  // Split by spaces but keep operators together
  const parts = expr.split(/\s+/)

  for (let i = 0; i < parts.length; i++) {
    const part = parts[i]

    // Check if it's an operator
    if (['−', '-', '+', '×', '÷', '=', '→'].includes(part)) {
      tokens.push({ type: 'operator', value: part })
      continue
    }

    // Check if it's a fraction (contains /)
    if (part.includes('/')) {
      const [num, denom] = part.split('/')
      // Check if previous token was a plain number (making this a mixed number)
      const prev = tokens[tokens.length - 1]
      if (prev && prev.type === 'number') {
        // Convert previous number + this fraction to mixed number
        tokens.pop()
        tokens.push({
          type: 'mixed',
          whole: prev.value,
          numerator: num,
          denominator: denom,
        })
      } else {
        tokens.push({ type: 'fraction', numerator: num, denominator: denom })
      }
      continue
    }

    // Check if it's a plain number
    if (/^-?\d+$/.test(part)) {
      tokens.push({ type: 'number', value: part })
      continue
    }

    // Check if it's a term like "2x" or "3y" (coefficient + variable)
    const termMatch = part.match(/^(-?\d*)([a-zA-Z])$/)
    if (termMatch) {
      const [, coef, varName] = termMatch
      if (coef === '' || coef === '-' || coef === '1' || coef === '-1') {
        // Coefficient of 1 is implicit: "1x" → "x", "-1x" → "-x"
        const isNegative = coef === '-' || coef === '-1'
        tokens.push({ type: 'variable', name: isNegative ? `-${varName}` : varName })
      } else {
        tokens.push({ type: 'term', coefficient: coef, variable: varName })
      }
      continue
    }

    // Check if it's a single variable
    if (/^[a-zA-Z]$/.test(part)) {
      tokens.push({ type: 'variable', name: part })
      continue
    }

    // Otherwise it's text
    tokens.push({ type: 'text', value: part })
  }

  return tokens
}

function renderToken(token: Token, key: number): React.ReactNode {
  switch (token.type) {
    case 'number':
      return <mn key={key}>{token.value}</mn>

    case 'fraction':
      return (
        <mfrac key={key}>
          <mn>{token.numerator}</mn>
          <mn>{token.denominator}</mn>
        </mfrac>
      )

    case 'mixed':
      return (
        <mrow key={key}>
          <mn>{token.whole}</mn>
          <mfrac>
            <mn>{token.numerator}</mn>
            <mn>{token.denominator}</mn>
          </mfrac>
        </mrow>
      )

    case 'operator':
      return <mo key={key}>{token.value}</mo>

    case 'variable':
      return <mi key={key}>{token.name}</mi>

    case 'term':
      return (
        <mrow key={key}>
          <mn>{token.coefficient}</mn>
          <mi>{token.variable}</mi>
        </mrow>
      )

    case 'text':
      return <mtext key={key}>{token.value}</mtext>
  }
}