format-link-as-autolink.js 1010 B

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * @typedef {import('mdast').Link} Link
  3. * @typedef {import('../types.js').State} State
  4. */
  5. import {toString} from 'mdast-util-to-string'
  6. /**
  7. * @param {Link} node
  8. * @param {State} state
  9. * @returns {boolean}
  10. */
  11. export function formatLinkAsAutolink(node, state) {
  12. const raw = toString(node)
  13. return Boolean(
  14. !state.options.resourceLink &&
  15. // If there’s a url…
  16. node.url &&
  17. // And there’s a no title…
  18. !node.title &&
  19. // And the content of `node` is a single text node…
  20. node.children &&
  21. node.children.length === 1 &&
  22. node.children[0].type === 'text' &&
  23. // And if the url is the same as the content…
  24. (raw === node.url || 'mailto:' + raw === node.url) &&
  25. // And that starts w/ a protocol…
  26. /^[a-z][a-z+.-]+:/i.test(node.url) &&
  27. // And that doesn’t contain ASCII control codes (character escapes and
  28. // references don’t work), space, or angle brackets…
  29. !/[\0- <>\u007F]/.test(node.url)
  30. )
  31. }