Comparing version 3.0.1 to 3.1.0
@@ -1,22 +0,19 @@ | ||
declare type MokujiOption = { | ||
anchorType: Boolean; | ||
anchorLink: Boolean; | ||
export declare type MokujiOption = { | ||
anchorType: boolean; | ||
anchorLink: boolean; | ||
anchorLinkSymbol: string; | ||
anchorLinkBefore: Boolean; | ||
anchorLinkBefore: boolean; | ||
anchorLinkClassName: string; | ||
anchorContainerTagName: string; | ||
anchorContainerTagName: 'ul' | 'ol'; | ||
}; | ||
export default class Mokuji { | ||
headings: NodeListOf<HTMLHeadingElement>; | ||
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, externalOptions: MokujiOption); | ||
render(): HTMLElement; | ||
generateMokuji(): HTMLElement; | ||
generateHierarchyList(elementContainer: HTMLElement): void; | ||
censorshipId(textContent: string | null): string; | ||
setAnchor(text: string, type: Boolean): string; | ||
renderAnchorLink(anchors: NodeListOf<HTMLAnchorElement> | undefined): void; | ||
removeDuplicateIds(anchors: HTMLCollectionOf<HTMLAnchorElement>): void; | ||
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 {}; |
@@ -1,2 +0,2 @@ | ||
var e=function(e,r){for(;e;){if(e===r)return!0;e=e.parentNode}return!1},r=function(e){var r=e.match(/\d/g).join("");return Number(r)};module.exports=function(){function n(e,r){return this.storeIds=[],this.options=Object.assign({anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},r),this.headings=function(e){return e.querySelectorAll("h1, h2, h3, h4, h5, h6")}(e),this.render()}var t=n.prototype;return t.render=function(){var e=this.generateMokuji();if(this.options.anchorLink){var r=null==e?void 0:e.querySelectorAll("a");this.renderAnchorLink(r)}return e},t.generateMokuji=function(){var e=document.createElement(this.options.anchorContainerTagName);this.generateHierarchyList(e);var r=e.getElementsByTagName("a");return this.removeDuplicateIds(r),e},t.generateHierarchyList=function(n){for(var t=0,o=document.createElement("li"),a=document.createElement("a"),i=0;i<this.headings.length;i++){var h=this.headings[i],s=r(h.tagName);if(0!==t&&t<s){var c=document.createElement("ol");n.lastChild.appendChild(c),n=c}else if(0!==t&&t>s)for(var d=0;d<t-s;d++)e(n,n.parentNode)&&(n=n.parentNode.parentNode);var l=this.censorshipId(h.textContent);h.id=this.setAnchor(l,this.options.anchorType);var u=a.cloneNode(!1);u.href="#"+h.id,u.textContent=h.textContent;var f=o.cloneNode(!1);f.appendChild(u),n.appendChild(f),t=s}},t.censorshipId=function(e){for(var r=e||"",n=1;n<=this.headings.length;){var t=1===n?r:r+"_"+n;if(-1===this.storeIds.indexOf(t)){this.storeIds.push(r=t);break}n++}return r},t.setAnchor=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.renderAnchorLink=function(e){if(e){var r=document.createElement("a");r.classList.add(this.options.anchorLinkClassName);for(var n=0;n<e.length;n++){var t=e[n].hash,o=document.querySelector('[id="'+t.replace("#","")+'"]');if(o){var a=r.cloneNode(!1);a.setAttribute("href",t),a.textContent=this.options.anchorLinkSymbol,this.options.anchorLinkBefore?o.insertBefore(a,o.firstChild):o.appendChild(a)}}}},t.removeDuplicateIds=function(e){for(var r=0;r<e.length;r++){var n=e[r].hash,t=document.querySelectorAll('[id="'+e[r].innerText+'"]');if(1!==t.length)for(var o=0,a=0,i=Array.from(t);a<i.length;a++){for(var h=i[a],s=h.id+"-"+o,c=0,d=Array.from(e);c<d.length;c++){var l=d[c];if(l.hash===n){l.href="#"+s;break}}h.id=s,o++}}},n}(); | ||
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=function(){function n(e,n){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; | ||
//# 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=>{const t=e.match(/\d/g).join("");return Number(t)};export default class{constructor(e,t){return this.storeIds=[],this.options=Object.assign({anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},t),this.headings=(e=>e.querySelectorAll("h1, h2, h3, h4, h5, h6"))(e),this.render()}render(){const e=this.generateMokuji();if(this.options.anchorLink){const t=null==e?void 0:e.querySelectorAll("a");this.renderAnchorLink(t)}return e}generateMokuji(){let e=document.createElement(this.options.anchorContainerTagName);this.generateHierarchyList(e);const t=e.getElementsByTagName("a");return this.removeDuplicateIds(t),e}generateHierarchyList(n){let o=0;const r=document.createElement("li"),s=document.createElement("a");for(let i=0;i<this.headings.length;i++){const a=this.headings[i],c=t(a.tagName);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(a.textContent);a.id=this.setAnchor(h,this.options.anchorType);const l=s.cloneNode(!1);l.href="#"+a.id,l.textContent=a.textContent;const d=r.cloneNode(!1);d.appendChild(l),n.appendChild(d),o=c}}censorshipId(e){let t=e||"",n=1;for(;n<=this.headings.length;){const e=1===n?t:`${t}_${n}`;if(-1===this.storeIds.indexOf(e)){t=e,this.storeIds.push(t);break}n++}return t}setAnchor(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}renderAnchorLink(e){if(!e)return;const t=document.createElement("a");t.classList.add(this.options.anchorLinkClassName);for(let n=0;n<e.length;n++){const o=e[n].hash,r=document.querySelector(`[id="${o.replace("#","")}"]`);if(!r)continue;const s=t.cloneNode(!1);s.setAttribute("href",o),s.textContent=this.options.anchorLinkSymbol,this.options.anchorLinkBefore?r.insertBefore(s,r.firstChild):r.appendChild(s)}}removeDuplicateIds(e){for(let t=0;t<e.length;t++){const n=e[t].hash,o=document.querySelectorAll(`[id="${e[t].innerText}"]`);if(1===o.length)continue;let r=0;for(const t of Array.from(o)){const o=`${t.id}-${r}`;for(const t of Array.from(e))if(t.hash===n){t.href=`#${o}`;break}t.id=o,r++}}}} | ||
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.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}; | ||
//# sourceMappingURL=mokuji.modern.js.map |
@@ -1,2 +0,2 @@ | ||
var e=function(e,r){for(;e;){if(e===r)return!0;e=e.parentNode}return!1},r=function(e){var r=e.match(/\d/g).join("");return Number(r)},n=function(){function n(e,r){return this.storeIds=[],this.options=Object.assign({anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},r),this.headings=function(e){return e.querySelectorAll("h1, h2, h3, h4, h5, h6")}(e),this.render()}var t=n.prototype;return t.render=function(){var e=this.generateMokuji();if(this.options.anchorLink){var r=null==e?void 0:e.querySelectorAll("a");this.renderAnchorLink(r)}return e},t.generateMokuji=function(){var e=document.createElement(this.options.anchorContainerTagName);this.generateHierarchyList(e);var r=e.getElementsByTagName("a");return this.removeDuplicateIds(r),e},t.generateHierarchyList=function(n){for(var t=0,o=document.createElement("li"),a=document.createElement("a"),i=0;i<this.headings.length;i++){var h=this.headings[i],s=r(h.tagName);if(0!==t&&t<s){var c=document.createElement("ol");n.lastChild.appendChild(c),n=c}else if(0!==t&&t>s)for(var d=0;d<t-s;d++)e(n,n.parentNode)&&(n=n.parentNode.parentNode);var l=this.censorshipId(h.textContent);h.id=this.setAnchor(l,this.options.anchorType);var u=a.cloneNode(!1);u.href="#"+h.id,u.textContent=h.textContent;var f=o.cloneNode(!1);f.appendChild(u),n.appendChild(f),t=s}},t.censorshipId=function(e){for(var r=e||"",n=1;n<=this.headings.length;){var t=1===n?r:r+"_"+n;if(-1===this.storeIds.indexOf(t)){this.storeIds.push(r=t);break}n++}return r},t.setAnchor=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.renderAnchorLink=function(e){if(e){var r=document.createElement("a");r.classList.add(this.options.anchorLinkClassName);for(var n=0;n<e.length;n++){var t=e[n].hash,o=document.querySelector('[id="'+t.replace("#","")+'"]');if(o){var a=r.cloneNode(!1);a.setAttribute("href",t),a.textContent=this.options.anchorLinkSymbol,this.options.anchorLinkBefore?o.insertBefore(a,o.firstChild):o.appendChild(a)}}}},t.removeDuplicateIds=function(e){for(var r=0;r<e.length;r++){var n=e[r].hash,t=document.querySelectorAll('[id="'+e[r].innerText+'"]');if(1!==t.length)for(var o=0,a=0,i=Array.from(t);a<i.length;a++){for(var h=i[a],s=h.id+"-"+o,c=0,d=Array.from(e);c<d.length;c++){var l=d[c];if(l.hash===n){l.href="#"+s;break}}h.id=s,o++}}},n}();export default n; | ||
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)}}})}},n=function(){function n(e,n){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}();export{n as Mokuji,r as renderAnchorLink}; | ||
//# sourceMappingURL=mokuji.module.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e||self).mokujiJs=n()}(this,function(){var e=function(e,n){for(;e;){if(e===n)return!0;e=e.parentNode}return!1},n=function(e){var n=e.match(/\d/g).join("");return Number(n)};return function(){function t(e,n){return this.storeIds=[],this.options=Object.assign({anchorType:!0,anchorLink:!1,anchorLinkSymbol:"#",anchorLinkBefore:!0,anchorLinkClassName:"",anchorContainerTagName:"ol"},n),this.headings=function(e){return e.querySelectorAll("h1, h2, h3, h4, h5, h6")}(e),this.render()}var r=t.prototype;return r.render=function(){var e=this.generateMokuji();if(this.options.anchorLink){var n=null==e?void 0:e.querySelectorAll("a");this.renderAnchorLink(n)}return e},r.generateMokuji=function(){var e=document.createElement(this.options.anchorContainerTagName);this.generateHierarchyList(e);var n=e.getElementsByTagName("a");return this.removeDuplicateIds(n),e},r.generateHierarchyList=function(t){for(var r=0,o=document.createElement("li"),i=document.createElement("a"),a=0;a<this.headings.length;a++){var h=this.headings[a],s=n(h.tagName);if(0!==r&&r<s){var c=document.createElement("ol");t.lastChild.appendChild(c),t=c}else if(0!==r&&r>s)for(var d=0;d<r-s;d++)e(t,t.parentNode)&&(t=t.parentNode.parentNode);var l=this.censorshipId(h.textContent);h.id=this.setAnchor(l,this.options.anchorType);var f=i.cloneNode(!1);f.href="#"+h.id,f.textContent=h.textContent;var u=o.cloneNode(!1);u.appendChild(f),t.appendChild(u),r=s}},r.censorshipId=function(e){for(var n=e||"",t=1;t<=this.headings.length;){var r=1===t?n:n+"_"+t;if(-1===this.storeIds.indexOf(r)){this.storeIds.push(n=r);break}t++}return n},r.setAnchor=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.renderAnchorLink=function(e){if(e){var n=document.createElement("a");n.classList.add(this.options.anchorLinkClassName);for(var t=0;t<e.length;t++){var r=e[t].hash,o=document.querySelector('[id="'+r.replace("#","")+'"]');if(o){var i=n.cloneNode(!1);i.setAttribute("href",r),i.textContent=this.options.anchorLinkSymbol,this.options.anchorLinkBefore?o.insertBefore(i,o.firstChild):o.appendChild(i)}}}},r.removeDuplicateIds=function(e){for(var n=0;n<e.length;n++){var t=e[n].hash,r=document.querySelectorAll('[id="'+e[n].innerText+'"]');if(1!==r.length)for(var o=0,i=0,a=Array.from(r);i<a.length;i++){for(var h=a[i],s=h.id+"-"+o,c=0,d=Array.from(e);c<d.length;c++){var l=d[c];if(l.hash===t){l.href="#"+s;break}}h.id=s,o++}}},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");r.classList.add(t.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=r.cloneNode(!1);c.setAttribute("href",i),c.textContent=t.anchorLinkSymbol,t.anchorLinkBefore?e.insertBefore(c,e.firstChild):e.appendChild(c)}}})}};e.Mokuji=function(){function e(e,n){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 a=o.querySelectorAll("a");t(r,a,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"),a=document.createElement("a"),i=0;i<e.length;i++){var c=e[i],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=a.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,a=Array.from(e).filter(function(e){return e.id===r});if(1===a.length)return"continue";var i=0;a.forEach(function(e){for(var n=e.id+"-"+i,r=0,a=Array.from(t);r<a.length;r++){var c=a[r];if(c.hash===o){c.href="#"+n;break}}e.id=n,i++})},o=0;o<t.length;o++)r(o)},e}(),e.renderAnchorLink=t}); | ||
//# sourceMappingURL=mokuji.umd.js.map |
{ | ||
"name": "mokuji.js", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "A table of content JavaScript Library", | ||
@@ -10,3 +10,3 @@ "scripts": { | ||
"test": "npm run lint", | ||
"prepublish": "npm run build" | ||
"prepare": "npm run clean && npm run build" | ||
}, | ||
@@ -48,11 +48,4 @@ "source": "src/index.ts", | ||
"homepage": "https://github.com/hiro0218/mokuji.js", | ||
"prettier": { | ||
"printWidth": 120, | ||
"singleQuote": false, | ||
"trailingComma": "all", | ||
"bracketSpacing": true | ||
}, | ||
"devDependencies": { | ||
"@babel/plugin-proposal-optional-chaining": "^7.13.12", | ||
"@hiro0218/prettier-config": "^1.0.2", | ||
"@types/node": "~14.14.41", | ||
@@ -62,4 +55,4 @@ "cross-env": "^5.2.0", | ||
"lint-staged": "^10.2.11", | ||
"microbundle": "^0.13.0", | ||
"prettier": "^2.0.5", | ||
"microbundle": "^0.13.3", | ||
"prettier": "^2.3.2", | ||
"rimraf": "^3.0.2", | ||
@@ -66,0 +59,0 @@ "typescript": "~4.2.4" |
156
src/index.ts
@@ -1,15 +0,51 @@ | ||
import { hasParentNode, getHeadingsElement } from "./dom"; | ||
import { replaceSpace2Underscore, convert2WikipediaStyleAnchor, getHeadingTagName2Number } from "./utils"; | ||
import { hasParentNode, getHeadingsElement } from './dom'; | ||
import { replaceSpace2Underscore, convert2WikipediaStyleAnchor, getHeadingTagName2Number } from './utils'; | ||
type MokujiOption = { | ||
anchorType: Boolean; | ||
anchorLink: Boolean; | ||
export type MokujiOption = { | ||
anchorType: boolean; | ||
anchorLink: boolean; | ||
anchorLinkSymbol: string; | ||
anchorLinkBefore: Boolean; | ||
anchorLinkBefore: boolean; | ||
anchorLinkClassName: string; | ||
anchorContainerTagName: string; | ||
anchorContainerTagName: 'ul' | 'ol'; | ||
}; | ||
export default class Mokuji { | ||
headings: NodeListOf<HTMLHeadingElement>; | ||
export const renderAnchorLink = ( | ||
headings: NodeListOf<HTMLHeadingElement>, | ||
anchors: NodeListOf<HTMLAnchorElement> | undefined, | ||
options: MokujiOption, | ||
) => { | ||
if (!anchors) return; | ||
const a = document.createElement('a'); | ||
a.classList.add(options.anchorLinkClassName); | ||
headings.forEach((heading) => { | ||
const { id } = heading; | ||
for (let i = 0; i < anchors.length; i++) { | ||
const { hash } = anchors[i]; | ||
if (hash.replace('#', '') !== id) { | ||
continue; | ||
} | ||
// create anchor | ||
const anchor = a.cloneNode(false) as HTMLAnchorElement; | ||
anchor.setAttribute('href', hash); | ||
anchor.textContent = options.anchorLinkSymbol; | ||
// insert anchor into headings | ||
if (options.anchorLinkBefore) { | ||
// before | ||
heading.insertBefore(anchor, heading.firstChild); | ||
} else { | ||
// after | ||
heading.appendChild(anchor); | ||
} | ||
} | ||
}); | ||
}; | ||
export class Mokuji { | ||
options: MokujiOption; | ||
@@ -25,40 +61,33 @@ storeIds: string[] = []; | ||
anchorLink: false, | ||
anchorLinkSymbol: "#", | ||
anchorLinkSymbol: '#', | ||
anchorLinkBefore: true, | ||
anchorLinkClassName: "", | ||
anchorContainerTagName: "ol", | ||
}, | ||
anchorLinkClassName: '', | ||
anchorContainerTagName: 'ol', | ||
} as MokujiOption, | ||
externalOptions, | ||
); | ||
this.headings = getHeadingsElement(element); | ||
const headings = getHeadingsElement(element); | ||
// mokuji start | ||
const mokuji = this.render(); | ||
// @ts-ignore | ||
return mokuji; | ||
} | ||
render() { | ||
// generate mokuji list | ||
const list = this.generateMokuji(); | ||
const mokuji = this.generateMokuji(headings); | ||
// setup anchor link | ||
if (this.options.anchorLink) { | ||
const anchors = list?.querySelectorAll("a"); | ||
this.renderAnchorLink(anchors); | ||
const anchors = mokuji.querySelectorAll('a'); | ||
renderAnchorLink(headings, anchors, this.options); | ||
} | ||
return list; | ||
// @ts-ignore | ||
return mokuji; | ||
} | ||
generateMokuji() { | ||
generateMokuji(headings: NodeListOf<HTMLHeadingElement>) { | ||
let elementContainer = document.createElement(this.options.anchorContainerTagName); | ||
this.generateHierarchyList(elementContainer); | ||
this.generateHierarchyList(headings, elementContainer); | ||
// remove duplicates by adding suffix | ||
const anchors = elementContainer.getElementsByTagName("a"); | ||
this.removeDuplicateIds(anchors); | ||
this.removeDuplicateIds(headings, elementContainer); | ||
@@ -68,9 +97,9 @@ return elementContainer; | ||
generateHierarchyList(elementContainer: HTMLElement) { | ||
generateHierarchyList(headings: NodeListOf<HTMLHeadingElement>, elementContainer: HTMLElement) { | ||
let number = 0; | ||
const elementListClone = document.createElement("li"); | ||
const elementAnchorClone = document.createElement("a"); | ||
const elementListClone = document.createElement('li'); | ||
const elementAnchorClone = document.createElement('a'); | ||
for (let i = 0; i < this.headings.length; i++) { | ||
const heading = this.headings[i]; | ||
for (let i = 0; i < headings.length; i++) { | ||
const heading = headings[i]; | ||
const currentNumber = getHeadingTagName2Number(heading.tagName); | ||
@@ -81,3 +110,3 @@ | ||
// number of the heading is large (small as heading) | ||
const nextElementOListClone = document.createElement("ol"); | ||
const nextElementOListClone = document.createElement('ol'); | ||
// @ts-ignore | ||
@@ -96,10 +125,11 @@ elementContainer.lastChild.appendChild(nextElementOListClone); | ||
const textContent = this.censorshipId(heading.textContent); | ||
const textContent = this.censorshipId(headings, heading.textContent); | ||
// headingへidを付与 | ||
heading.id = this.setAnchor(textContent, this.options.anchorType); | ||
const anchorText = this.generateAnchorText(textContent, this.options.anchorType); | ||
heading.id = anchorText; | ||
// add to wrapper | ||
const elementAnchor = elementAnchorClone.cloneNode(false) as HTMLAnchorElement; | ||
elementAnchor.href = "#" + heading.id; | ||
elementAnchor.href = `#${anchorText}`; | ||
elementAnchor.textContent = heading.textContent; | ||
@@ -115,8 +145,8 @@ const elementList = elementListClone.cloneNode(false); | ||
censorshipId(textContent: string | null) { | ||
let id = textContent || ""; | ||
censorshipId(headings: NodeListOf<HTMLHeadingElement>, textContent: string | null) { | ||
let id = textContent || ''; | ||
let suffix_count = 1; | ||
// IDが重複していた場合はsuffixを付ける | ||
while (suffix_count <= this.headings.length) { | ||
while (suffix_count <= headings.length) { | ||
const tmp_id = suffix_count === 1 ? id : `${id}_${suffix_count}`; | ||
@@ -136,3 +166,3 @@ | ||
setAnchor(text: string, type: Boolean) { | ||
generateAnchorText(text: string, type: boolean) { | ||
// convert spaces to _ | ||
@@ -142,3 +172,3 @@ let anchor = replaceSpace2Underscore(text); | ||
// remove & | ||
anchor = anchor.replace(/\&+/g, "").replace(/\&+/g, ""); | ||
anchor = anchor.replace(/\&+/g, '').replace(/\&+/g, ''); | ||
@@ -152,39 +182,11 @@ if (type === true) { | ||
renderAnchorLink(anchors: NodeListOf<HTMLAnchorElement> | undefined) { | ||
if (!anchors) return; | ||
removeDuplicateIds(headings: NodeListOf<HTMLHeadingElement>, elementContainer: HTMLElement) { | ||
const anchors = elementContainer.getElementsByTagName('a'); | ||
const a = document.createElement("a"); | ||
a.classList.add(this.options.anchorLinkClassName); | ||
for (let i = 0; i < anchors.length; i++) { | ||
const hash = anchors[i].hash; | ||
const headings = document.querySelector(`[id="${hash.replace("#", "")}"]`); | ||
if (!headings) { | ||
continue; | ||
} | ||
// create anchor | ||
const anchor = a.cloneNode(false) as HTMLAnchorElement; | ||
anchor.setAttribute("href", hash); | ||
anchor.textContent = this.options.anchorLinkSymbol; | ||
// insert anchor into headings | ||
if (this.options.anchorLinkBefore) { | ||
// before | ||
headings.insertBefore(anchor, headings.firstChild); | ||
} else { | ||
// after | ||
headings.appendChild(anchor); | ||
} | ||
} | ||
} | ||
removeDuplicateIds(anchors: HTMLCollectionOf<HTMLAnchorElement>) { | ||
for (let i = 0; i < anchors.length; i++) { | ||
const id = anchors[i].innerText; | ||
const hash = anchors[i].hash; | ||
const headings = document.querySelectorAll(`[id="${id}"]`); | ||
const matchedHeadings = Array.from(headings).filter((heading) => heading.id === id); | ||
if (headings.length === 1) { | ||
if (matchedHeadings.length === 1) { | ||
continue; | ||
@@ -196,3 +198,3 @@ } | ||
for (const heading of Array.from(headings)) { | ||
matchedHeadings.forEach((heading) => { | ||
const heading_id = `${heading.id}-${count}`; | ||
@@ -212,5 +214,5 @@ | ||
count++; | ||
} | ||
}); | ||
} | ||
} | ||
} |
export const replaceSpace2Underscore = (text: string) => { | ||
return text.replace(/\s+/g, "_"); | ||
return text.replace(/\s+/g, '_'); | ||
}; | ||
@@ -7,3 +7,3 @@ | ||
anchor = encodeURIComponent(anchor); | ||
anchor = anchor.replace(/\%+/g, "."); | ||
anchor = anchor.replace(/\%+/g, '.'); | ||
@@ -14,6 +14,3 @@ return anchor; | ||
export const getHeadingTagName2Number = (tagName: string) => { | ||
// @ts-ignore | ||
const currentNumber = tagName.match(/\d/g).join(""); | ||
return Number(currentNumber); | ||
return Number(tagName.substring(1)); | ||
}; |
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
65880
9
264
1