Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b425db0

Browse files
author
Aaron O'Mullan
committed
Allow external links in summary, fixes GitbookIO#300
1 parent 30c6d1c commit b425db0

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

lib/generate/template.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var path = require("path");
22
var swig = require('swig');
33
var hljs = require('highlight.js');
44

5+
var links = require('../utils/').links;
56
var pkg = require('../../package.json');
67

78
swig.setDefaults({
@@ -24,7 +25,6 @@ swig.setFilter('mdLink', function(link) {
2425
return link;
2526
});
2627

27-
2828
// Swig filter: highlight coloration
2929
swig.setFilter('code', function(code, lang) {
3030
try {
@@ -44,4 +44,9 @@ swig.setFilter('pathJoin', function(base, _path) {
4444
return path.join(base, _path);
4545
});
4646

47+
// Is a link an absolute link
48+
swig.setFilter('isExternalLink', function(link) {
49+
return links.isExternal(link);
50+
});
51+
4752
module.exports = swig;

lib/utils/links.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
var url = require('url');
22
var path = require('path');
33

4+
// Is the link an external link
5+
var isExternal = function(href) {
6+
return Boolean(url.parse(href).protocol);
7+
};
8+
49
// Return true if the link is relative
510
var isRelative = function(href) {
611
var parsed = url.parse(href);
@@ -41,6 +46,7 @@ var join = function() {
4146

4247
module.exports = {
4348
isRelative: isRelative,
49+
isExternal: isExternal,
4450
toAbsolute: toAbsolute,
4551
join: join
46-
};
52+
};

theme/templates/includes/book/summary.html

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
{% macro articles(_articles) %}
22
{% for item in _articles %}
3-
<li class="chapter {% if item._path == _input %}active{% endif %}" data-level="{{ item.level }}" {% if item.path %}data-path="{{ item.path|mdLink }}"{% endif %}>
3+
{% set externalLink = item.path|isExternalLink %}
4+
<li class="chapter {% if item._path == _input %}active{% endif %}" data-level="{{ item.level }}" {% if item.path && !externalLink %}data-path="{{ item.path|mdLink }}"{% endif %}>
45
{% if item.path %}
5-
<a href="{{ basePath }}/{{ item.path|mdLink }}">
6-
<i class="fa fa-check"></i> <b>{{ item.level }}.</b> {{ item.title }}
7-
</a>
6+
{% if !externalLink %}
7+
<a href="{{ basePath }}/{{ item.path|mdLink }}">
8+
<i class="fa fa-check"></i> <b>{{ item.level }}.</b> {{ item.title }}
9+
</a>
10+
{% else %}
11+
<a target="_blank" href="{{ item.path }}">
12+
<i class="fa fa-check"></i> <b>{{ item.level }}.</b> {{ item.title }}
13+
</a>
14+
{% endif %}
815
{% else %}
916
<span><b>{{ item.level }}.</b> {{ item.title }}</span>
1017
{% endif %}

0 commit comments

Comments
 (0)