import {visit} from 'unist-util-visit'

export default () => {
  return (tree) => {
    visit(tree, 'element', function (node) {
      if (
        node.tagName === 'a' &&
        typeof node.properties.href === 'string'
      ) {
        const url = node.properties.href
        if (url.startsWith('http://') || url.startsWith('https://')) {
          node.properties.target = '_blank'
        }
      }
    })
  }
}
