url_for.js 637 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. const url = require('url');
  3. function urlForHelper(path = '/', options) {
  4. if (path[0] === '#' || path.startsWith('//')) {
  5. return path;
  6. }
  7. const config = this.config;
  8. const root = config.root;
  9. const data = url.parse(path);
  10. options = Object.assign({
  11. relative: config.relative_link
  12. }, options);
  13. // Exit if this is an external path
  14. if (data.protocol) {
  15. return path;
  16. }
  17. // Resolve relative url
  18. if (options.relative) {
  19. return this.relative_url(this.path, path);
  20. }
  21. // Prepend root path
  22. path = root + path;
  23. return path.replace(/\/{2,}/g, '/');
  24. }
  25. module.exports = urlForHelper;