Skip to content

Commit 50e5909

Browse files
Merge pull request #34 from bit-docs/27-section-linking
#27 - allow linking to a section with hash syntax
2 parents 5209c66 + 76c2c19 commit 50e5909

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

build/build_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
require("./make_default_helpers_test");
22

33
var getRenderer = require('./get_renderer'),
44
getPartials = require('./get_partials'),

build/make_default_helpers.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,23 +267,34 @@ module.exports = function(docMap, config, getCurrent, Handlebars){
267267
var replacer = function (match, content) {
268268
var parts = content.match(linkRegExp),
269269
name,
270+
hashParts,
270271
description,
271272
linkText,
272-
docObject;
273+
docObject,
274+
href;
273275

274276
name = parts ? parts[1].replace('::', '.prototype.') : content;
275277

278+
//the name can be something like 'some-name#someId'
279+
//this allows linking to a specific section with the hash syntax (#27)
280+
hashParts = name.split("#");
281+
name = hashParts.shift();
282+
276283
docObject = docMap[name]
277284
if (docObject) {
278285
linkText = parts && parts[2] ? parts[2] : docObject.title || name;
279286
description = docObject.description || name;
280287

281-
return '<a href="' + urlTo(name) + '" title="' + stripMarkdown(description) + '">' + linkText + '</a>';
288+
//if there is anything in the hashParts, append it to the end of the url
289+
href = urlTo(name) + (hashParts.length >= 1 ? ("#" + hashParts.join("#")) : "");
290+
291+
return '<a href="' + href + '" title="' + stripMarkdown(description) + '">' + linkText + '</a>';
282292
}
283293

284294
if (httpRegExp.test(name)) {
285295
linkText = parts && parts[2] ? parts[2] : name;
286-
return '<a href="' + name + '" title="' + escape(linkText) + '">' + linkText + '</a>';
296+
href = name;
297+
return '<a href="' + href + '" title="' + escape(linkText) + '">' + linkText + '</a>';
287298
}
288299

289300
return match;

build/make_default_helpers_test.js

Lines changed: 77 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)