Blame view

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