entity.mjs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Process html entity - {, ¯, ", ...
  2. import { decodeHTML } from 'entities'
  3. import { isValidEntityCode, fromCodePoint } from '../common/utils.mjs'
  4. const DIGITAL_RE = /^&#((?:x[a-f0-9]{1,6}|[0-9]{1,7}));/i
  5. const NAMED_RE = /^&([a-z][a-z0-9]{1,31});/i
  6. export default function entity (state, silent) {
  7. const pos = state.pos
  8. const max = state.posMax
  9. if (state.src.charCodeAt(pos) !== 0x26/* & */) return false
  10. if (pos + 1 >= max) return false
  11. const ch = state.src.charCodeAt(pos + 1)
  12. if (ch === 0x23 /* # */) {
  13. const match = state.src.slice(pos).match(DIGITAL_RE)
  14. if (match) {
  15. if (!silent) {
  16. const code = match[1][0].toLowerCase() === 'x' ? parseInt(match[1].slice(1), 16) : parseInt(match[1], 10)
  17. const token = state.push('text_special', '', 0)
  18. token.content = isValidEntityCode(code) ? fromCodePoint(code) : fromCodePoint(0xFFFD)
  19. token.markup = match[0]
  20. token.info = 'entity'
  21. }
  22. state.pos += match[0].length
  23. return true
  24. }
  25. } else {
  26. const match = state.src.slice(pos).match(NAMED_RE)
  27. if (match) {
  28. const decoded = decodeHTML(match[0])
  29. if (decoded !== match[0]) {
  30. if (!silent) {
  31. const token = state.push('text_special', '', 0)
  32. token.content = decoded
  33. token.markup = match[0]
  34. token.info = 'entity'
  35. }
  36. state.pos += match[0].length
  37. return true
  38. }
  39. }
  40. }
  41. return false
  42. }