Comparing version 3.2.0 to 3.2.1
export declare type MokujiOption = { | ||
anchorType: boolean; | ||
anchorLink: boolean; | ||
anchorLinkSymbol: string; | ||
anchorLinkBefore: boolean; | ||
anchorLinkClassName: string; | ||
anchorContainerTagName: 'ul' | 'ol'; | ||
anchorType?: boolean; | ||
anchorLink?: boolean; | ||
anchorLinkSymbol?: string; | ||
anchorLinkBefore?: boolean; | ||
anchorLinkClassName?: string; | ||
anchorContainerTagName?: 'ul' | 'ol'; | ||
}; | ||
@@ -13,3 +13,3 @@ export declare const renderAnchorLink: (headings: NodeListOf<HTMLHeadingElement>, anchors: NodeListOf<HTMLAnchorElement> | undefined, options: MokujiOption) => void; | ||
storeIds: string[]; | ||
constructor(element: HTMLElement, externalOptions: MokujiOption); | ||
constructor(element: HTMLElement | null, externalOptions: MokujiOption); | ||
generateMokuji(headings: NodeListOf<HTMLHeadingElement>): HTMLUListElement | HTMLOListElement; | ||
@@ -16,0 +16,0 @@ generateHierarchyList(headings: NodeListOf<HTMLHeadingElement>, elementContainer: HTMLElement): void; |
@@ -1,2 +0,2 @@ | ||
var e=function(e,r){for(;e;){if(e===r)return!0;e=e.parentNode}return!1},r=function(e,r,n){if(r){var t=document.createElement("a");t.classList.add(n.anchorLinkClassName),e.forEach(function(e){for(var o=e.id,a=0;a<r.length;a++){var i=r[a].hash;if(i.replace("#","")===o){var c=t.cloneNode(!1);c.setAttribute("href",i),c.textContent=n.anchorLinkSymbol,n.anchorLinkBefore?e.insertBefore(c,e.firstChild):e.appendChild(c)}}})}};exports.Mokuji=/*#__PURE__*/function(){function n(e,n){this.options=void 0,this.storeIds=[],this.options=Object.assign({anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},n);var t=function(e){return e.querySelectorAll("h1, h2, h3, h4, h5, h6")}(e),o=this.generateMokuji(t);if(this.options.anchorLink){var a=o.querySelectorAll("a");r(t,a,this.options)}return o}var t=n.prototype;return t.generateMokuji=function(e){var r=document.createElement(this.options.anchorContainerTagName);return this.generateHierarchyList(e,r),this.removeDuplicateIds(e,r),r},t.generateHierarchyList=function(r,n){for(var t=0,o=document.createElement("li"),a=document.createElement("a"),i=0;i<r.length;i++){var c=r[i],h=Number(c.tagName.substring(1));if(0!==t&&t<h){var s=document.createElement("ol");n.lastChild.appendChild(s),n=s}else if(0!==t&&t>h)for(var f=0;f<t-h;f++)e(n,n.parentNode)&&(n=n.parentNode.parentNode);var l=this.censorshipId(r,c.textContent),u=this.generateAnchorText(l,this.options.anchorType);c.id=u;var d=a.cloneNode(!1);d.href="#"+u,d.textContent=c.textContent;var p=o.cloneNode(!1);p.appendChild(d),n.appendChild(p),t=h}},t.censorshipId=function(e,r){for(var n=r||"",t=1;t<=e.length;){var o=1===t?n:n+"_"+t;if(-1===this.storeIds.indexOf(o)){this.storeIds.push(n=o);break}t++}return n},t.generateAnchorText=function(e,r){var n=function(e){return e.replace(/\s+/g,"_")}(e);return n=n.replace(/\&+/g,"").replace(/\&+/g,""),!0===r&&(n=function(e){return(e=encodeURIComponent(e)).replace(/\%+/g,".")}(n)),n},t.removeDuplicateIds=function(e,r){for(var n=r.getElementsByTagName("a"),t=function(r){var t=n[r].innerText,o=n[r].hash,a=Array.from(e).filter(function(e){return e.id===t});if(1===a.length)return"continue";var i=0;a.forEach(function(e){for(var r=e.id+"-"+i,t=0,a=Array.from(n);t<a.length;t++){var c=a[t];if(c.hash===o){c.href="#"+r;break}}e.id=r,i++})},o=0;o<n.length;o++)t(o)},n}(),exports.renderAnchorLink=r; | ||
var e=function(e,n){for(;e;){if(e===n)return!0;e=e.parentNode}return!1},n=function(e,n,r){if(n){var t=document.createElement("a");r.anchorLinkClassName&&t.classList.add(r.anchorLinkClassName),e.forEach(function(e){for(var o=e.id,a=0;a<n.length;a++){var i=n[a].hash;if(i.replace("#","")===o){var h=t.cloneNode(!1);h.setAttribute("href",i),r.anchorLinkSymbol&&(h.textContent=r.anchorLinkSymbol),r.anchorLinkBefore?e.insertBefore(h,e.firstChild):e.appendChild(h)}}})}};exports.Mokuji=/*#__PURE__*/function(){function r(e,r){if(this.options={anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},this.storeIds=[],e){this.options=Object.assign(this.options,r);var t=function(e){return e.querySelectorAll("h1, h2, h3, h4, h5, h6")}(e),o=this.generateMokuji(t);if(this.options.anchorLink){var a=o.querySelectorAll("a");n(t,a,this.options)}return o}}var t=r.prototype;return t.generateMokuji=function(e){var n=document.createElement(this.options.anchorContainerTagName||"ol");return this.generateHierarchyList(e,n),this.removeDuplicateIds(e,n),n},t.generateHierarchyList=function(n,r){for(var t=0,o=document.createElement("li"),a=document.createElement("a"),i=0;i<n.length;i++){var h=n[i],c=Number(h.tagName.substring(1));if(0!==t&&t<c){var s=document.createElement("ol");r.lastChild.appendChild(s),r=s}else if(0!==t&&t>c)for(var l=0;l<t-c;l++)e(r,r.parentNode)&&(r=r.parentNode.parentNode);var f=this.censorshipId(n,h.textContent),u=this.generateAnchorText(f,!!this.options.anchorType);h.id=u;var d=a.cloneNode(!1);d.href="#"+u,d.textContent=h.textContent;var p=o.cloneNode(!1);p.appendChild(d),r.appendChild(p),t=c}},t.censorshipId=function(e,n){for(var r=n||"",t=1;t<=e.length;){var o=1===t?r:r+"_"+t;if(-1===this.storeIds.indexOf(o)){this.storeIds.push(r=o);break}t++}return r},t.generateAnchorText=function(e,n){var r=function(e){return e.replace(/\s+/g,"_")}(e);return r=r.replace(/\&+/g,"").replace(/\&+/g,""),!0===n&&(r=function(e){return(e=encodeURIComponent(e)).replace(/\%+/g,".")}(r)),r},t.removeDuplicateIds=function(e,n){for(var r=n.getElementsByTagName("a"),t=function(n){var t=r[n].innerText,o=r[n].hash,a=Array.from(e).filter(function(e){return e.id===t});if(1===a.length)return"continue";var i=0;a.forEach(function(e){for(var n=e.id+"-"+i,t=0,a=Array.from(r);t<a.length;t++){var h=a[t];if(h.hash===o){h.href="#"+n;break}}e.id=n,i++})},o=0;o<r.length;o++)t(o)},r}(),exports.renderAnchorLink=n; | ||
//# sourceMappingURL=mokuji.js.map |
@@ -1,2 +0,2 @@ | ||
const e=(e,t)=>{for(;e;){if(e===t)return!0;e=e.parentNode}return!1},t=(e,t,n)=>{if(!t)return;const o=document.createElement("a");o.classList.add(n.anchorLinkClassName),e.forEach(e=>{const{id:r}=e;for(let s=0;s<t.length;s++){const{hash:a}=t[s];if(a.replace("#","")!==r)continue;const i=o.cloneNode(!1);i.setAttribute("href",a),i.textContent=n.anchorLinkSymbol,n.anchorLinkBefore?e.insertBefore(i,e.firstChild):e.appendChild(i)}})};class n{constructor(e,n){this.options=void 0,this.storeIds=[],this.options=Object.assign({anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},n);const o=(e=>e.querySelectorAll("h1, h2, h3, h4, h5, h6"))(e),r=this.generateMokuji(o);if(this.options.anchorLink){const e=r.querySelectorAll("a");t(o,e,this.options)}return r}generateMokuji(e){let t=document.createElement(this.options.anchorContainerTagName);return this.generateHierarchyList(e,t),this.removeDuplicateIds(e,t),t}generateHierarchyList(t,n){let o=0;const r=document.createElement("li"),s=document.createElement("a");for(let a=0;a<t.length;a++){const i=t[a],c=Number(i.tagName.substring(1));if(0!==o&&o<c){const e=document.createElement("ol");n.lastChild.appendChild(e),n=e}else if(0!==o&&o>c)for(let t=0;t<o-c;t++)e(n,n.parentNode)&&(n=n.parentNode.parentNode);const h=this.censorshipId(t,i.textContent),l=this.generateAnchorText(h,this.options.anchorType);i.id=l;const d=s.cloneNode(!1);d.href=`#${l}`,d.textContent=i.textContent;const p=r.cloneNode(!1);p.appendChild(d),n.appendChild(p),o=c}}censorshipId(e,t){let n=t||"",o=1;for(;o<=e.length;){const e=1===o?n:`${n}_${o}`;if(-1===this.storeIds.indexOf(e)){n=e,this.storeIds.push(n);break}o++}return n}generateAnchorText(e,t){let n=(e=>e.replace(/\s+/g,"_"))(e);return n=n.replace(/\&+/g,"").replace(/\&+/g,""),!0===t&&(n=(e=>(e=encodeURIComponent(e)).replace(/\%+/g,"."))(n)),n}removeDuplicateIds(e,t){const n=t.getElementsByTagName("a");for(let t=0;t<n.length;t++){const o=n[t].innerText,r=n[t].hash,s=Array.from(e).filter(e=>e.id===o);if(1===s.length)continue;let a=0;s.forEach(e=>{const t=`${e.id}-${a}`;for(const e of Array.from(n))if(e.hash===r){e.href=`#${t}`;break}e.id=t,a++})}}}export{n as Mokuji,t as renderAnchorLink}; | ||
const e=(e,t)=>{for(;e;){if(e===t)return!0;e=e.parentNode}return!1},t=(e,t,n)=>{if(!t)return;const o=document.createElement("a");n.anchorLinkClassName&&o.classList.add(n.anchorLinkClassName),e.forEach(e=>{const{id:r}=e;for(let s=0;s<t.length;s++){const{hash:a}=t[s];if(a.replace("#","")!==r)continue;const i=o.cloneNode(!1);i.setAttribute("href",a),n.anchorLinkSymbol&&(i.textContent=n.anchorLinkSymbol),n.anchorLinkBefore?e.insertBefore(i,e.firstChild):e.appendChild(i)}})};class n{constructor(e,n){if(this.options={anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},this.storeIds=[],!e)return;this.options=Object.assign(this.options,n);const o=(e=>e.querySelectorAll("h1, h2, h3, h4, h5, h6"))(e),r=this.generateMokuji(o);if(this.options.anchorLink){const e=r.querySelectorAll("a");t(o,e,this.options)}return r}generateMokuji(e){let t=document.createElement(this.options.anchorContainerTagName||"ol");return this.generateHierarchyList(e,t),this.removeDuplicateIds(e,t),t}generateHierarchyList(t,n){let o=0;const r=document.createElement("li"),s=document.createElement("a");for(let a=0;a<t.length;a++){const i=t[a],c=Number(i.tagName.substring(1));if(0!==o&&o<c){const e=document.createElement("ol");n.lastChild.appendChild(e),n=e}else if(0!==o&&o>c)for(let t=0;t<o-c;t++)e(n,n.parentNode)&&(n=n.parentNode.parentNode);const h=this.censorshipId(t,i.textContent),l=this.generateAnchorText(h,!!this.options.anchorType);i.id=l;const d=s.cloneNode(!1);d.href=`#${l}`,d.textContent=i.textContent;const p=r.cloneNode(!1);p.appendChild(d),n.appendChild(p),o=c}}censorshipId(e,t){let n=t||"",o=1;for(;o<=e.length;){const e=1===o?n:`${n}_${o}`;if(-1===this.storeIds.indexOf(e)){n=e,this.storeIds.push(n);break}o++}return n}generateAnchorText(e,t){let n=(e=>e.replace(/\s+/g,"_"))(e);return n=n.replace(/\&+/g,"").replace(/\&+/g,""),!0===t&&(n=(e=>(e=encodeURIComponent(e)).replace(/\%+/g,"."))(n)),n}removeDuplicateIds(e,t){const n=t.getElementsByTagName("a");for(let t=0;t<n.length;t++){const o=n[t].innerText,r=n[t].hash,s=Array.from(e).filter(e=>e.id===o);if(1===s.length)continue;let a=0;s.forEach(e=>{const t=`${e.id}-${a}`;for(const e of Array.from(n))if(e.hash===r){e.href=`#${t}`;break}e.id=t,a++})}}}export{n as Mokuji,t as renderAnchorLink}; | ||
//# sourceMappingURL=mokuji.modern.js.map |
@@ -1,2 +0,2 @@ | ||
var e=function(e,n){for(;e;){if(e===n)return!0;e=e.parentNode}return!1},n=function(e,n,r){if(n){var t=document.createElement("a");t.classList.add(r.anchorLinkClassName),e.forEach(function(e){for(var o=e.id,a=0;a<n.length;a++){var i=n[a].hash;if(i.replace("#","")===o){var c=t.cloneNode(!1);c.setAttribute("href",i),c.textContent=r.anchorLinkSymbol,r.anchorLinkBefore?e.insertBefore(c,e.firstChild):e.appendChild(c)}}})}},r=/*#__PURE__*/function(){function r(e,r){this.options=void 0,this.storeIds=[],this.options=Object.assign({anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},r);var t=function(e){return e.querySelectorAll("h1, h2, h3, h4, h5, h6")}(e),o=this.generateMokuji(t);if(this.options.anchorLink){var a=o.querySelectorAll("a");n(t,a,this.options)}return o}var t=r.prototype;return t.generateMokuji=function(e){var n=document.createElement(this.options.anchorContainerTagName);return this.generateHierarchyList(e,n),this.removeDuplicateIds(e,n),n},t.generateHierarchyList=function(n,r){for(var t=0,o=document.createElement("li"),a=document.createElement("a"),i=0;i<n.length;i++){var c=n[i],h=Number(c.tagName.substring(1));if(0!==t&&t<h){var s=document.createElement("ol");r.lastChild.appendChild(s),r=s}else if(0!==t&&t>h)for(var f=0;f<t-h;f++)e(r,r.parentNode)&&(r=r.parentNode.parentNode);var l=this.censorshipId(n,c.textContent),u=this.generateAnchorText(l,this.options.anchorType);c.id=u;var d=a.cloneNode(!1);d.href="#"+u,d.textContent=c.textContent;var p=o.cloneNode(!1);p.appendChild(d),r.appendChild(p),t=h}},t.censorshipId=function(e,n){for(var r=n||"",t=1;t<=e.length;){var o=1===t?r:r+"_"+t;if(-1===this.storeIds.indexOf(o)){this.storeIds.push(r=o);break}t++}return r},t.generateAnchorText=function(e,n){var r=function(e){return e.replace(/\s+/g,"_")}(e);return r=r.replace(/\&+/g,"").replace(/\&+/g,""),!0===n&&(r=function(e){return(e=encodeURIComponent(e)).replace(/\%+/g,".")}(r)),r},t.removeDuplicateIds=function(e,n){for(var r=n.getElementsByTagName("a"),t=function(n){var t=r[n].innerText,o=r[n].hash,a=Array.from(e).filter(function(e){return e.id===t});if(1===a.length)return"continue";var i=0;a.forEach(function(e){for(var n=e.id+"-"+i,t=0,a=Array.from(r);t<a.length;t++){var c=a[t];if(c.hash===o){c.href="#"+n;break}}e.id=n,i++})},o=0;o<r.length;o++)t(o)},r}();export{r as Mokuji,n as renderAnchorLink}; | ||
var e=function(e,n){for(;e;){if(e===n)return!0;e=e.parentNode}return!1},n=function(e,n,r){if(n){var t=document.createElement("a");r.anchorLinkClassName&&t.classList.add(r.anchorLinkClassName),e.forEach(function(e){for(var o=e.id,a=0;a<n.length;a++){var i=n[a].hash;if(i.replace("#","")===o){var h=t.cloneNode(!1);h.setAttribute("href",i),r.anchorLinkSymbol&&(h.textContent=r.anchorLinkSymbol),r.anchorLinkBefore?e.insertBefore(h,e.firstChild):e.appendChild(h)}}})}},r=/*#__PURE__*/function(){function r(e,r){if(this.options={anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},this.storeIds=[],e){this.options=Object.assign(this.options,r);var t=function(e){return e.querySelectorAll("h1, h2, h3, h4, h5, h6")}(e),o=this.generateMokuji(t);if(this.options.anchorLink){var a=o.querySelectorAll("a");n(t,a,this.options)}return o}}var t=r.prototype;return t.generateMokuji=function(e){var n=document.createElement(this.options.anchorContainerTagName||"ol");return this.generateHierarchyList(e,n),this.removeDuplicateIds(e,n),n},t.generateHierarchyList=function(n,r){for(var t=0,o=document.createElement("li"),a=document.createElement("a"),i=0;i<n.length;i++){var h=n[i],c=Number(h.tagName.substring(1));if(0!==t&&t<c){var s=document.createElement("ol");r.lastChild.appendChild(s),r=s}else if(0!==t&&t>c)for(var l=0;l<t-c;l++)e(r,r.parentNode)&&(r=r.parentNode.parentNode);var f=this.censorshipId(n,h.textContent),u=this.generateAnchorText(f,!!this.options.anchorType);h.id=u;var d=a.cloneNode(!1);d.href="#"+u,d.textContent=h.textContent;var p=o.cloneNode(!1);p.appendChild(d),r.appendChild(p),t=c}},t.censorshipId=function(e,n){for(var r=n||"",t=1;t<=e.length;){var o=1===t?r:r+"_"+t;if(-1===this.storeIds.indexOf(o)){this.storeIds.push(r=o);break}t++}return r},t.generateAnchorText=function(e,n){var r=function(e){return e.replace(/\s+/g,"_")}(e);return r=r.replace(/\&+/g,"").replace(/\&+/g,""),!0===n&&(r=function(e){return(e=encodeURIComponent(e)).replace(/\%+/g,".")}(r)),r},t.removeDuplicateIds=function(e,n){for(var r=n.getElementsByTagName("a"),t=function(n){var t=r[n].innerText,o=r[n].hash,a=Array.from(e).filter(function(e){return e.id===t});if(1===a.length)return"continue";var i=0;a.forEach(function(e){for(var n=e.id+"-"+i,t=0,a=Array.from(r);t<a.length;t++){var h=a[t];if(h.hash===o){h.href="#"+n;break}}e.id=n,i++})},o=0;o<r.length;o++)t(o)},r}();export{r as Mokuji,n as renderAnchorLink}; | ||
//# sourceMappingURL=mokuji.module.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e||self).mokujiJs={})}(this,function(e){var n=function(e,n){for(;e;){if(e===n)return!0;e=e.parentNode}return!1},t=function(e,n,t){if(n){var r=document.createElement("a");r.classList.add(t.anchorLinkClassName),e.forEach(function(e){for(var o=e.id,i=0;i<n.length;i++){var a=n[i].hash;if(a.replace("#","")===o){var c=r.cloneNode(!1);c.setAttribute("href",a),c.textContent=t.anchorLinkSymbol,t.anchorLinkBefore?e.insertBefore(c,e.firstChild):e.appendChild(c)}}})}};e.Mokuji=/*#__PURE__*/function(){function e(e,n){this.options=void 0,this.storeIds=[],this.options=Object.assign({anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},n);var r=function(e){return e.querySelectorAll("h1, h2, h3, h4, h5, h6")}(e),o=this.generateMokuji(r);if(this.options.anchorLink){var i=o.querySelectorAll("a");t(r,i,this.options)}return o}var r=e.prototype;return r.generateMokuji=function(e){var n=document.createElement(this.options.anchorContainerTagName);return this.generateHierarchyList(e,n),this.removeDuplicateIds(e,n),n},r.generateHierarchyList=function(e,t){for(var r=0,o=document.createElement("li"),i=document.createElement("a"),a=0;a<e.length;a++){var c=e[a],h=Number(c.tagName.substring(1));if(0!==r&&r<h){var s=document.createElement("ol");t.lastChild.appendChild(s),t=s}else if(0!==r&&r>h)for(var f=0;f<r-h;f++)n(t,t.parentNode)&&(t=t.parentNode.parentNode);var u=this.censorshipId(e,c.textContent),l=this.generateAnchorText(u,this.options.anchorType);c.id=l;var d=i.cloneNode(!1);d.href="#"+l,d.textContent=c.textContent;var p=o.cloneNode(!1);p.appendChild(d),t.appendChild(p),r=h}},r.censorshipId=function(e,n){for(var t=n||"",r=1;r<=e.length;){var o=1===r?t:t+"_"+r;if(-1===this.storeIds.indexOf(o)){this.storeIds.push(t=o);break}r++}return t},r.generateAnchorText=function(e,n){var t=function(e){return e.replace(/\s+/g,"_")}(e);return t=t.replace(/\&+/g,"").replace(/\&+/g,""),!0===n&&(t=function(e){return(e=encodeURIComponent(e)).replace(/\%+/g,".")}(t)),t},r.removeDuplicateIds=function(e,n){for(var t=n.getElementsByTagName("a"),r=function(n){var r=t[n].innerText,o=t[n].hash,i=Array.from(e).filter(function(e){return e.id===r});if(1===i.length)return"continue";var a=0;i.forEach(function(e){for(var n=e.id+"-"+a,r=0,i=Array.from(t);r<i.length;r++){var c=i[r];if(c.hash===o){c.href="#"+n;break}}e.id=n,a++})},o=0;o<t.length;o++)r(o)},e}(),e.renderAnchorLink=t}); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e||self).mokujiJs={})}(this,function(e){var n=function(e,n){for(;e;){if(e===n)return!0;e=e.parentNode}return!1},t=function(e,n,t){if(n){var r=document.createElement("a");t.anchorLinkClassName&&r.classList.add(t.anchorLinkClassName),e.forEach(function(e){for(var o=e.id,i=0;i<n.length;i++){var a=n[i].hash;if(a.replace("#","")===o){var c=r.cloneNode(!1);c.setAttribute("href",a),t.anchorLinkSymbol&&(c.textContent=t.anchorLinkSymbol),t.anchorLinkBefore?e.insertBefore(c,e.firstChild):e.appendChild(c)}}})}};e.Mokuji=/*#__PURE__*/function(){function e(e,n){if(this.options={anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},this.storeIds=[],e){this.options=Object.assign(this.options,n);var r=function(e){return e.querySelectorAll("h1, h2, h3, h4, h5, h6")}(e),o=this.generateMokuji(r);if(this.options.anchorLink){var i=o.querySelectorAll("a");t(r,i,this.options)}return o}}var r=e.prototype;return r.generateMokuji=function(e){var n=document.createElement(this.options.anchorContainerTagName||"ol");return this.generateHierarchyList(e,n),this.removeDuplicateIds(e,n),n},r.generateHierarchyList=function(e,t){for(var r=0,o=document.createElement("li"),i=document.createElement("a"),a=0;a<e.length;a++){var c=e[a],h=Number(c.tagName.substring(1));if(0!==r&&r<h){var s=document.createElement("ol");t.lastChild.appendChild(s),t=s}else if(0!==r&&r>h)for(var f=0;f<r-h;f++)n(t,t.parentNode)&&(t=t.parentNode.parentNode);var l=this.censorshipId(e,c.textContent),u=this.generateAnchorText(l,!!this.options.anchorType);c.id=u;var d=i.cloneNode(!1);d.href="#"+u,d.textContent=c.textContent;var p=o.cloneNode(!1);p.appendChild(d),t.appendChild(p),r=h}},r.censorshipId=function(e,n){for(var t=n||"",r=1;r<=e.length;){var o=1===r?t:t+"_"+r;if(-1===this.storeIds.indexOf(o)){this.storeIds.push(t=o);break}r++}return t},r.generateAnchorText=function(e,n){var t=function(e){return e.replace(/\s+/g,"_")}(e);return t=t.replace(/\&+/g,"").replace(/\&+/g,""),!0===n&&(t=function(e){return(e=encodeURIComponent(e)).replace(/\%+/g,".")}(t)),t},r.removeDuplicateIds=function(e,n){for(var t=n.getElementsByTagName("a"),r=function(n){var r=t[n].innerText,o=t[n].hash,i=Array.from(e).filter(function(e){return e.id===r});if(1===i.length)return"continue";var a=0;i.forEach(function(e){for(var n=e.id+"-"+a,r=0,i=Array.from(t);r<i.length;r++){var c=i[r];if(c.hash===o){c.href="#"+n;break}}e.id=n,a++})},o=0;o<t.length;o++)r(o)},e}(),e.renderAnchorLink=t}); | ||
//# sourceMappingURL=mokuji.umd.js.map |
{ | ||
"name": "mokuji.js", | ||
"version": "3.2.0", | ||
"version": "3.2.1", | ||
"description": "A table of content JavaScript Library", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -5,8 +5,8 @@ import { hasParentNode, getHeadingsElement } from './dom'; | ||
export type MokujiOption = { | ||
anchorType: boolean; | ||
anchorLink: boolean; | ||
anchorLinkSymbol: string; | ||
anchorLinkBefore: boolean; | ||
anchorLinkClassName: string; | ||
anchorContainerTagName: 'ul' | 'ol'; | ||
anchorType?: boolean; | ||
anchorLink?: boolean; | ||
anchorLinkSymbol?: string; | ||
anchorLinkBefore?: boolean; | ||
anchorLinkClassName?: string; | ||
anchorContainerTagName?: 'ul' | 'ol'; | ||
}; | ||
@@ -22,4 +22,7 @@ | ||
const a = document.createElement('a'); | ||
a.classList.add(options.anchorLinkClassName); | ||
if (options.anchorLinkClassName) { | ||
a.classList.add(options.anchorLinkClassName); | ||
} | ||
headings.forEach((heading) => { | ||
@@ -38,4 +41,7 @@ const { id } = heading; | ||
anchor.setAttribute('href', hash); | ||
anchor.textContent = options.anchorLinkSymbol; | ||
if (options.anchorLinkSymbol) { | ||
anchor.textContent = options.anchorLinkSymbol; | ||
} | ||
// insert anchor into headings | ||
@@ -54,17 +60,21 @@ if (options.anchorLinkBefore) { | ||
export class Mokuji { | ||
options: MokujiOption; | ||
options: MokujiOption = { | ||
anchorType: true, | ||
anchorLink: false, | ||
anchorLinkSymbol: '#', | ||
anchorLinkBefore: true, | ||
anchorLinkClassName: '', | ||
anchorContainerTagName: 'ol', | ||
}; | ||
storeIds: string[] = []; | ||
constructor(element: HTMLElement, externalOptions: MokujiOption) { | ||
constructor(element: HTMLElement | null, externalOptions: MokujiOption) { | ||
if (!element) { | ||
return; | ||
} | ||
// Merge the default options with the external options. | ||
this.options = Object.assign( | ||
// default options | ||
{ | ||
anchorType: true, | ||
anchorLink: false, | ||
anchorLinkSymbol: '#', | ||
anchorLinkBefore: true, | ||
anchorLinkClassName: '', | ||
anchorContainerTagName: 'ol', | ||
} as MokujiOption, | ||
this.options, | ||
externalOptions, | ||
@@ -90,3 +100,3 @@ ); | ||
generateMokuji(headings: NodeListOf<HTMLHeadingElement>) { | ||
let elementContainer = document.createElement(this.options.anchorContainerTagName); | ||
let elementContainer = document.createElement(this.options.anchorContainerTagName || 'ol'); | ||
@@ -130,3 +140,3 @@ this.generateHierarchyList(headings, elementContainer); | ||
// headingへidを付与 | ||
const anchorText = this.generateAnchorText(textContent, this.options.anchorType); | ||
const anchorText = this.generateAnchorText(textContent, !!this.options.anchorType); | ||
heading.id = anchorText; | ||
@@ -133,0 +143,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67239
272