Comparing version 3.2.1 to 4.0.0
@@ -0,1 +1,2 @@ | ||
declare type AnchorContainerTagNameProps = 'ul' | 'ol'; | ||
export declare type MokujiOption = { | ||
@@ -7,14 +8,5 @@ anchorType?: boolean; | ||
anchorLinkClassName?: string; | ||
anchorContainerTagName?: 'ul' | 'ol'; | ||
anchorContainerTagName?: AnchorContainerTagNameProps; | ||
}; | ||
export declare const renderAnchorLink: (headings: NodeListOf<HTMLHeadingElement>, anchors: NodeListOf<HTMLAnchorElement> | undefined, options: MokujiOption) => void; | ||
export declare class Mokuji { | ||
options: MokujiOption; | ||
storeIds: string[]; | ||
constructor(element: HTMLElement | null, externalOptions: MokujiOption); | ||
generateMokuji(headings: NodeListOf<HTMLHeadingElement>): HTMLUListElement | HTMLOListElement; | ||
generateHierarchyList(headings: NodeListOf<HTMLHeadingElement>, elementContainer: HTMLElement): void; | ||
censorshipId(headings: NodeListOf<HTMLHeadingElement>, textContent: string | null): string; | ||
generateAnchorText(text: string, type: boolean): string; | ||
removeDuplicateIds(headings: NodeListOf<HTMLHeadingElement>, elementContainer: HTMLElement): void; | ||
} | ||
export declare const Mokuji: (element: HTMLElement | null, externalOptions?: MokujiOption | undefined) => HTMLOListElement | undefined; | ||
export {}; |
@@ -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");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; | ||
var e=function(e,n){for(;e;){if(e===n)return!0;e=e.parentNode}return!1},n={anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},r=[],a=function(e,n){for(var a=n||"",t=1;t<=e.length;){var o=1===t?a:a+"_"+t;if(-1===r.indexOf(o)){r.push(a=o);break}t++}return a};exports.Mokuji=function(r,t){if(r){var o=Object.assign(n,t),i=function(e){return e.querySelectorAll("h1, h2, h3, h4, h5, h6")}(r),c=document.createElement(o.anchorContainerTagName||n.anchorContainerTagName);return function(n,r,t){for(var o=0,i=document.createElement("li"),c=document.createElement("a"),h=0;h<n.length;h++){var l=n[h],f=Number(l.tagName.substring(1));if(0!==o&&o<f){var u=document.createElement("ol");r.lastChild.appendChild(u),r=u}else if(0!==o&&o>f)for(var d=0;d<o-f;d++)e(r,r.parentNode)&&(r=r.parentNode.parentNode);var m=(g=t,C=void 0,C=(v=a(n,l.textContent),C=v.replace(/\s+/g,"_")).replace(/\&+/g,"").replace(/\&+/g,""),!0===g&&(C=function(e){return(e=encodeURIComponent(e)).replace(/\%+/g,".")}(C)),C);l.id=m;var s=c.cloneNode(!1);s.href="#"+m,s.textContent=l.textContent;var p=i.cloneNode(!1);p.appendChild(s),r.appendChild(p),o=f}var v,g,C}(i,c,!!o.anchorType),function(e,n){for(var r=n.getElementsByTagName("a"),a=function(n){var a=r[n].innerText,t=r[n].hash,o=Array.from(e).filter(function(e){return e.id===a});if(1===o.length)return"continue";var i=0;o.forEach(function(e){for(var n=e.id+"-"+i,a=0,o=Array.from(r);a<o.length;a++){var c=o[a];if(c.hash===t){c.href="#"+n;break}}e.id=n,i++})},t=0;t<r.length;t++)a(t)}(i,c),o.anchorLink&&function(e,n,r){if(n){var a=document.createElement("a");r.anchorLinkClassName&&a.classList.add(r.anchorLinkClassName),e.forEach(function(e){for(var t=e.id,o=0;o<n.length;o++){var i=n[o].hash;if(i.replace("#","")===t){var c=a.cloneNode(!1);c.setAttribute("href",i),r.anchorLinkSymbol&&(c.textContent=r.anchorLinkSymbol),r.anchorLinkBefore?e.insertBefore(c,e.firstChild):e.appendChild(c)}}})}}(i,c.querySelectorAll("a"),o),c}}; | ||
//# 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");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}; | ||
const e=(e,n)=>{for(;e;){if(e===n)return!0;e=e.parentNode}return!1},n={anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},t=[],o=(e,n)=>{let o=n||"",r=1;for(;r<=e.length;){const e=1===r?o:`${o}_${r}`;if(-1===t.indexOf(e)){o=e,t.push(o);break}r++}return o},r=(e,n)=>{let t=(e=>e.replace(/\s+/g,"_"))(e);return t=t.replace(/\&+/g,"").replace(/\&+/g,""),!0===n&&(t=(e=>(e=encodeURIComponent(e)).replace(/\%+/g,"."))(t)),t},a=(t,a)=>{if(!t)return;const c=Object.assign(n,a),l=(e=>e.querySelectorAll("h1, h2, h3, h4, h5, h6"))(t),i=document.createElement(c.anchorContainerTagName||n.anchorContainerTagName);return((n,t,a)=>{let c=0;const l=document.createElement("li"),i=document.createElement("a");for(let h=0;h<n.length;h++){const s=n[h],d=Number(s.tagName.substring(1));if(0!==c&&c<d){const e=document.createElement("ol");t.lastChild.appendChild(e),t=e}else if(0!==c&&c>d)for(let n=0;n<c-d;n++)e(t,t.parentNode)&&(t=t.parentNode.parentNode);const f=o(n,s.textContent),m=r(f,a);s.id=m;const p=i.cloneNode(!1);p.href=`#${m}`,p.textContent=s.textContent;const u=l.cloneNode(!1);u.appendChild(p),t.appendChild(u),c=d}})(l,i,!!c.anchorType),((e,n)=>{const t=n.getElementsByTagName("a");for(let n=0;n<t.length;n++){const o=t[n].innerText,r=t[n].hash,a=Array.from(e).filter(e=>e.id===o);if(1===a.length)continue;let c=0;a.forEach(e=>{const n=`${e.id}-${c}`;for(const e of Array.from(t))if(e.hash===r){e.href=`#${n}`;break}e.id=n,c++})}})(l,i),c.anchorLink&&((e,n,t)=>{if(!n)return;const o=document.createElement("a");t.anchorLinkClassName&&o.classList.add(t.anchorLinkClassName),e.forEach(e=>{const{id:r}=e;for(let a=0;a<n.length;a++){const{hash:c}=n[a];if(c.replace("#","")!==r)continue;const l=o.cloneNode(!1);l.setAttribute("href",c),t.anchorLinkSymbol&&(l.textContent=t.anchorLinkSymbol),t.anchorLinkBefore?e.insertBefore(l,e.firstChild):e.appendChild(l)}})})(l,i.querySelectorAll("a"),c),i};export{a as Mokuji}; | ||
//# 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");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}; | ||
var e=function(e,n){for(;e;){if(e===n)return!0;e=e.parentNode}return!1},n={anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},r=[],a=function(e,n){for(var a=n||"",t=1;t<=e.length;){var o=1===t?a:a+"_"+t;if(-1===r.indexOf(o)){r.push(a=o);break}t++}return a},t=function(r,t){if(r){var o=Object.assign(n,t),i=function(e){return e.querySelectorAll("h1, h2, h3, h4, h5, h6")}(r),c=document.createElement(o.anchorContainerTagName||n.anchorContainerTagName);return function(n,r,t){for(var o=0,i=document.createElement("li"),c=document.createElement("a"),h=0;h<n.length;h++){var l=n[h],f=Number(l.tagName.substring(1));if(0!==o&&o<f){var d=document.createElement("ol");r.lastChild.appendChild(d),r=d}else if(0!==o&&o>f)for(var u=0;u<o-f;u++)e(r,r.parentNode)&&(r=r.parentNode.parentNode);var m=(g=t,C=void 0,C=(v=a(n,l.textContent),C=v.replace(/\s+/g,"_")).replace(/\&+/g,"").replace(/\&+/g,""),!0===g&&(C=function(e){return(e=encodeURIComponent(e)).replace(/\%+/g,".")}(C)),C);l.id=m;var s=c.cloneNode(!1);s.href="#"+m,s.textContent=l.textContent;var p=i.cloneNode(!1);p.appendChild(s),r.appendChild(p),o=f}var v,g,C}(i,c,!!o.anchorType),function(e,n){for(var r=n.getElementsByTagName("a"),a=function(n){var a=r[n].innerText,t=r[n].hash,o=Array.from(e).filter(function(e){return e.id===a});if(1===o.length)return"continue";var i=0;o.forEach(function(e){for(var n=e.id+"-"+i,a=0,o=Array.from(r);a<o.length;a++){var c=o[a];if(c.hash===t){c.href="#"+n;break}}e.id=n,i++})},t=0;t<r.length;t++)a(t)}(i,c),o.anchorLink&&function(e,n,r){if(n){var a=document.createElement("a");r.anchorLinkClassName&&a.classList.add(r.anchorLinkClassName),e.forEach(function(e){for(var t=e.id,o=0;o<n.length;o++){var i=n[o].hash;if(i.replace("#","")===t){var c=a.cloneNode(!1);c.setAttribute("href",i),r.anchorLinkSymbol&&(c.textContent=r.anchorLinkSymbol),r.anchorLinkBefore?e.insertBefore(c,e.firstChild):e.appendChild(c)}}})}}(i,c.querySelectorAll("a"),o),c}};export{t as Mokuji}; | ||
//# 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");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}); | ||
!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},r={anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},t=[],a=function(e,n){for(var r=n||"",a=1;a<=e.length;){var o=1===a?r:r+"_"+a;if(-1===t.indexOf(o)){t.push(r=o);break}a++}return r};e.Mokuji=function(e,t){if(e){var o=Object.assign(r,t),i=function(e){return e.querySelectorAll("h1, h2, h3, h4, h5, h6")}(e),c=document.createElement(o.anchorContainerTagName||r.anchorContainerTagName);return function(e,r,t){for(var o=0,i=document.createElement("li"),c=document.createElement("a"),f=0;f<e.length;f++){var l=e[f],h=Number(l.tagName.substring(1));if(0!==o&&o<h){var d=document.createElement("ol");r.lastChild.appendChild(d),r=d}else if(0!==o&&o>h)for(var u=0;u<o-h;u++)n(r,r.parentNode)&&(r=r.parentNode.parentNode);var s=(g=t,C=void 0,C=(v=a(e,l.textContent),C=v.replace(/\s+/g,"_")).replace(/\&+/g,"").replace(/\&+/g,""),!0===g&&(C=function(e){return(e=encodeURIComponent(e)).replace(/\%+/g,".")}(C)),C);l.id=s;var m=c.cloneNode(!1);m.href="#"+s,m.textContent=l.textContent;var p=i.cloneNode(!1);p.appendChild(m),r.appendChild(p),o=h}var v,g,C}(i,c,!!o.anchorType),function(e,n){for(var r=n.getElementsByTagName("a"),t=function(n){var t=r[n].innerText,a=r[n].hash,o=Array.from(e).filter(function(e){return e.id===t});if(1===o.length)return"continue";var i=0;o.forEach(function(e){for(var n=e.id+"-"+i,t=0,o=Array.from(r);t<o.length;t++){var c=o[t];if(c.hash===a){c.href="#"+n;break}}e.id=n,i++})},a=0;a<r.length;a++)t(a)}(i,c),o.anchorLink&&function(e,n,r){if(n){var t=document.createElement("a");r.anchorLinkClassName&&t.classList.add(r.anchorLinkClassName),e.forEach(function(e){for(var a=e.id,o=0;o<n.length;o++){var i=n[o].hash;if(i.replace("#","")===a){var c=t.cloneNode(!1);c.setAttribute("href",i),r.anchorLinkSymbol&&(c.textContent=r.anchorLinkSymbol),r.anchorLinkBefore?e.insertBefore(c,e.firstChild):e.appendChild(c)}}})}}(i,c.querySelectorAll("a"),o),c}}}); | ||
//# sourceMappingURL=mokuji.umd.js.map |
{ | ||
"name": "mokuji.js", | ||
"version": "3.2.1", | ||
"version": "4.0.0", | ||
"description": "A table of content JavaScript Library", | ||
@@ -50,12 +50,10 @@ "scripts": { | ||
"devDependencies": { | ||
"@babel/plugin-proposal-optional-chaining": "^7.13.12", | ||
"@types/node": "~17.0.23", | ||
"cross-env": "^5.2.0", | ||
"husky": "^7.0.4", | ||
"lint-staged": "^12.3.7", | ||
"microbundle": "^0.14.2", | ||
"prettier": "^2.6.0", | ||
"prettier": "^2.6.1", | ||
"rimraf": "^3.0.2", | ||
"typescript": "~4.6.2" | ||
"typescript": "~4.6.3" | ||
} | ||
} |
@@ -17,7 +17,9 @@ mokuji.js | ||
let textElement = document.querySelector('.text'); | ||
let mokuji = new Mokuji(textElement); | ||
const textElement = document.querySelector('.text'); | ||
const mokujiList = Mokuji(textElement); | ||
let listElement = document.querySelector('.list'); | ||
listElement.appendChild(mokuji); | ||
if (!mokujiList) return; | ||
const listElement = document.querySelector('.list'); | ||
listElement?.appendChild(mokujiList); | ||
``` | ||
@@ -24,0 +26,0 @@ |
259
src/index.ts
import { hasParentNode, getHeadingsElement } from './dom'; | ||
import { replaceSpace2Underscore, convert2WikipediaStyleAnchor, getHeadingTagName2Number } from './utils'; | ||
type AnchorContainerTagNameProps = 'ul' | 'ol'; | ||
export type MokujiOption = { | ||
@@ -10,6 +12,17 @@ anchorType?: boolean; | ||
anchorLinkClassName?: string; | ||
anchorContainerTagName?: 'ul' | 'ol'; | ||
anchorContainerTagName?: AnchorContainerTagNameProps; | ||
}; | ||
export const renderAnchorLink = ( | ||
const defaultOptions = { | ||
anchorType: true, | ||
anchorLink: false, | ||
anchorLinkSymbol: '#', | ||
anchorLinkBefore: true, | ||
anchorLinkClassName: '', | ||
anchorContainerTagName: 'ol', | ||
} as const; | ||
const storeIds: string[] = []; | ||
const renderAnchorLink = ( | ||
headings: NodeListOf<HTMLHeadingElement>, | ||
@@ -57,164 +70,152 @@ anchors: NodeListOf<HTMLAnchorElement> | undefined, | ||
export class Mokuji { | ||
options: MokujiOption = { | ||
anchorType: true, | ||
anchorLink: false, | ||
anchorLinkSymbol: '#', | ||
anchorLinkBefore: true, | ||
anchorLinkClassName: '', | ||
anchorContainerTagName: 'ol', | ||
}; | ||
storeIds: string[] = []; | ||
const removeDuplicateIds = (headings: NodeListOf<HTMLHeadingElement>, elementContainer: HTMLElement) => { | ||
const anchors = elementContainer.getElementsByTagName('a'); | ||
constructor(element: HTMLElement | null, externalOptions: MokujiOption) { | ||
if (!element) { | ||
return; | ||
for (let i = 0; i < anchors.length; i++) { | ||
const id = anchors[i].innerText; | ||
const hash = anchors[i].hash; | ||
const matchedHeadings = Array.from(headings).filter((heading) => heading.id === id); | ||
if (matchedHeadings.length === 1) { | ||
continue; | ||
} | ||
// Merge the default options with the external options. | ||
this.options = Object.assign( | ||
// default options | ||
this.options, | ||
externalOptions, | ||
); | ||
// duplicated id | ||
let count = 0; | ||
const headings = getHeadingsElement(element); | ||
matchedHeadings.forEach((heading) => { | ||
const heading_id = `${heading.id}-${count}`; | ||
// mokuji start | ||
// generate mokuji list | ||
const mokuji = this.generateMokuji(headings); | ||
// search duplicate list | ||
for (const anchor of Array.from(anchors)) { | ||
if (anchor.hash === hash) { | ||
// update hash | ||
anchor.href = `#${heading_id}`; | ||
break; | ||
} | ||
} | ||
// setup anchor link | ||
if (this.options.anchorLink) { | ||
const anchors = mokuji.querySelectorAll('a'); | ||
renderAnchorLink(headings, anchors, this.options); | ||
} | ||
// @ts-ignore | ||
return mokuji; | ||
// update id | ||
heading.id = heading_id; | ||
count++; | ||
}); | ||
} | ||
}; | ||
generateMokuji(headings: NodeListOf<HTMLHeadingElement>) { | ||
let elementContainer = document.createElement(this.options.anchorContainerTagName || 'ol'); | ||
const censorshipId = (headings: NodeListOf<HTMLHeadingElement>, textContent: string | null) => { | ||
let id = textContent || ''; | ||
let suffix_count = 1; | ||
this.generateHierarchyList(headings, elementContainer); | ||
// IDが重複していた場合はsuffixを付ける | ||
while (suffix_count <= headings.length) { | ||
const tmp_id = suffix_count === 1 ? id : `${id}_${suffix_count}`; | ||
// remove duplicates by adding suffix | ||
this.removeDuplicateIds(headings, elementContainer); | ||
if (storeIds.indexOf(tmp_id) === -1) { | ||
id = tmp_id; | ||
storeIds.push(id); | ||
break; | ||
} | ||
return elementContainer; | ||
suffix_count++; | ||
} | ||
generateHierarchyList(headings: NodeListOf<HTMLHeadingElement>, elementContainer: HTMLElement) { | ||
let number = 0; | ||
const elementListClone = document.createElement('li'); | ||
const elementAnchorClone = document.createElement('a'); | ||
return id; | ||
}; | ||
for (let i = 0; i < headings.length; i++) { | ||
const heading = headings[i]; | ||
const currentNumber = getHeadingTagName2Number(heading.tagName); | ||
const generateAnchorText = (text: string, type: boolean) => { | ||
// convert spaces to _ | ||
let anchor = replaceSpace2Underscore(text); | ||
// check list hierarchy | ||
if (number !== 0 && number < currentNumber) { | ||
// number of the heading is large (small as heading) | ||
const nextElementOListClone = document.createElement('ol'); | ||
// @ts-ignore | ||
elementContainer.lastChild.appendChild(nextElementOListClone); | ||
elementContainer = nextElementOListClone; | ||
} else if (number !== 0 && number > currentNumber) { | ||
// number of heading is small (large as heading) | ||
for (let i = 0; i < number - currentNumber; i++) { | ||
if (hasParentNode(elementContainer, elementContainer.parentNode)) { | ||
// @ts-ignore | ||
elementContainer = elementContainer.parentNode.parentNode; | ||
} | ||
} | ||
} | ||
// remove & | ||
anchor = anchor.replace(/\&+/g, '').replace(/\&+/g, ''); | ||
const textContent = this.censorshipId(headings, heading.textContent); | ||
if (type === true) { | ||
anchor = convert2WikipediaStyleAnchor(anchor); | ||
} | ||
// headingへidを付与 | ||
const anchorText = this.generateAnchorText(textContent, !!this.options.anchorType); | ||
heading.id = anchorText; | ||
return anchor; | ||
}; | ||
// add to wrapper | ||
const elementAnchor = elementAnchorClone.cloneNode(false) as HTMLAnchorElement; | ||
elementAnchor.href = `#${anchorText}`; | ||
elementAnchor.textContent = heading.textContent; | ||
const elementList = elementListClone.cloneNode(false); | ||
elementList.appendChild(elementAnchor); | ||
elementContainer.appendChild(elementList); | ||
const generateHierarchyList = ( | ||
headings: NodeListOf<HTMLHeadingElement>, | ||
elementContainer: HTMLUListElement | HTMLOListElement, | ||
anchorType: boolean, | ||
) => { | ||
let number = 0; | ||
const elementListClone = document.createElement('li'); | ||
const elementAnchorClone = document.createElement('a'); | ||
// upadte current number | ||
number = currentNumber; | ||
} | ||
} | ||
for (let i = 0; i < headings.length; i++) { | ||
const heading = headings[i]; | ||
const currentNumber = getHeadingTagName2Number(heading.tagName); | ||
censorshipId(headings: NodeListOf<HTMLHeadingElement>, textContent: string | null) { | ||
let id = textContent || ''; | ||
let suffix_count = 1; | ||
// IDが重複していた場合はsuffixを付ける | ||
while (suffix_count <= headings.length) { | ||
const tmp_id = suffix_count === 1 ? id : `${id}_${suffix_count}`; | ||
if (this.storeIds.indexOf(tmp_id) === -1) { | ||
id = tmp_id; | ||
this.storeIds.push(id); | ||
break; | ||
// check list hierarchy | ||
if (number !== 0 && number < currentNumber) { | ||
// number of the heading is large (small as heading) | ||
const nextElementOListClone = document.createElement('ol'); | ||
// @ts-ignore | ||
elementContainer.lastChild.appendChild(nextElementOListClone); | ||
elementContainer = nextElementOListClone; | ||
} else if (number !== 0 && number > currentNumber) { | ||
// number of heading is small (large as heading) | ||
for (let i = 0; i < number - currentNumber; i++) { | ||
if (hasParentNode(elementContainer, elementContainer.parentNode)) { | ||
// @ts-ignore | ||
elementContainer = elementContainer.parentNode.parentNode; | ||
} | ||
} | ||
suffix_count++; | ||
} | ||
return id; | ||
} | ||
const textContent = censorshipId(headings, heading.textContent); | ||
generateAnchorText(text: string, type: boolean) { | ||
// convert spaces to _ | ||
let anchor = replaceSpace2Underscore(text); | ||
// headingへidを付与 | ||
const anchorText = generateAnchorText(textContent, anchorType); | ||
heading.id = anchorText; | ||
// remove & | ||
anchor = anchor.replace(/\&+/g, '').replace(/\&+/g, ''); | ||
// add to wrapper | ||
const elementAnchor = elementAnchorClone.cloneNode(false) as HTMLAnchorElement; | ||
elementAnchor.href = `#${anchorText}`; | ||
elementAnchor.textContent = heading.textContent; | ||
const elementList = elementListClone.cloneNode(false) as HTMLLIElement; | ||
elementList.appendChild(elementAnchor); | ||
if (type === true) { | ||
anchor = convert2WikipediaStyleAnchor(anchor); | ||
} | ||
elementContainer.appendChild(elementList); | ||
return anchor; | ||
// upadte current number | ||
number = currentNumber; | ||
} | ||
}; | ||
removeDuplicateIds(headings: NodeListOf<HTMLHeadingElement>, elementContainer: HTMLElement) { | ||
const anchors = elementContainer.getElementsByTagName('a'); | ||
export const Mokuji = (element: HTMLElement | null, externalOptions?: MokujiOption): HTMLOListElement | undefined => { | ||
if (!element) { | ||
return; | ||
} | ||
for (let i = 0; i < anchors.length; i++) { | ||
const id = anchors[i].innerText; | ||
const hash = anchors[i].hash; | ||
const matchedHeadings = Array.from(headings).filter((heading) => heading.id === id); | ||
// Merge the default options with the external options. | ||
const options = Object.assign( | ||
// default options | ||
defaultOptions, | ||
externalOptions, | ||
); | ||
if (matchedHeadings.length === 1) { | ||
continue; | ||
} | ||
const headings = getHeadingsElement(element); | ||
// duplicated id | ||
let count = 0; | ||
// mokuji start | ||
const elementContainer = document.createElement( | ||
options.anchorContainerTagName || defaultOptions.anchorContainerTagName, | ||
); | ||
matchedHeadings.forEach((heading) => { | ||
const heading_id = `${heading.id}-${count}`; | ||
// generate mokuji list | ||
generateHierarchyList(headings, elementContainer, !!options.anchorType); | ||
// search duplicate list | ||
for (const anchor of Array.from(anchors)) { | ||
if (anchor.hash === hash) { | ||
// update hash | ||
anchor.href = `#${heading_id}`; | ||
break; | ||
} | ||
} | ||
// remove duplicates by adding suffix | ||
removeDuplicateIds(headings, elementContainer); | ||
// update id | ||
heading.id = heading_id; | ||
count++; | ||
}); | ||
} | ||
// setup anchor link | ||
if (options.anchorLink) { | ||
const anchors = elementContainer.querySelectorAll('a'); | ||
renderAnchorLink(headings, anchors, options); | ||
} | ||
} | ||
return elementContainer; | ||
}; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
7
72
63093
257
1