summaryrefslogtreecommitdiffstats
path: root/lib/utils/links.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils/links.js')
-rw-r--r--lib/utils/links.js40
1 files changed, 30 insertions, 10 deletions
diff --git a/lib/utils/links.js b/lib/utils/links.js
index b4d2fb7..aa7c241 100644
--- a/lib/utils/links.js
+++ b/lib/utils/links.js
@@ -15,32 +15,43 @@ var isRelative = function(href) {
try {
var parsed = url.parse(href);
- return !parsed.protocol && parsed.path && parsed.path[0] != '/';
+ return !!(!parsed.protocol && parsed.path);
} catch(err) {}
return true;
};
+// Return true if the link is an achor
+var isAnchor = function(href) {
+ try {
+ var parsed = url.parse(href);
+ return !!(!parsed.protocol && !parsed.path && parsed.hash);
+ } catch(err) {}
+
+ return false;
+};
+
// Relative to absolute path
// dir: directory parent of the file currently in rendering process
// outdir: directory parent from the html output
var toAbsolute = function(_href, dir, outdir) {
- // Absolute file in source
- _href = path.join(dir, _href);
+ if (isExternal(_href)) return _href;
- // make it relative to output
- _href = path.relative(outdir, _href);
+ // Path '_href' inside the base folder
+ var hrefInRoot = path.normalize(path.join(dir, _href));
+ if (_href[0] == "/") hrefInRoot = path.normalize(_href.slice(1));
- if (process.platform === 'win32') {
- _href = _href.replace(/\\/g, '/');
- }
+ // Make it relative to output
+ _href = path.relative(outdir, hrefInRoot);
+
+ // Normalize windows paths
+ _href = _href.replace(/\\/g, '/');
return _href;
};
// Join links
-
var join = function() {
var _href = path.join.apply(path, arguments);
@@ -51,10 +62,19 @@ var join = function() {
return _href;
};
+// Change extension
+var changeExtension = function(filename, newext) {
+ return path.join(
+ path.dirname(filename),
+ path.basename(filename, path.extname(filename))+newext
+ );
+};
module.exports = {
+ isAnchor: isAnchor,
isRelative: isRelative,
isExternal: isExternal,
toAbsolute: toAbsolute,
- join: join
+ join: join,
+ changeExtension: changeExtension
};