mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 10:03:31 +00:00
Fix #628: preserve pure anchor links and hash in relative links
This commit is contained in:
+12
-1
@@ -15,12 +15,22 @@ 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
|
||||
@@ -61,6 +71,7 @@ var changeExtension = function(filename, newext) {
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
isAnchor: isAnchor,
|
||||
isRelative: isRelative,
|
||||
isExternal: isExternal,
|
||||
toAbsolute: toAbsolute,
|
||||
|
||||
+9
-3
@@ -1,5 +1,6 @@
|
||||
var Q = require('q');
|
||||
var _ = require('lodash');
|
||||
var url = require('url');
|
||||
var path = require('path');
|
||||
var cheerio = require('cheerio');
|
||||
var domSerializer = require('dom-serializer');
|
||||
@@ -186,8 +187,12 @@ function normalizeHtml(src, options) {
|
||||
var href = $(this).attr("href");
|
||||
if (!href) return;
|
||||
|
||||
if (links.isRelative(href)) {
|
||||
var absolutePath = path.join(options.base, href);
|
||||
if (links.isAnchor(href)) {
|
||||
// Keep it as it is
|
||||
} else if (links.isRelative(href)) {
|
||||
var parts = url.parse(path.join(options.base, href));
|
||||
var absolutePath = parts.pathname;
|
||||
var anchor = parts.hash;
|
||||
|
||||
// If is in navigation relative: transform as content
|
||||
if (options.navigation[absolutePath]) {
|
||||
@@ -195,8 +200,9 @@ function normalizeHtml(src, options) {
|
||||
}
|
||||
|
||||
// Transform as absolute
|
||||
href = links.toAbsolute(href, options.base, options.output);
|
||||
href = links.toAbsolute(href, options.base, options.output)+anchor;
|
||||
} else {
|
||||
// External links
|
||||
$(this).attr("target", "_blank");
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,13 @@ describe('Links', function () {
|
||||
assert(!links.isExternal("/folder/test.md"));
|
||||
});
|
||||
|
||||
it('should correctly test anchor links', function() {
|
||||
assert(links.isAnchor("#test"));
|
||||
assert(links.isAnchor(" #test"));
|
||||
assert(!links.isAnchor("https://google.fr#test"));
|
||||
assert(!links.isAnchor("test.md#test"));
|
||||
});
|
||||
|
||||
describe('toAbsolute', function() {
|
||||
it('should correctly transform as absolute', function() {
|
||||
assert.equal(links.toAbsolute("http://google.fr"), "http://google.fr");
|
||||
|
||||
Reference in New Issue
Block a user