linkify.mjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Process links like https://example.org/
  2. // RFC3986: scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  3. const SCHEME_RE = /(?:^|[^a-z0-9.+-])([a-z][a-z0-9.+-]*)$/i
  4. export default function linkify (state, silent) {
  5. if (!state.md.options.linkify) return false
  6. if (state.linkLevel > 0) return false
  7. const pos = state.pos
  8. const max = state.posMax
  9. if (pos + 3 > max) return false
  10. if (state.src.charCodeAt(pos) !== 0x3A/* : */) return false
  11. if (state.src.charCodeAt(pos + 1) !== 0x2F/* / */) return false
  12. if (state.src.charCodeAt(pos + 2) !== 0x2F/* / */) return false
  13. const match = state.pending.match(SCHEME_RE)
  14. if (!match) return false
  15. const proto = match[1]
  16. const link = state.md.linkify.matchAtStart(state.src.slice(pos - proto.length))
  17. if (!link) return false
  18. let url = link.url
  19. // invalid link, but still detected by linkify somehow;
  20. // need to check to prevent infinite loop below
  21. if (url.length <= proto.length) return false
  22. // disallow '*' at the end of the link (conflicts with emphasis)
  23. url = url.replace(/\*+$/, '')
  24. const fullUrl = state.md.normalizeLink(url)
  25. if (!state.md.validateLink(fullUrl)) return false
  26. if (!silent) {
  27. state.pending = state.pending.slice(0, -proto.length)
  28. const token_o = state.push('link_open', 'a', 1)
  29. token_o.attrs = [['href', fullUrl]]
  30. token_o.markup = 'linkify'
  31. token_o.info = 'auto'
  32. const token_t = state.push('text', '', 0)
  33. token_t.content = state.md.normalizeLinkText(url)
  34. const token_c = state.push('link_close', 'a', -1)
  35. token_c.markup = 'linkify'
  36. token_c.info = 'auto'
  37. }
  38. state.pos += url.length - proto.length
  39. return true
  40. }