summaryrefslogtreecommitdiffstats
path: root/lib/utils/location.js
blob: 17edc003f332cb0b5bae3e8b2d76aaa4cd5dc5f1 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var url = require('url');
var path = require('path');

// Is the url an external url
function isExternal(href) {
    try {
        return Boolean(url.parse(href).protocol) && !isDataURI(href);
    } catch(err) {
        return false;
    }
}

// Is the url an iniline data-uri
function isDataURI(href) {
    try {
        return Boolean(url.parse(href).protocol) && (url.parse(href).protocol === 'data:');
    } catch(err) {
        return false;
    }
}

// Inverse of isExternal
function isRelative(href) {
    return !isExternal(href);
}

// Return true if the link is an achor
function isAnchor(href) {
    try {
        var parsed = url.parse(href);
        return !!(!parsed.protocol && !parsed.path && parsed.hash);
    } catch(err) {
        return false;
    }
}

// Normalize a path to be a link
function normalize(s) {
    return path.normalize(s).replace(/\\/g, '/');
}

/**
    Convert a relative path to absolute

    @param {String} href
    @param {String} dir: directory parent of the file currently in rendering process
    @param {String} outdir: directory parent from the html output
    @return {String}
*/
function toAbsolute(_href, dir, outdir) {
    if (isExternal(_href) || isDataURI(_href)) {
        return _href;
    }

    outdir = outdir == undefined? dir : outdir;

    _href = normalize(_href);
    dir = normalize(dir);
    outdir = normalize(outdir);

    // Path "_href" inside the base folder
    var hrefInRoot = normalize(path.join(dir, _href));
    if (_href[0] == '/') {
        hrefInRoot = normalize(_href.slice(1));
    }

    // Make it relative to output
    _href = path.relative(outdir, hrefInRoot);

    // Normalize windows paths
    _href = normalize(_href);

    return _href;
}

/**
    Convert an absolute path to a relative path for a specific folder (dir)
    ('test/', 'hello.md') -> '../hello.md'

    @param {String} dir: current directory
    @param {String} file: absolute path of file
    @return {String}
*/
function relative(dir, file) {
    var isDirectory = file.slice(-1) === '/';
    return normalize(path.relative(dir, file)) + (isDirectory? '/': '');
}

/**
    Convert an absolute path to a relative path for a specific folder (dir)
    ('test/test.md', 'hello.md') -> '../hello.md'

    @param {String} baseFile: current file
    @param {String} file: absolute path of file
    @return {String}
*/
function relativeForFile(baseFile, file) {
    return relative(path.dirname(baseFile), file);
}

/**
    Compare two paths, return true if they are identical
    ('README.md', './README.md') -> true

    @param {String} p1: first path
    @param {String} p2: second path
    @return {Boolean}
*/
function areIdenticalPaths(p1, p2) {
    return normalize(p1) === normalize(p2);
}

module.exports = {
    areIdenticalPaths: areIdenticalPaths,
    isDataURI: isDataURI,
    isExternal: isExternal,
    isRelative: isRelative,
    isAnchor: isAnchor,
    normalize: normalize,
    toAbsolute: toAbsolute,
    relative: relative,
    relativeForFile: relativeForFile
};