Skip to content

Commit

Permalink
Merge pull request #50 from afeld/unicode
Browse files Browse the repository at this point in the history
support unicode characters
  • Loading branch information
afeld committed Jan 9, 2019
2 parents 3f4ae8f + bb1edf6 commit b597909
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
25 changes: 20 additions & 5 deletions bootstrap-toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,26 @@

generateUniqueIdBase: function(el) {
var text = $(el).text();
var anchor = text
.trim()
.toLowerCase()
.replace(/[^A-Za-z0-9]+/g, "-");
return anchor || el.tagName.toLowerCase();

// adapted from
// https://github.com/bryanbraun/anchorjs/blob/65fede08d0e4a705f72f1e7e6284f643d5ad3cf3/anchor.js#L237-L257

// Regex for finding the non-safe URL characters (many need escaping): & +$,:;=?@"#{}|^~[`%!'<>]./()*\ (newlines, tabs, backspace, & vertical tabs)
var nonsafeChars = /[& +$,:;=?@"#{}|^~[`%!'<>\]\.\/\(\)\*\\\n\t\b\v]/g,
urlText;

// Note: we trim hyphens after truncating because truncating can cause dangling hyphens.
// Example string: // " ⚡⚡ Don't forget: URL fragments should be i18n-friendly, hyphenated, short, and clean."
urlText = text
.trim() // "⚡⚡ Don't forget: URL fragments should be i18n-friendly, hyphenated, short, and clean."
.replace(/\'/gi, "") // "⚡⚡ Dont forget: URL fragments should be i18n-friendly, hyphenated, short, and clean."
.replace(nonsafeChars, "-") // "⚡⚡-Dont-forget--URL-fragments-should-be-i18n-friendly--hyphenated--short--and-clean-"
.replace(/-{2,}/g, "-") // "⚡⚡-Dont-forget-URL-fragments-should-be-i18n-friendly-hyphenated-short-and-clean-"
.substring(0, 64) // "⚡⚡-Dont-forget-URL-fragments-should-be-i18n-friendly-hyphenated-"
.replace(/^-+|-+$/gm, "") // "⚡⚡-Dont-forget-URL-fragments-should-be-i18n-friendly-hyphenated"
.toLowerCase(); // "⚡⚡-dont-forget-url-fragments-should-be-i18n-friendly-hyphenated"

return urlText || el.tagName.toLowerCase();
},

generateUniqueId: function(el) {
Expand Down
8 changes: 7 additions & 1 deletion test/toc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ describe("Toc", function() {
var el = document.createElement("h1");
el.innerHTML = "Some tExt- with aidan's /. stuff ";
var base = Toc.helpers.generateUniqueIdBase(el);
expect(base).to.eql("some-text-with-aidan-s-stuff");
expect(base).to.eql("some-text-with-aidans-stuff");
});

it("uses the tag name of the element if there's no text", function() {
var el = document.createElement("h1");
var base = Toc.helpers.generateUniqueIdBase(el);
expect(base).to.eql("h1");
});

it("handles unicode", function() {
var el = $("<h1>💃 🕺</h1>")[0];
var base = Toc.helpers.generateUniqueIdBase(el);
expect(base).to.eql("💃-🕺");
});
});

describe(".generateUniqueId()", function() {
Expand Down

0 comments on commit b597909

Please sign in to comment.