Debian Codegraph @

debian/build_modules/relateurl/lib/relate/relativize.js

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