index.js
function gatherReferences(file, tree, info, fileSet) {
  var cache = {}
  var getDefinition = definitions(tree)
  var prefix = ''
  var headingPrefix = '#'
  var lines

  if (info && info.type in viewPaths) {
    prefix = '/' + info.path() + '/' + viewPaths[info.type] + '/'
  }

  if (info && info.type in headingPrefixes) {
    headingPrefix = headingPrefixes[info.type]
  }

  lines = info && info.type in lineLinks ? lineLinks[info.type] : false

  visit(tree, ['link', 'image', 'linkReference', 'imageReference'], onresource)

  return cache

  // Handle resources.
  function onresource(node) {
    var link = node.url
    var definition
    var index
    var uri
    var pathname
    var hash

    // Handle references.
    if (node.identifier) {
      definition = getDefinition(node.identifier)
      link = definition && definition.url
    }

    // Ignore definitions without url.
    if (!link) {
      return
    }

    uri = parse(link)

    // Drop `?search`
    uri.search = ''
    link = format(uri)

    if (!fileSet && (uri.hostname || uri.pathname)) {
      return
    }

    if (!uri.hostname) {
      if (lines && lineExpression.test(uri.hash)) {
        uri.hash = ''
      }

      // Handle hashes, or relative files.
      if (!uri.pathname && uri.hash) {
        link = file.path + uri.hash
        uri = parse(link)
      } else {
        link = urljoin(file.dirname, link)

        if (uri.hash) {
          link += uri.hash
        }

        uri = parse(link)
      }
    }

    // Handle full links.
    if (uri.hostname) {
      if (!prefix || !fileSet) {
        return
      }

      if (
        uri.hostname !== info.domain ||
        uri.pathname.slice(0, prefix.length) !== prefix
      ) {
        return
      }

      link = uri.pathname.slice(prefix.length) + (uri.hash || '')

      // Things get interesting here: branches: `foo/bar/baz` could be `baz` on
      // the `foo/bar` branch, or, `baz` in the `bar` directory on the `foo`
      // branch.
      //  Currently, we’re ignoring this and just not supporting branches.
      link = link.slice(link.indexOf('/') + 1)
    }

    // Handle file links, or combinations of files and hashes.
    index = link.indexOf(headingPrefix)

    if (index === -1) {
      pathname = link
      hash = null
    } else {
      pathname = link.slice(0, index)
      hash = link.slice(index + headingPrefix.length)

      if (lines && lineExpression.test(hash)) {
        hash = null
      }
    }

    if (!cache[pathname]) {
      cache[pathname] = []
    }

    cache[pathname].push(node)

    if (hash) {
      link = pathname + '#' + hash

      if (!cache[link]) {
        cache[link] = []
      }

      cache[link].push(node)
    }
  }
}
function gatherReferences(file, tree, info, fileSet) {
  var cache = {}
  var getDefinition = definitions(tree)
  var prefix = ''
  var headingPrefix = '#'
  var lines

  if (info && info.type in viewPaths) {
    prefix = '/' + info.path() + '/' + viewPaths[info.type] + '/'
  }

  if (info && info.type in headingPrefixes) {
    headingPrefix = headingPrefixes[info.type]
  }

  lines = info && info.type in lineLinks ? lineLinks[info.type] : false

  visit(tree, ['link', 'image', 'linkReference', 'imageReference'], onresource)

  return cache

  // Handle resources.
  function onresource(node) {
    var link = node.url
    var definition
    var index
    var uri
    var pathname
    var hash

    // Handle references.
    if (node.identifier) {
      definition = getDefinition(node.identifier)
      link = definition && definition.url
    }

    // Ignore definitions without url.
    if (!link) {
      return
    }

    uri = parse(link)

    // Drop `?search`
    uri.search = ''
    link = format(uri)

    if (!fileSet && (uri.hostname || uri.pathname)) {
      return
    }

    if (!uri.hostname) {
      if (lines && lineExpression.test(uri.hash)) {
        uri.hash = ''
      }

      // Handle hashes, or relative files.
      if (!uri.pathname && uri.hash) {
        link = file.path + uri.hash
        uri = parse(link)
      } else {
        link = urljoin(file.dirname, link)

        if (uri.hash) {
          link += uri.hash
        }

        uri = parse(link)
      }
    }

    // Handle full links.
    if (uri.hostname) {
      if (!prefix || !fileSet) {
        return
      }

      if (
        uri.hostname !== info.domain ||
        uri.pathname.slice(0, prefix.length) !== prefix
      ) {
        return
      }

      link = uri.pathname.slice(prefix.length) + (uri.hash || '')

      // Things get interesting here: branches: `foo/bar/baz` could be `baz` on
      // the `foo/bar` branch, or, `baz` in the `bar` directory on the `foo`
      // branch.
      //  Currently, we’re ignoring this and just not supporting branches.
      link = link.slice(link.indexOf('/') + 1)
    }

    // Handle file links, or combinations of files and hashes.
    index = link.indexOf(headingPrefix)

    if (index === -1) {
      pathname = link
      hash = null
    } else {
      pathname = link.slice(0, index)
      hash = link.slice(index + headingPrefix.length)

      if (lines && lineExpression.test(hash)) {
        hash = null
      }
    }

    if (!cache[pathname]) {
      cache[pathname] = []
    }

    cache[pathname].push(node)

    if (hash) {
      link = pathname + '#' + hash

      if (!cache[link]) {
        cache[link] = []
      }

      cache[link].push(node)
    }
  }
}
function gatherReferences ( file , tree , info , fileSet ) { var cache = { } var getDefinition = definitions ( tree ) var prefix = '' var headingPrefix = '#' var lines if ( info && info . type in viewPaths ) { prefix = '/' + info . path ( ) + '/' + viewPaths [ info . type ] + '/' } if ( info && info . type in headingPrefixes ) { headingPrefix = headingPrefixes [ info . type ] } lines = info && info . type in lineLinks ? lineLinks [ info . type ] : false visit ( tree , [ 'link' , 'image' , 'linkReference' , 'imageReference' ] , onresource ) return cache // Handle resources. function onresource ( node ) { var link = node . url var definition var index var uri var pathname var hash // Handle references. if ( node . identifier ) { definition = getDefinition ( node . identifier ) link = definition && definition . url } // Ignore definitions without url. if ( ! link ) { return } uri = parse ( link ) // Drop `?search` uri . search = '' link = format ( uri ) if ( ! fileSet && ( uri . hostname || uri . pathname ) ) { return } if ( ! uri . hostname ) { if ( lines && lineExpression . test ( uri . hash ) ) { uri . hash = '' } // Handle hashes, or relative files. if ( ! uri . pathname && uri . hash ) { link = file . path + uri . hash uri = parse ( link ) } else { link = urljoin ( file . dirname , link ) if ( uri . hash ) { link += uri . hash } uri = parse ( link ) } } // Handle full links. if ( uri . hostname ) { if ( ! prefix || ! fileSet ) { return } if ( uri . hostname !== info . domain || uri . pathname . slice ( 0 , prefix . length ) !== prefix ) { return } link = uri . pathname . slice ( prefix . length ) + ( uri . hash || '' ) // Things get interesting here: branches: `foo/bar/baz` could be `baz` on // the `foo/bar` branch, or, `baz` in the `bar` directory on the `foo` // branch. //  Currently, we’re ignoring this and just not supporting branches. link = link . slice ( link . indexOf ( '/' ) + 1 ) } // Handle file links, or combinations of files and hashes. index = link . indexOf ( headingPrefix ) if ( index === - 1 ) { pathname = link hash = null } else { pathname = link . slice ( 0 , index ) hash = link . slice ( index + headingPrefix . length ) if ( lines && lineExpression . test ( hash ) ) { hash = null } } if ( ! cache [ pathname ] ) { cache [ pathname ] = [ ] } cache [ pathname ] . push ( node ) if ( hash ) { link = pathname + '#' + hash if ( ! cache [ link ] ) { cache [ link ] = [ ] } cache [ link ] . push ( node ) } } }
Gather references: a map of file-paths references to be one or more nodes.
Gather references : a map of file - paths references to be one or more nodes .