Repository: marktext/marktext
File: src/muya/lib/parser/parse.js (JavaScript)

Code before:
import { beginRules, inlineRules } from './rules'
import { isLengthEven, union } from '../utils'
import { punctuation } from '../config'

const CAN_NEST_RULES = ['strong', 'em', 'link', 'del', 'image', 'a_link'] // image can not nest but it has children

const validateRules = Object.assign({}, inlineRules)
delete validateRules.em
delete validateRules.strong
delete validateRules['tail_header']
delete validateRules['backlash']

const getSrcAlt = text => {
  const SRC_REG = /src\s*=\s*("|')([^\1]+?)\1/
  const ALT_REG = /alt\s*=\s*("|')([^\1]+?)\1/
  const WIDTH_REG = /width\s*=\s*("|')([^\1]+?)\1/
  const HEIGHT_REG = /height\s*=\s*("|')([^\1]+?)\1/
  const srcMatch = SRC_REG.exec(text)
  const src = srcMatch ? srcMatch[2] : ''
  const altMatch = ALT_REG.exec(text)
  const alt = altMatch ? altMatch[2] : ''
  const widthMatch = WIDTH_REG.exec(text)
  const width = widthMatch ? widthMatch[2] : ''
  const heightMatch = HEIGHT_REG.exec(text)
  const height = heightMatch ? heightMatch[2] : ''

  return { src, alt, width, height }
}

const lowerPriority = (src, offset) => {
  let i
  for (i = 0; i < offset; i++) {
    const text = src.substring(i)
    for (const rule of Object.keys(validateRules)) {
      const to = validateRules[rule].exec(text)
      if (to && to[0].length > offset - i) {
        return false
      }
    }
  }
  return true
}

const validateEmphasize = (src, offset, marker, pending) => {
  /**
   * Intraword emphasis is disallowed for _
   */
  const lastChar = pending.charAt(pending.length - 1)

Review comment (refactor):
We should extract only `height` and `width` if it's a number. According to w3schools the attributes unit is `pixels` (unsigned integer).

Code after:
import { beginRules, inlineRules } from './rules'
import { isLengthEven, union } from '../utils'
import { punctuation } from '../config'

const CAN_NEST_RULES = ['strong', 'em', 'link', 'del', 'image', 'a_link'] // image can not nest but it has children

const validateRules = Object.assign({}, inlineRules)
delete validateRules.em
delete validateRules.strong
delete validateRules['tail_header']
delete validateRules['backlash']

const validWidthAndHeight = value => {
  if (!/^\d{1,}$/.test(value)) return ''
  value = parseInt(value)
  return value >= 0 ? value : ''
}

const getSrcAlt = text => {
  const SRC_REG = /src\s*=\s*("|')([^\1]+?)\1/
  const ALT_REG = /alt\s*=\s*("|')([^\1]+?)\1/
  const WIDTH_REG = /width\s*=\s*("|')([^\1]+?)\1/
  const HEIGHT_REG = /height\s*=\s*("|')([^\1]+?)\1/
  const srcMatch = SRC_REG.exec(text)
  const src = srcMatch ? srcMatch[2] : ''
  const altMatch = ALT_REG.exec(text)
  const alt = altMatch ? altMatch[2] : ''
  const widthMatch = WIDTH_REG.exec(text)
  const width = widthMatch ? validWidthAndHeight(widthMatch[2]) : ''
  const heightMatch = HEIGHT_REG.exec(text)
  const height = heightMatch ? validWidthAndHeight(heightMatch[2]) : ''

  return { src, alt, width, height }
}

const lowerPriority = (src, offset) => {
  let i
  for (i = 0; i < offset; i++) {
    const text = src.substring(i)
    for (const rule of Object.keys(validateRules)) {
      const to = validateRules[rule].exec(text)
      if (to && to[0].length > offset - i) {
        return false
      }
    }
  }
  return true
}