diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-05-04 15:15:18 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-05-04 15:15:18 +0200 |
commit | f7fe976c7e6cb6984aae0050a91cd76f77d10062 (patch) | |
tree | a98ee5fb178d4a9335ee9126b48877f5d173e8c1 /theme/javascript/utils/url.js | |
parent | 756629b91d684d24ed71ff8d1323ee1c8230a47b (diff) | |
download | gitbook-f7fe976c7e6cb6984aae0050a91cd76f77d10062.zip gitbook-f7fe976c7e6cb6984aae0050a91cd76f77d10062.tar.gz gitbook-f7fe976c7e6cb6984aae0050a91cd76f77d10062.tar.bz2 |
Fix #197: use URI.js for calculation of relative url
Use bower for client dependencies
Diffstat (limited to 'theme/javascript/utils/url.js')
-rw-r--r-- | theme/javascript/utils/url.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/theme/javascript/utils/url.js b/theme/javascript/utils/url.js new file mode 100644 index 0000000..dbb1570 --- /dev/null +++ b/theme/javascript/utils/url.js @@ -0,0 +1,33 @@ +define([ + "vendors/URIjs/src/URI" +], function(URI) { + // Joins path segments. Preserves initial "/" and resolves ".." and "." + // Does not support using ".." to go above/outside the root. + // This means that join("foo", "../../bar") will not resolve to "../bar" + function join(baseUrl, url) { + var theUrl = new URI(url); + if (theUrl.is("relative")) { + theUrl = theUrl.absoluteTo(baseUrl); + } + return theUrl.toString(); + } + + // A simple function to get the dirname of a path + // Trailing slashes are ignored. Leading slash is preserved. + function dirname(path) { + return join(path, ".."); + } + + // test if a path or url is absolute + function isAbsolute(path) { + if (!path) return false; + + return (path[0] == "/" || path.indexOf("http://") == 0 || path.indexOf("https://") == 0); + } + + return { + dirname: dirname, + join: join, + isAbsolute: isAbsolute + }; +})
\ No newline at end of file |