Skip to content

Commit 089815a

Browse files
committed
Fix GitbookIO#1328: fix crash when anchor links
1 parent 7e8f1a9 commit 089815a

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

lib/output/modifiers/__tests__/resolveLinks.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ describe('resolveLinks', function() {
4040
});
4141

4242
describe('Anchor', function() {
43-
var TEST = '<p>This is a <a href="test/cool.md#an-anchor"></a></p>';
44-
4543
it('should prevent anchors in resolution', function() {
44+
var TEST = '<p>This is a <a href="test/cool.md#an-anchor"></a></p>';
4645
var $ = cheerio.load(TEST);
4746

4847
return resolveLinks('hello.md', resolveFileCustom, $)
@@ -51,6 +50,17 @@ describe('resolveLinks', function() {
5150
expect(link.attr('href')).toBe('test/cool.html#an-anchor');
5251
});
5352
});
53+
54+
it('should ignore pure anchor links', function() {
55+
var TEST = '<p>This is a <a href="#an-anchor"></a></p>';
56+
var $ = cheerio.load(TEST);
57+
58+
return resolveLinks('hello.md', resolveFileCustom, $)
59+
.then(function() {
60+
var link = $('a');
61+
expect(link.attr('href')).toBe('#an-anchor');
62+
});
63+
});
5464
});
5565

5666
describe('Custom Resolver', function() {

lib/output/modifiers/resolveLinks.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,18 @@ function resolveLinks(currentFile, resolveFile, $) {
3030

3131
// Split anchor
3232
var parsed = url.parse(href);
33-
href = parsed.pathname;
33+
href = parsed.pathname || '';
3434

35-
// Calcul absolute path for this
36-
href = LocationUtils.toAbsolute(href, currentDirectory, '.');
35+
if (href) {
36+
// Calcul absolute path for this
37+
href = LocationUtils.toAbsolute(href, currentDirectory, '.');
3738

38-
// Resolve file
39-
href = resolveFile(href);
39+
// Resolve file
40+
href = resolveFile(href);
4041

41-
// Convert back to relative
42-
href = LocationUtils.relative(currentDirectory, href);
42+
// Convert back to relative
43+
href = LocationUtils.relative(currentDirectory, href);
44+
}
4345

4446
// Add back anchor
4547
href = href + (parsed.hash || '');

0 commit comments

Comments
 (0)