You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.0 KiB

4 years ago
  1. "use strict";
  2. var pathUtils = require("../util/path");
  3. /*
  4. Get a path relative to the site path.
  5. */
  6. function relatePath(absolutePath, siteAbsolutePath)
  7. {
  8. var relativePath = [];
  9. // At this point, it's related to the host/port
  10. var related = true;
  11. var parentIndex = -1;
  12. // Find parents
  13. siteAbsolutePath.forEach( function(siteAbsoluteDir, i)
  14. {
  15. if (related)
  16. {
  17. if (absolutePath[i] !== siteAbsoluteDir)
  18. {
  19. related = false;
  20. }
  21. else
  22. {
  23. parentIndex = i;
  24. }
  25. }
  26. if (!related)
  27. {
  28. // Up one level
  29. relativePath.push("..");
  30. }
  31. });
  32. // Form path
  33. absolutePath.forEach( function(dir, i)
  34. {
  35. if (i > parentIndex)
  36. {
  37. relativePath.push(dir);
  38. }
  39. });
  40. return relativePath;
  41. }
  42. function relativize(urlObj, siteUrlObj, options)
  43. {
  44. if (urlObj.extra.relation.minimumScheme)
  45. {
  46. var pathArray = relatePath(urlObj.path.absolute.array, siteUrlObj.path.absolute.array);
  47. urlObj.path.relative.array = pathArray;
  48. urlObj.path.relative.string = pathUtils.join(pathArray);
  49. }
  50. }
  51. module.exports = relativize;