nest sublist like ol > li > ol > li, resolve #1
This commit is contained in:
Vendored
+1
-1
@@ -6,4 +6,4 @@
|
||||
* @version 0.0.4
|
||||
* Copyright 2015. MIT licensed.
|
||||
*/
|
||||
!function(n){"function"==typeof define&&define.amd?define(["jquery"],n):n(jQuery)}(function(n){"use strict";var e=function(n){if("string"!=typeof n)return 0;var e=n.match(/\d/g);return e?Math.min.apply(null,e):1},t=function(e,t){for(;t--;)e=n("<ol/>").appendTo(e);return e},r=function(n,e){for(;e--;)n=n.parent();return n},o=function(n,e){return function(t,r,o){var i=t.text(),a=e+"-"+o;r.text(i);var c=t[0],f=r[0],u=n?a:c.id||a;u=encodeURIComponent(u),c.id=u,f.href="#"+u}},i=function(i){var a=i.selector,c=i.scope,f=n("<ol/>"),u=f,h=null,s=e(a),p=o(i.overwrite,i.prefix);return n(c).find(a).each(function(o,i){var a=e(i.tagName),c=a-s;c>0&&(u=t(h,c)),0>c&&(u=r(u,1-c)),u.length||(u=f);var l=n("<li/>"),d=n("<a/>");p(n(i),d,o),l.append(d).appendTo(u),h=l,s=a}),f};n.fn.initTOC=function(e){var t={selector:"h1, h2, h3, h4, h5, h6",scope:"body",overwrite:!1,prefix:"toc"};e=n.extend(t,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");n(this).append(i(e));var o=location.hash;return o&&setTimeout(function(){location.hash="",location.hash=o},0),n(this)}});
|
||||
!function(n){"function"==typeof define&&define.amd?define(["jquery"],n):n(jQuery)}(function(n){"use strict";var e=function(n){if("string"!=typeof n)return 0;var e=n.match(/\d/g);return e?Math.min.apply(null,e):1},t=function(e,t){for(;t--;)e=n("<ol/>").appendTo(e),t&&(e=n("<li/>").appendTo(e));return e},r=function(n,e){for(;e--;)n=n.parent();return n},o=function(n,e){return function(t,r,o){var i=t.text(),a=e+"-"+o;r.text(i);var c=t[0],f=r[0],u=n?a:c.id||a;u=encodeURIComponent(u),c.id=u,f.href="#"+u}},i=function(i){var a=i.selector,c=i.scope,f=n("<ol/>"),u=f,h=null,p=e(a),s=o(i.overwrite,i.prefix);return n(c).find(a).each(function(o,i){var a=e(i.tagName),c=a-p;c>0&&(u=t(h,c)),0>c&&(u=r(u,2*-c)),u.length||(u=f);var l=n("<li/>"),d=n("<a/>");s(n(i),d,o),l.append(d).appendTo(u),h=l,p=a}),f};n.fn.initTOC=function(e){var t={selector:"h1, h2, h3, h4, h5, h6",scope:"body",overwrite:!1,prefix:"toc"};e=n.extend(t,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");n(this).append(i(e));var o=location.hash;return o&&setTimeout(function(){location.hash="",location.hash=o},0),n(this)}});
|
||||
Vendored
+1
-1
@@ -6,4 +6,4 @@
|
||||
* @version 0.0.4
|
||||
* 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){for(;t--;)e=e.parentElement;return e},i=function(e,t){return function(n,r,o){var c=n.textContent,i=t+"-"+o;r.textContent=c;var a=e?i:n.id||i;a=encodeURIComponent(a),n.id=a,r.href="#"+a}},a=function(e){var t=e.selector,a=e.scope,u=document.createElement("ol"),l=u,f=null,h=i(e.overwrite,e.prefix);return n(t,a).reduce(function(e,t,n){var i=r(t.tagName),a=i-e;a>0&&(l=o(f,a)),0>a&&(l=c(l,1-a)),l=l||u;var p=document.createElement("li"),d=document.createElement("a");return h(t,d,n),l.appendChild(p).appendChild(d),f=p,i},r(t)),u},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&&setTimeout(function(){location.hash="",location.hash=o},0),a(e)};"function"==typeof define&&define.amd?define(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--;)e=e.appendChild(document.createElement("ol")),t&&(e=e.appendChild(document.createElement("li")));return e},c=function(e,t){for(;t--;)e=e.parentElement;return e},i=function(e,t){return function(n,r,o){var c=n.textContent,i=t+"-"+o;r.textContent=c;var a=e?i:n.id||i;a=encodeURIComponent(a),n.id=a,r.href="#"+a}},a=function(e){var t=e.selector,a=e.scope,u=document.createElement("ol"),l=u,f=null,h=i(e.overwrite,e.prefix);return n(t,a).reduce(function(e,t,n){var i=r(t.tagName),a=i-e;a>0&&(l=o(f,a)),0>a&&(l=c(l,1-a)),l=l||u;var p=document.createElement("li"),d=document.createElement("a");return h(t,d,n),l.appendChild(p).appendChild(d),f=p,i},r(t)),u},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&&setTimeout(function(){location.hash="",location.hash=o},0),a(e)};"function"==typeof define&&define.amd?define(function(){return u}):e.initTOC=u}(window);
|
||||
+8
-4
@@ -25,7 +25,7 @@
|
||||
};
|
||||
|
||||
/**
|
||||
* create ul/ol
|
||||
* create ordered list
|
||||
* @param {jQuert} $wrapper
|
||||
* @param {Number} count
|
||||
*
|
||||
@@ -34,6 +34,10 @@
|
||||
var createList = function ($wrapper, count) {
|
||||
while (count--) {
|
||||
$wrapper = $('<ol/>').appendTo($wrapper);
|
||||
|
||||
if (count) {
|
||||
$wrapper = $('<li/>').appendTo($wrapper);
|
||||
}
|
||||
}
|
||||
|
||||
return $wrapper;
|
||||
@@ -106,8 +110,8 @@
|
||||
}
|
||||
|
||||
if (offset < 0) {
|
||||
// should be one more level to jump back
|
||||
// eg: (h2 > h3) + h2, offset = h2 - h3 = -1
|
||||
// should be once more level to jump back
|
||||
// eg: h2 + h3 + h2, offset = h2 - h3 = -1
|
||||
//
|
||||
// ol <------+ target
|
||||
// li |
|
||||
@@ -115,7 +119,7 @@
|
||||
// li
|
||||
//
|
||||
// jumpback = target - current = 2
|
||||
$wrapper = jumpBack($wrapper, 1 - offset);
|
||||
$wrapper = jumpBack($wrapper, -offset * 2);
|
||||
}
|
||||
|
||||
if (!$wrapper.length) {
|
||||
|
||||
+9
-3
@@ -34,9 +34,15 @@
|
||||
|
||||
var createList = function (wrapper, count) {
|
||||
while (count--) {
|
||||
var ol = document.createElement('ol');
|
||||
wrapper.appendChild(ol);
|
||||
wrapper = ol;
|
||||
wrapper = wrapper.appendChild(
|
||||
document.createElement('ol')
|
||||
);
|
||||
|
||||
if (count) {
|
||||
wrapper = wrapper.appendChild(
|
||||
document.createElement('li')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return wrapper;
|
||||
|
||||
Reference in New Issue
Block a user