From c8992fa4216f2c81df0dfbcdee1ff7edb09a16ef Mon Sep 17 00:00:00 2001 From: Nikhil Dabas Date: Wed, 10 Aug 2016 14:42:34 +0530 Subject: [PATCH] Release 0.4.0 Update automatic ID generation to support int'l character sets, and prevent duplicate IDs. --- README.md | 9 ++++----- bower.json | 2 +- docs/index.html | 12 ++++++------ jquery.toc.js | 32 ++++++++++++++++++++++++++------ package.json | 2 +- toc.jquery.json | 2 +- 6 files changed, 39 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 80d92df..c935694 100644 --- a/README.md +++ b/README.md @@ -69,12 +69,11 @@ assigned; if they do not, the plugin will generate and assign IDs automatically. The generated IDs are based on the text inside the headings, and uses two simple rules: -* The ID must begin with a letter; so any non-letter (`[^A-Za-z]`) characters are discarded from the - beginning of the string. -* For the rest of the ID, only letters and numbers are used from the heading text; all other - characters (`[^A-Za-z0-9]`) are converted to underscores. +* Space characters are converted to underscores. Multiple spaces are replaced with a single + underscore. +* If the ID already exists, a suffix like "_1", "_2", etc. is tried till we get a unique ID. -For example, a heading like `

Heading 2.1

` will get the ID `Heading_2_1`. +For example, a heading like `

Heading 2.1

` will get the ID `Heading_2.1`. ## Alternatives diff --git a/bower.json b/bower.json index 9d72115..6aba7e5 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "jquery.toc", - "version": "0.3.5", + "version": "0.4.0", "main": "./jquery.toc.js", "dependencies": { "jquery": ">=1.6.3" diff --git a/docs/index.html b/docs/index.html index fe43e8d..f4ec2fc 100644 --- a/docs/index.html +++ b/docs/index.html @@ -12,7 +12,7 @@

Table of Contents jQuery Plugin

Download

- Version 0.3.5 · + Version 0.4.0 · GitHub Project

@@ -103,15 +103,15 @@

Automatic ID generation

For example, a heading like <h2>Heading 2.1</h2> will get the ID - Heading_2_1.

+ Heading_2.1.

Alternatives

diff --git a/jquery.toc.js b/jquery.toc.js index 035c42b..5d6333d 100644 --- a/jquery.toc.js +++ b/jquery.toc.js @@ -1,8 +1,8 @@ /* * Table of Contents jQuery Plugin - jquery.toc * - * Copyright 2013 Nikhil Dabas - * + * Copyright 2013-2016 Nikhil Dabas + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * @@ -42,10 +42,30 @@ // Set up some automatic IDs if we do not already have them $(thisOptions.content).find(thisOptions.headings).attr("id", function (index, attr) { - // Generate a valid ID: must start with a letter, and contain only letters and - // numbers. All other characters are replaced with underscores. - return attr || - $(this).text().replace(/^[^A-Za-z]*/, "").replace(/[^A-Za-z0-9]+/g, "_"); + // In HTML5, the id attribute must be at least one character long and must not + // contain any space characters. + // + // We just use the HTML5 spec now because all browsers work fine with it. + // https://mathiasbynens.be/notes/html5-id-class + var generateUniqueId = function (text) { + // Generate a valid ID. Spaces are replaced with underscores. We also check if + // the ID already exists in the document. If so, we append "_1", "_2", etc. + // until we find an unused ID. + + if (text.length === 0) { + text = "?"; + } + + var baseId = text.replace(/\s+/g, "_"), suffix = "", count = 1; + + while (document.getElementById(baseId + suffix) !== null) { + suffix = "_" + count++; + } + + return baseId + suffix; + }; + + return attr || generateUniqueId($(this).text()); }).each(function () { // What level is the current heading? var elem = $(this), level = $.map(headingSelectors, function (selector, index) { diff --git a/package.json b/package.json index 973af8e..63e978e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jquery.toc", - "version": "0.3.5", + "version": "0.4.0", "description": "A minimal, tiny jQuery plugin that will generate a table of contents, drawing from headings on the page.", "main": "jquery.toc.js", "repository": { diff --git a/toc.jquery.json b/toc.jquery.json index 8650d7b..7369a93 100644 --- a/toc.jquery.json +++ b/toc.jquery.json @@ -8,7 +8,7 @@ "contents", "headings" ], - "version": "0.3.5", + "version": "0.4.0", "author": { "name": "Nikhil Dabas", "url": "http://www.nikhildabas.com/"