fix: jump to anchor after reload
This commit is contained in:
Dolphin Wood
2015-05-18 14:28:08 +08:00
parent c15e784013
commit 46fee9e21f
7 changed files with 24 additions and 8 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
A plugin that generates table of contents(TOC) dynamically for both jQuery or non-jQuery.
A plugin that generates table of contents(TOC) automatically for both jQuery or non-jQuery.
## Install
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "jquery-toc",
"version": "0.0.1",
"version": "0.0.2",
"homepage": "https://github.com/idiotWu/jQuery-TOC",
"authors": [
"Dolphin Wood <dolphin.w.e@gmail.com>"
@@ -23,6 +23,6 @@
"tests"
],
"dependencies": {
"jquery": "~2.1.4"
"jquery": "*"
}
}
+2 -2
View File
@@ -3,7 +3,7 @@
* Table of Contents Generator Plugin for (non-)jQuery
*
* @author Dolphin Wood <dolphin.w.e@gmail.com>
* @version 0.0.1
* @version 0.0.2
* Copyright 2015. MIT licensed.
*/
!function(e){"function"==typeof define&&define.amd?define("jquery.toc",["jquery"],e):e(jQuery)}(function(e){"use strict";var n=function(e){if("string"!=typeof e)return 0;var n=e.match(/\d/g);return n?Math.min.apply(null,n):1},t=function(n,t){for(;t--;)n=e("<ol/>").appendTo(n);return n},r=function(e,n){return 0>n?r(e.parent(),n+1):n>0?t(e,n):e.parent()},o=function(e,n){return function(t,r,o){var i=t.text(),c=n+"-"+o;r.text(i);var f=t[0],a=r[0],u=e?c:f.id||c;u=encodeURIComponent(u),f.id=u,a.href="#"+u}},i=function(t){var i=t.selector,c=t.scope,f=e("<ol/>"),a=f,u=n(i),p=o(t.overwrite,t.prefix);return e(c).find(i).each(function(t,o){var i=n(o.tagName),c=i-u,h=e("<li/>"),s=e("<a/>"),d=r(a,c);d.length||(d=f),p(e(o),s,t),h.append(s).appendTo(d),a=h,u=i}),f};e.fn.initTOC=function(n){var t={selector:"h1, h2, h3, h4, h5, h6",scope:"body",overwrite:!1,prefix:"toc"};n=e.extend(t,n);var r=n.selector;if("string"!=typeof r)throw new TypeError("selector must be a string");if(!r.match(/^(?:h[1-6],?\s*)*$/g))throw new TypeError("selector must contains only h1-6");return e(this).append(i(n)),e(this)}});
!function(e){"function"==typeof define&&define.amd?define("jquery.toc",["jquery"],e):e(jQuery)}(function(e){"use strict";var n=function(e){if("string"!=typeof e)return 0;var n=e.match(/\d/g);return n?Math.min.apply(null,n):1},t=function(n,t){for(;t--;)n=e("<ol/>").appendTo(n);return n},r=function(e,n){return 0>n?r(e.parent(),n+1):n>0?t(e,n):e.parent()},o=function(e,n){return function(t,r,o){var i=t.text(),a=n+"-"+o;r.text(i);var c=t[0],f=r[0],u=e?a:c.id||a;u=encodeURIComponent(u),c.id=u,f.href="#"+u}},i=function(t){var i=t.selector,a=t.scope,c=e("<ol/>"),f=c,u=n(i),h=o(t.overwrite,t.prefix);return e(a).find(i).each(function(t,o){var i=n(o.tagName),a=i-u,p=e("<li/>"),s=e("<a/>"),l=r(f,a);l.length||(l=c),h(e(o),s,t),p.append(s).appendTo(l),f=p,u=i}),c};e.fn.initTOC=function(n){var t={selector:"h1, h2, h3, h4, h5, h6",scope:"body",overwrite:!1,prefix:"toc"};n=e.extend(t,n);var r=n.selector;if("string"!=typeof r)throw new TypeError("selector must be a string");if(!r.match(/^(?:h[1-6],?\s*)*$/g))throw new TypeError("selector must contains only h1-6");e(this).append(i(n));var o=location.hash;return o&&(location.hash="",location.hash=o),e(this)}});
+2 -2
View File
@@ -3,7 +3,7 @@
* Table of Contents Generator Plugin for (non-)jQuery
*
* @author Dolphin Wood <dolphin.w.e@gmail.com>
* @version 0.0.1
* @version 0.0.2
* Copyright 2015. MIT licensed.
*/
!function(e){"use strict";var t=function(e,t){for(var n in t)t.hasOwnProperty(n)&&t[n]&&(e[n]=t[n]);return e},n=function(e,t){var n=[],r=document.querySelectorAll(t);return Array.prototype.forEach.call(r,function(t){var r=t.querySelectorAll(e);n=n.concat(Array.prototype.slice.call(r))}),n},r=function(e){if("string"!=typeof e)return 0;var t=e.match(/\d/g);return t?Math.min.apply(null,t):1},o=function(e,t){for(;t--;){var n=document.createElement("ol");e.appendChild(n),e=n}return e},c=function(e,t){return 0>t?c(e.parentElement,t+1):t>0?o(e,t):e.parentElement},i=function(e,t){return function(n,r,o){var c=(n.textContent,t+"-"+o);r.textContent=n.textContent;var i=e?c:n.id||c;i=encodeURIComponent(i),n.id=i,r.href="#"+i}},a=function(e){var t=e.selector,o=e.scope,a=document.createElement("ol"),u=a,l=i(e.overwrite,e.prefix);return n(t,o).reduce(function(e,t,n){var o=r(t.tagName),i=o-e,f=c(u,i)||a,p=document.createElement("li"),d=document.createElement("a");return l(t,d,n),f.appendChild(p).appendChild(d),u=p,o},r(t)),a},u=function(e){var n={selector:"h1, h2, h3, h4, h5, h6",scope:"body",overwrite:!1,prefix:"toc"};e=t(n,e);var r=e.selector;if("string"!=typeof r)throw new TypeError("selector must be a string");if(!r.match(/^(?:h[1-6],?\s*)*$/g))throw new TypeError("selector must contains only h1-6");return a(e)};"function"==typeof define&&define.amd?define("toc",[],function(){return u}):e.initTOC=u}(window);
!function(e){"use strict";var t=function(e,t){for(var n in t)t.hasOwnProperty(n)&&t[n]&&(e[n]=t[n]);return e},n=function(e,t){var n=[],r=document.querySelectorAll(t);return Array.prototype.forEach.call(r,function(t){var r=t.querySelectorAll(e);n=n.concat(Array.prototype.slice.call(r))}),n},r=function(e){if("string"!=typeof e)return 0;var t=e.match(/\d/g);return t?Math.min.apply(null,t):1},o=function(e,t){for(;t--;){var n=document.createElement("ol");e.appendChild(n),e=n}return e},c=function(e,t){return 0>t?c(e.parentElement,t+1):t>0?o(e,t):e.parentElement},a=function(e,t){return function(n,r,o){var c=(n.textContent,t+"-"+o);r.textContent=n.textContent;var a=e?c:n.id||c;a=encodeURIComponent(a),n.id=a,r.href="#"+a}},i=function(e){var t=e.selector,o=e.scope,i=document.createElement("ol"),u=i,l=a(e.overwrite,e.prefix);return n(t,o).reduce(function(e,t,n){var o=r(t.tagName),a=o-e,f=c(u,a)||i,h=document.createElement("li"),p=document.createElement("a");return l(t,p,n),f.appendChild(h).appendChild(p),u=h,o},r(t)),i},u=function(e){var n={selector:"h1, h2, h3, h4, h5, h6",scope:"body",overwrite:!1,prefix:"toc"};e=t(n,e);var r=e.selector;if("string"!=typeof r)throw new TypeError("selector must be a string");if(!r.match(/^(?:h[1-6],?\s*)*$/g))throw new TypeError("selector must contains only h1-6");var o=location.hash;return o&&(location.hash="",location.hash=o),i(e)};"function"==typeof define&&define.amd?define("toc",[],function(){return u}):e.initTOC=u}(window);
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "jQuery-TOC",
"version": "0.0.1",
"version": "0.0.2",
"description": "Table of Contents Generator Plugin for (non-)jQuery",
"main": "./dist/toc.min.js",
"copyright": "2015",
+8
View File
@@ -155,6 +155,14 @@
$(this).append(buildTOC(options));
var currentHash = location.hash;
if (currentHash) {
// fix hash
location.hash = '';
location.hash = currentHash;
}
return $(this);
};
}));
+8
View File
@@ -117,6 +117,14 @@
throw new TypeError('selector must contains only h1-6')
}
var currentHash = location.hash;
if (currentHash) {
// fix hash
location.hash = '';
location.hash = currentHash;
}
return buildTOC(options);
};