import React, {PropTypes} from 'react'
import {Link} from 'react-router'
import {findPage, map} from '../../utils/navigation'
import {isAbsolute, isHash, resolve, parse} from '../../utils/url'
import {getBlobUrl} from '../../utils/github'

const absUrl = (url, currPageName) => {
  if (isAbsolute(url)) return url
  const currPage = map[currPageName]
  const resolvedUrl = resolve(currPage.path, url)
  const resolvedPath = parse(resolvedUrl).pathname
  return getBlobUrl(currPage.repo, resolvedPath, undefined, currPage.org)
}

const formatProps = (props) => {
  const {page: pageName, children, ...rest} = props
  let {href} = props
  let target
  let to

  if (href && !isHash(href)) {
    href = absUrl(href, pageName)
    const page = findPage(href)
    if (page && !page.external) to = `/${page.name}`
    else target = '_blank'
  }

  return {
    ...rest,
    target,
    href,
    to,
    children
  }
}

export default function A(props) {
  const {to, children, ...rest} = formatProps(props)

  if (to) {
    return <Link {...rest} to={to}>{children}</Link>
  }

  return <a {...rest}>{children}</a>
}

A.propTypes = {
  href: PropTypes.string,
  children: PropTypes.node
}

@@ -402,28 +402,25 @@
   const 
-resolvedPath
+parsedUrl
  = parse
@@ -436,17 +436,59 @@
 Url)
-.pathname
+%0A  const path = parsedUrl.pathname + parsedUrl.hash
 %0A  r
@@ -523,17 +523,9 @@
 po, 
-resolvedP
+p
 ath,
@@ -810,16 +810,24 @@
 xternal)
+ %7B%0A     
  to = %60/
@@ -838,17 +838,42 @@
 ge.name%7D
-%60
+$%7Bparse(href).hash%7D%60%0A    %7D
 %0A    els

fix urls with hashes