html_inline.mjs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Process html tags
  2. import { HTML_TAG_RE } from '../common/html_re.mjs'
  3. function isLinkOpen (str) {
  4. return /^<a[>\s]/i.test(str)
  5. }
  6. function isLinkClose (str) {
  7. return /^<\/a\s*>/i.test(str)
  8. }
  9. function isLetter (ch) {
  10. /* eslint no-bitwise:0 */
  11. const lc = ch | 0x20 // to lower case
  12. return (lc >= 0x61/* a */) && (lc <= 0x7a/* z */)
  13. }
  14. export default function html_inline (state, silent) {
  15. if (!state.md.options.html) { return false }
  16. // Check start
  17. const max = state.posMax
  18. const pos = state.pos
  19. if (state.src.charCodeAt(pos) !== 0x3C/* < */ ||
  20. pos + 2 >= max) {
  21. return false
  22. }
  23. // Quick fail on second char
  24. const ch = state.src.charCodeAt(pos + 1)
  25. if (ch !== 0x21/* ! */ &&
  26. ch !== 0x3F/* ? */ &&
  27. ch !== 0x2F/* / */ &&
  28. !isLetter(ch)) {
  29. return false
  30. }
  31. const match = state.src.slice(pos).match(HTML_TAG_RE)
  32. if (!match) { return false }
  33. if (!silent) {
  34. const token = state.push('html_inline', '', 0)
  35. token.content = match[0]
  36. if (isLinkOpen(token.content)) state.linkLevel++
  37. if (isLinkClose(token.content)) state.linkLevel--
  38. }
  39. state.pos += match[0].length
  40. return true
  41. }