Skip to content

Commit

Permalink
Release 0.4.0
Browse files Browse the repository at this point in the history
Update automatic ID generation to support int'l character sets, and
prevent duplicate IDs.
  • Loading branch information
ndabas committed Aug 10, 2016
1 parent f79c46a commit c8992fa
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 20 deletions.
9 changes: 4 additions & 5 deletions README.md
Expand Up @@ -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 `<h2>Heading 2.1</h2>` will get the ID `Heading_2_1`.
For example, a heading like `<h2>Heading 2.1</h2>` will get the ID `Heading_2.1`.

## Alternatives

Expand Down
2 changes: 1 addition & 1 deletion 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"
Expand Down
12 changes: 6 additions & 6 deletions docs/index.html
Expand Up @@ -12,7 +12,7 @@ <h1>Table of Contents jQuery Plugin</h1>
<p><a class="btn btn-lg btn-success" href="assets/jquery.toc.zip">Download</a></p>

<p>
<span class="muted">Version 0.3.5</span> &middot;
<span class="muted">Version 0.4.0</span> &middot;
<a href="http://github.com/ndabas/toc">GitHub Project</a></p>
</div>

Expand Down Expand Up @@ -103,15 +103,15 @@ <h3>Automatic ID generation</h3>

<ul>
<li>
The ID must begin with a letter; so any non-letter (<code>[^A-Za-z]</code>) characters
are discarded from the beginning of the string.</li>
<li>For the rest of the ID, only letters and numbers are used from the heading text; all
other characters (<code>[^A-Za-z0-9]</code>) are converted to underscores.</li>
Space characters are converted to underscores. Multiple spaces are replaced with a
single underscore.</li>
<li>If the ID already exists, a suffix like "_1", "_2", etc. is tried till we get a unique
ID.</li>
</ul>

<p>
For example, a heading like <code>&lt;h2&gt;Heading 2.1&lt;/h2&gt;</code> will get the ID
<code>Heading_2_1</code>.</p>
<code>Heading_2.1</code>.</p>

<h2>Alternatives</h2>

Expand Down
32 changes: 26 additions & 6 deletions 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
*
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion 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": {
Expand Down
2 changes: 1 addition & 1 deletion toc.jquery.json
Expand Up @@ -8,7 +8,7 @@
"contents",
"headings"
],
"version": "0.3.5",
"version": "0.4.0",
"author": {
"name": "Nikhil Dabas",
"url": "http://www.nikhildabas.com/"
Expand Down

0 comments on commit c8992fa

Please sign in to comment.