var utils = require('../lib/utils');

var pkg = require('../package');
var aframeCurrentSha = 'master';
try {
  aframeCurrentSha = pkg.dependencies.aframe.split('#')[1];
} catch (e) {
}

var isUrl = utils.isUrl;
var urljoin = utils.urljoin;

hexo.extend.helper.register('github_release_url', function (version) {
  version = version || ('v' + this.config.aframe_version);
  return urljoin(this.config.github.aframe.url, 'releases', 'tag', version);
});

hexo.extend.helper.register('github_file_url', function (path) {
  return urljoin(this.config.github.aframe.url, 'blob', aframeCurrentSha, path);
});

hexo.extend.helper.register('website_github_edit_url', function (path) {
  if (path.indexOf('docs/') !== -1) {
    return urljoin(this.config.github.aframe.url, 'edit', 'master', path.replace(/\.html$/, '.md'));
  } else {
    return urljoin(this.config.github.aframe_site.url, 'edit', 'master', this.config.source_dir, path.replace(/\.html$/, '.md'));
  }
});

hexo.extend.helper.register('page_url', function (path, options) {
  return this.url_for(path, options).replace(/index\.html$/, '');
});

hexo.extend.helper.register('example_url', function (item) {
  return urljoin(this.url_for('examples'), item.section, item.slug) + '/';
});

hexo.extend.helper.register('docs_url_prefix', function (item) {
  return this.page.source.split('/', 2).join('/') + '/';
});

hexo.extend.helper.register('is_external_url', isUrl);

hexo.extend.filter.register('urljoin', urljoin);

hexo.extend.filter.register('after_render:html', function (str, data) {
  if (data.path.substr(-3) === '.md') {
    str = str.replace(/href="([^"]+)\/index\.md([^"]+)?"/g, 'href="$1/$2"')
             .replace(/href="([^"]+)\.md([^"]+)?"/g, 'href="$1.html$2"');
  }
  return str;
});

@@ -1,12 +1,42 @@
+var urllib = require('url');%0A%0A
 var utils = 
@@ -1654,140 +1654,476 @@
 %22%5D+)
-%5C/index%5C.md(%5B%5E%22%5D+)?%22/g, 'href=%22$1/$2%22')%0A             .replace(/href=%22(%5B%5E%22%5D+)%5C.md(%5B%5E%22%5D+)?%22/g, 'href=%22$1.html$2%22');%0A  %7D%0A  return str
+%22/g, function (origStr, p1) %7B%0A      return 'href=%22' + convertMarkdownToHtmlUrl(p1) + '%22';%0A    %7D);%0A  %7D%0A  return str;%0A%7D);%0A%0A%0Afunction convertMarkdownToHtmlUrl (url) %7B%0A  var urlObj = urllib.parse(url);%0A  if (!urlObj.pathname %7C%7C urlObj.pathname%5B0%5D === '/') %7B%0A    // Do not rewrite absolute URLs.%0A    return url;%0A  %7D%0A  urlObj.pathname = urlObj.pathname.replace(/%5C.md$/, '.html')%0A                                   .replace('/index.html', '/');%0A  return urllib.format(urlObj)
 ;%0A%7D
-);
 %0A

rewrite Markdown doc URLs even better (fixes #209)