@xiee/utils 1.3.1 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,10 @@
1
1
  // add section numbers to headings
2
2
  (d => {
3
3
  // find the body of the article
4
- const b = d.querySelector('.article, .body, article, body');
5
- if (!b) return;
4
+ let b;
5
+ ['.article', '.body', 'article', 'body'].forEach(s => {
6
+ if (!b) b = d.querySelector(s);
7
+ });
6
8
  const hs = b.querySelectorAll('h1, h2, h3, h4, h5, h6');
7
9
  if (hs.length === 0) return;
8
10
  // normalize Pandoc's .header-section-number class to .section-number
@@ -1 +1 @@
1
- (e=>{const r=document.querySelector(".article, .body, article, body");if(!r)return;const n=r.querySelectorAll("h1, h2, h3, h4, h5, h6");if(0===n.length)return;if(r.querySelectorAll("span.header-section-number").forEach((e=>{e.classList.add("section-number")})),r.querySelector("span.section-number"))return void r.querySelectorAll("span.section-number").forEach((e=>{e.innerText=e.innerText.replace(/^(0\.)+/,"")}));let t,c=0,a=[0,0,0,0,0,0];n.forEach((e=>{if(t=parseInt(e.tagName.replace(/^h/i,"")),t<c)for(let e=t;e<a.length;e++)a[e]=0;e.insertAdjacentHTML("afterbegin",(a[t-1]++,`<span class="section-number">${a.join(".").replace(/^(0\.)+|(\.0)+$/g,"").replace(/^([0-9]+)$/,"$1.")}</span> `)),c=t}))})();
1
+ (e=>{let r;[".article",".body","article","body"].forEach((n=>{r||(r=e.querySelector(n))}));const n=r.querySelectorAll("h1, h2, h3, h4, h5, h6");if(0===n.length)return;if(r.querySelectorAll("span.header-section-number").forEach((e=>{e.classList.add("section-number")})),r.querySelector("span.section-number"))return void r.querySelectorAll("span.section-number").forEach((e=>{e.innerText=e.innerText.replace(/^(0\.)+/,"")}));let t,c=0,a=[0,0,0,0,0,0];n.forEach((e=>{if(t=parseInt(e.tagName.replace(/^h/i,"")),t<c)for(let e=t;e<a.length;e++)a[e]=0;e.insertAdjacentHTML("afterbegin",(a[t-1]++,`<span class="section-number">${a.join(".").replace(/^(0\.)+|(\.0)+$/g,"").replace(/^([0-9]+)$/,"$1.")}</span> `)),c=t}))})(document);
package/js/toc.js CHANGED
@@ -1,8 +1,10 @@
1
1
  // build TOC using headings
2
2
  (d => {
3
3
  // find the body of the article
4
- const b = d.querySelector('.article, .body, article, body');
5
- if (!b) return;
4
+ let b;
5
+ ['.article', '.body', 'article', 'body'].forEach(s => {
6
+ if (!b) b = d.querySelector(s);
7
+ });
6
8
  const hs = b.querySelectorAll('h1, h2, h3, h4, h5, h6');
7
9
  if (hs.length === 0) return;
8
10
 
@@ -45,5 +47,5 @@
45
47
  b.insertBefore(toc, b.firstChild);
46
48
 
47
49
  // check if headings are numbered
48
- toc.querySelector('span.section-number') && toc.classList.add('numbered');
50
+ toc.querySelector('span.section-number') && toc.firstElementChild.classList.add('numbered');
49
51
  })(document);
package/js/toc.min.js CHANGED
@@ -1 +1 @@
1
- (e=>{const t=e.querySelector(".article, .body, article, body");if(!t)return;const r=t.querySelectorAll("h1, h2, h3, h4, h5, h6");if(0===r.length)return;var n=e.getElementById("TOC");let i,l;n?.remove(),(n=e.createElement("div")).id="TOC";let a,d=n,c=0;r.forEach((t=>{if(a=parseInt(t.tagName.replace(/^h/i,"")),i=e.createElement("li"),a>c)l=e.createElement("ul"),l.appendChild(i),d.appendChild(l);else if(a<c)for(let e=0;e<c-a;e++)d=d.parentNode.parentNode;a<=c&&d.parentNode.appendChild(i),d=i;const r=e.createElement("a");if(r.innerHTML=t.innerHTML,t.id)r.href="#"+t.id;else{const e=t.parentNode;e.classList.contains("section")&&e.id&&(r.href="#"+e.id)}d.appendChild(r),c=a})),t.insertBefore(n,t.firstChild),n.querySelector("span.section-number")&&n.classList.add("numbered")})(document);
1
+ (e=>{let t;[".article",".body","article","body"].forEach((r=>{t||(t=e.querySelector(r))}));const r=t.querySelectorAll("h1, h2, h3, h4, h5, h6");if(0===r.length)return;var n=e.getElementById("TOC");let l,i;n?.remove(),(n=e.createElement("div")).id="TOC";let a,d=n,c=0;r.forEach((t=>{if(a=parseInt(t.tagName.replace(/^h/i,"")),l=e.createElement("li"),a>c)i=e.createElement("ul"),i.appendChild(l),d.appendChild(i);else if(a<c)for(let e=0;e<c-a;e++)d=d.parentNode.parentNode;a<=c&&d.parentNode.appendChild(l),d=l;const r=e.createElement("a");if(r.innerHTML=t.innerHTML,t.id)r.href="#"+t.id;else{const e=t.parentNode;e.classList.contains("section")&&e.id&&(r.href="#"+e.id)}d.appendChild(r),c=a})),t.insertBefore(n,t.firstChild),n.querySelector("span.section-number")&&n.firstElementChild.classList.add("numbered")})(document);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xiee/utils",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "Miscellaneous tools and utilities to manipulate HTML pages",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"