Skip to content

Commit cc3c677

Browse files
author
Mat Ryer
committed
Added first version of hashGenerator option
1 parent a323579 commit cc3c677

File tree

3 files changed

+118
-6
lines changed

3 files changed

+118
-6
lines changed

src/javascripts/jquery.tocify.js

+53-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
$.widget("toc.tocify", {
2424

2525
//Plugin version
26-
version: "1.2.0",
26+
version: "1.2.1",
2727

2828
// These options will be used as defaults
2929
options: {
@@ -94,7 +94,16 @@
9494

9595
// **history**: Accepts a boolean: true or false
9696
// Adds a hash to the page url to maintain history
97-
history: true
97+
history: true,
98+
99+
// **hashGenerator**: How the hash value (the anchor segment of the URL, following the
100+
// # character) will be generated.
101+
//
102+
// "compact" (default) - #CompressesEverythingTogether
103+
// "pretty" - #looks-like-a-nice-url-and-is-easily-readable
104+
// function(text){} - Your own hash generation function that accepts the text as an
105+
// argument, and returns the hash value.
106+
hashGenerator: "compact"
98107

99108
},
100109

@@ -301,13 +310,15 @@
301310

302311
}
303312

313+
var hashValue = this._generateHashValue(arr, self, index);
314+
304315
// Appends a list item HTML element to the last unordered list HTML element found within the HTML element calling the plugin
305316
item = $("<li/>", {
306317

307318
// Sets a common class name to the list item
308319
"class": "item",
309320

310-
"data-unique": (!arr.length ? self.text() : self.text() + index).replace(/\s/g, "")
321+
"data-unique": hashValue
311322

312323
}).append($("<a/>", {
313324

@@ -319,16 +330,53 @@
319330
self.before($("<div/>", {
320331

321332
// Sets a name attribute on the anchor tag to the text of the currently traversed HTML element (also making sure that all whitespace is replaced with an underscore)
322-
"name": self.text().replace(/\s/g, ""),
333+
"name": hashValue,
323334

324-
"data-unique": (!arr.length ? self.text() : self.text() + index).replace(/\s/g, "")
335+
"data-unique": hashValue
325336

326337
}));
327338

328339
return item;
329340

330341
},
331342

343+
// _generateHashValue
344+
// ------------------
345+
// Generates the hash value that will be used to refer to each item.
346+
_generateHashValue: function(arr, self, index) {
347+
348+
var hashValue = "";
349+
350+
if (this.options.hashGenerator == "pretty") {
351+
352+
// pretify the text
353+
var hashValue = self.text().toLowerCase().replace(/\s/g, "-");
354+
355+
// fix double hyphens
356+
while (hashValue.indexOf("--") > -1) {
357+
hashValue = hashValue.replace(/--/g, "-");
358+
}
359+
360+
} else if (typeof this.options.hashGenerator == "function") {
361+
362+
// call the function
363+
hashValue = this.options.hashGenerator(self.text(), self);
364+
365+
} else {
366+
367+
// compact - the default
368+
hashValue = self.text().replace(/\s/g, "");
369+
370+
}
371+
372+
// add the index if we need to
373+
if (arr.length) { hashValue += ""+index; }
374+
375+
// return the value
376+
return hashValue;
377+
378+
},
379+
332380
// _appendElements
333381
// ---------------
334382
// Helps create the table of contents list by appending subheader elements

test/spec/javascripts/fixtures/tocifyFixture.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ <h4>History.js</h4>
5454
Note: If you do not include History.js, then Tocify will not have history support, but it will still be functional.
5555
</p>
5656
<br />
57-
<h1>Getting Started</h1>
57+
<h1 class="getting-started-test-marker">Getting Started</h1>
5858
<br />
5959
<h3>Downloads</h3>
6060
<br />

test/spec/tocifySpec.js

+64
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,69 @@ describe("Tocify jQuery Plugin", function () {
3838

3939
});
4040

41+
describe("Hash generation", function(){
42+
43+
it("Should add an index if there are duplicate values", function(){
44+
45+
loadFixtures("tocifyFixture.html");
46+
47+
// set all H1s to the same text
48+
$("h1").text("The same value");
49+
50+
toc = $("#toc").tocify({ hashGenerator: "pretty", context: ".documentation", selectors: "h1, h3, h4" });
51+
52+
expect($("h1.getting-started-test-marker").eq(0).prev("div").eq(0).attr("name")).toBe("the-same-value3");
53+
54+
});
55+
56+
it("Should default to 'original'", function(){
57+
expect(toc.data("tocify").options["hashGenerator"]).toBe("compact");
58+
});
59+
60+
it("Should support the 'pretty' hashGenerator option", function(){
61+
62+
loadFixtures("tocifyFixture.html");
63+
toc = $("#toc").tocify({ hashGenerator: "pretty", context: ".documentation", selectors: "h1, h3, h4" });
64+
expect($("h1.getting-started-test-marker").eq(0).prev("div").eq(0).attr("name")).toBe("getting-started");
65+
66+
});
67+
68+
it("Should fix double hyphens in the 'pretty' hashGenerator option", function(){
69+
70+
loadFixtures("tocifyFixture.html");
71+
$("h1.getting-started-test-marker").text("Getting started")
72+
toc = $("#toc").tocify({ hashGenerator: "pretty", context: ".documentation", selectors: "h1, h3, h4" });
73+
expect($("h1.getting-started-test-marker").eq(0).prev("div").eq(0).attr("name")).toBe("getting-started");
74+
75+
});
76+
77+
it("Should support a function hashGenerator option", function(){
78+
79+
loadFixtures("tocifyFixture.html");
80+
var args = [];
81+
82+
toc = $("#toc").tocify({ hashGenerator: function(text, element){
83+
84+
// record this call
85+
args.push(arguments);
86+
87+
// return a test value
88+
return text + "(TEST)";
89+
90+
}, context: ".documentation", selectors: "h1, h3, h4" });
91+
92+
expect($("h1.getting-started-test-marker").eq(0).prev("div").eq(0).attr("name")).toBe($("h1.getting-started-test-marker").text() + "(TEST)");
93+
94+
// check the correct arguments were passed to the function too
95+
96+
// text
97+
expect(args[0][0], $("h1.getting-started-test-marker").text());
98+
99+
// element
100+
expect(args[0][1].attr("class"), "getting-started-test-marker");
101+
102+
});
103+
104+
});
41105

42106
});

0 commit comments

Comments
 (0)