Skip to content

Commit 97765ea

Browse files
authored
Merge pull request #357 from canjs/fix-description-link-paths
Fixed description link paths
2 parents ed5ab55 + e985993 commit 97765ea

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"devDependencies": {
4343
"bit-docs-generate-html": "^0.6.0",
44-
"bit-docs-generate-searchmap": "^0.1.2",
44+
"bit-docs-generate-searchmap": "^0.1.3",
4545
"bit-docs-html-toc": "^0.6.0",
4646
"connect": "^3.5.0",
4747
"normalize.css": "^5.0.0",

static/search.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,25 +505,33 @@ var Search = Control.extend({
505505
numResults: numResults,
506506
searchValue: this.searchTerm
507507
},{
508-
docUrl: function(){
508+
docUrl: function(url){
509509
if(self.options.pathPrefix){
510-
return self.options.pathPrefix + "/" + this.url;
510+
return self.options.pathPrefix + "/" + url;
511511
}
512512

513-
if(this.url.substr(-1) === "/"){
513+
if(url.substr(-1) === "/"){
514514
return this.url;
515515
}
516516

517-
return "/" + this.url;
517+
return "/" + url;
518518
},
519-
addTargetToExternalURLs: function(html){
519+
addTargetToExternalURLs: function(html, parentHref){
520520
var $html = $('<div>').html(html);
521521
$html.find('a').each(function(){
522522
var $a = $(this);
523-
var a = $a[0];
524-
if(a.hostname !== location.hostname || a.protocol !== location.protocol){
523+
var isLocal = this.hostname === location.hostname;
524+
var isRelative = this.getAttribute('href').indexOf(location.hostname) === -1;
525+
if(!isLocal || this.protocol !== location.protocol){
525526
$a.attr('target', '_blank');
526527
}
528+
if(isLocal && isRelative){
529+
// These links are generated by bit-docs and are relative to the page they are from
530+
// so we need to get the path prefix of the parent a tag
531+
var prefix = parentHref.substr(0, parentHref.lastIndexOf('/'));
532+
var href = prefix + '/' + $a.attr('href');
533+
$a.attr('href', href);
534+
}
527535
});
528536
return $html.contents();
529537
}

templates/search-results.stache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<ul>
1717
{{#each results as result}}
1818
<li>
19-
<a href="{{docUrl}}" title="{{result.name}}" class="result-name">
19+
<a href="{{docUrl(result.url)}}" title="{{result.name}}" class="result-name">
2020
{{#if result.title}}{{result.title}}{{else}}{{result.name}}{{/if}}
2121

2222

@@ -30,7 +30,7 @@
3030

3131
{{#if result.description}}
3232
<div class="result-description" title="{{result.description}}">
33-
{{{addTargetToExternalURLs(result.description)}}}
33+
{{{addTargetToExternalURLs(result.description, docUrl(result.url))}}}
3434
</div>
3535
{{/if}}
3636
</a>

0 commit comments

Comments
 (0)