@xiee/utils
Advanced tools
Comparing version 1.13.34 to 1.13.35
// turn <div class="callout-*"> to <fieldset><legend>* | ||
document.querySelectorAll('div[class^="callout-"]').forEach(el => { | ||
const f = document.createElement('fieldset'); | ||
f.className = el.className; | ||
f.className = el.className; f.classList.add('callout'); | ||
// if the data-legend attribute exists, use its value as legend, otherwise use the class name | ||
const l = el.dataset.legend || el.classList[0].replace('callout-', ''); | ||
f.insertAdjacentHTML('afterbegin', `<legend>${l.toUpperCase()}</legend>`); | ||
f.insertAdjacentHTML('afterbegin', '<legend></legend>'); | ||
f.firstChild.dataset.legend = el.dataset.legend || el.classList[0].replace('callout-', ''); | ||
el.after(f); | ||
@@ -9,0 +9,0 @@ f.append(...el.children); |
@@ -1,1 +0,1 @@ | ||
document.querySelectorAll('div[class^="callout-"]').forEach((e=>{const a=document.createElement("fieldset");a.className=e.className;const l=e.dataset.legend||e.classList[0].replace("callout-","");a.insertAdjacentHTML("afterbegin",`<legend>${l.toUpperCase()}</legend>`),e.after(a),a.append(...e.children),e.remove()})); | ||
document.querySelectorAll('div[class^="callout-"]').forEach((e=>{const l=document.createElement("fieldset");l.className=e.className,l.classList.add("callout"),l.insertAdjacentHTML("afterbegin","<legend></legend>"),l.firstChild.dataset.legend=e.dataset.legend||e.classList[0].replace("callout-",""),e.after(l),l.append(...e.children),e.remove()})); |
@@ -50,3 +50,5 @@ // move elements into page boxes such that each box contains as many elements as | ||
function fragment(el, container, parent, page) { | ||
if (box.scrollHeight <= H) return; | ||
const tag = el.tagName, is_code = tag === 'CODE'; | ||
// if <code>, keep fragmenting; otherwise exit when box fits | ||
if (!(is_code && container) && box.scrollHeight <= H) return; | ||
const box_cur = page || box, el2 = el.cloneNode(); // shallow clone (wrapper only) | ||
@@ -57,4 +59,7 @@ // add the clone to current box, and move original el to next box | ||
); | ||
// for DIVs containing a single child (e.g., #TOC > ul), try to break the child | ||
if (el.tagName === 'DIV' && nChild(el) === 1) { | ||
// fragment <pre>'s <code> and <div>'s single child (e.g., #TOC > ul) | ||
if (tag === 'PRE') { | ||
const code = el.firstElementChild; | ||
code?.tagName == 'CODE' && /\n/.test(code.innerHTML) && fragment(code, el2, el, box_cur); | ||
} else if (tag === 'DIV' && nChild(el) === 1) { | ||
fragment(el.firstElementChild, el2, el, box_cur); | ||
@@ -64,3 +69,3 @@ } | ||
// keep moving el's first item to el2 until page height > H | ||
if (['UL', 'BLOCKQUOTE'].indexOf(el.tagName) > -1 && nChild(el) > 1) while (true) { | ||
if (['UL', 'BLOCKQUOTE'].indexOf(tag) > -1 && nChild(el) > 1) while (true) { | ||
const item = el.firstChild; | ||
@@ -75,4 +80,19 @@ if (!item) break; | ||
} | ||
// split lines in <code> and try to move them into el2 line by line | ||
if (is_code) { | ||
const code = el.innerHTML.split('\n'), code2 = []; | ||
for (let i of code) { | ||
code2.push(i); el2.innerHTML = code2.join('\n'); | ||
if (box_cur.scrollHeight > H) { | ||
code2.pop(); el2.innerHTML = code2.join('\n'); | ||
break; | ||
} | ||
} | ||
if (code2.length > 0) { | ||
el.innerHTML = code.slice(code2.length).join('\n'); | ||
if (removeBlank(parent)) return; // exit if <pre> is empty | ||
} | ||
} | ||
// if the clone is empty, remove it, otherwise keep fragmenting the remaining el | ||
if (!removeBlank(el2) || prev) fragment(container ? parent : el); | ||
if (!removeBlank(el2) || is_code || prev) fragment(container ? parent : el); | ||
} | ||
@@ -79,0 +99,0 @@ |
@@ -1,1 +0,1 @@ | ||
(e=>{function t(t,n=e){return n?.querySelector(t)}function n(t,n=e){return n?n.querySelectorAll(t):[]}function r(e){return e.childElementCount}const a=e.createElement("div"),s=n("h1").length>1;a.className="pagesjs-page",a.innerHTML='<div class="pagesjs-header"></div>\n<div class="pagesjs-body"></div>\n<div class="pagesjs-footer"></div>';let i,o,c,d=[];function l(e){return e&&!t(".pagesjs-body",e)&&e.insertAdjacentHTML("afterbegin",a.innerHTML),i=e||a.cloneNode(!0),o=i.children[1],d.length&&i.classList.add(...d),i}function f(e){if(!e)return!1;const t=""===e.innerText.trim();return t&&e.remove(),t}function p(e){if(e.classList.contains("pagesjs-page"))return i.after(l(e)),void(r(e)>3&&(o.append(...[...e.children].slice(3)),i.after(l())));if(i.scrollHeight>c){const[e,t]=[i,o];e.after(l()),r(t)>1&&o.append(t.lastChild)}o.append(e),g(e)}function g(e,t,n,a){if(i.scrollHeight<=c)return;const s=a||i,d=e.cloneNode();t?t.append(d):(o.append(d),s.after(l()),o.append(e)),"DIV"===e.tagName&&1===r(e)&&g(e.firstElementChild,d,e,s);const p=d.previousElementSibling;if(["UL","BLOCKQUOTE"].indexOf(e.tagName)>-1&&r(e)>1)for(;;){const t=e.firstChild;if(!t)break;if(d.append(t),s.scrollHeight>c){(p||r(d)>1)&&e.insertBefore(t,e.firstChild);break}}f(d)&&!p||g(t?n:e)}function h(e){return e&&(e.dataset.shortTitle||e.innerText)}const u=h(t("h1.title, .frontmatter h1, .title, h1")),m=(s?"h1":"h2")+":not(.frontmatter *)",b=["top","bottom"].map((t=>+getComputedStyle(e.documentElement).getPropertyValue(`--paper-margin-${t}`).replace("px","")||0));function E(){if("complete"!==e.readyState)return setTimeout(E,10);const r=e.body.classList;if(r.contains("pagesjs"))return;r.add("pagesjs"),s&&r.add("page-book"),e.body.insertAdjacentElement("afterbegin",l()),c=i.clientHeight||window.innerHeight,n(":is(#TOC, .footnotes, .chapter-before, .chapter-after):is(.side-left, .side-right).side").forEach((e=>{e.classList.remove("side","side-left","side-right")})),r.add("pagesjs-filling"),[t(".frontmatter"),t("#TOC"),t(".abstract")].forEach((e=>{e&&(p(e),s&&i.after(l()))})),n(".body").forEach((e=>{d=["chapter","appendix"].filter((t=>e.classList.contains(t))),s&&(""===i.innerText?l(i):i.after(l())),[...e.children].map(p),f(e.parentNode),f(e)})),r.remove("pagesjs-filling"),n('#TOC a[href^="#"]').forEach((t=>{const n=e.createElement("span"),r=t.firstElementChild;for(r?.classList.contains("section-number")?r.after(n):t.insertAdjacentElement("afterbegin",n);n.nextSibling;)n.append(n.nextSibling);t.insertAdjacentHTML("beforeend",'<span class="dot-leader"></span>'),t.dataset.pageNumber="000"}));let a,o=0;n(".pagesjs-page").forEach((e=>{if(f(e))return;if(s){if(t(".frontmatter",e))return;t(m,e)&&(a="")}const r=function(e){let t=+e.dataset.pagesOffset;if(t)return t;const r=e.scrollHeight;if(t=Math.ceil(r/c),t<=1)return t;const a=b.concat([...n("thead",e)].map((e=>+e.offsetHeight))).reduce(((e,t)=>e+t));if(!a)return t;function s(){return Math.ceil((r+(t-1)*a)/c)}let i=s();for(;i>t;)t=i,i=s();return t}(e);r>1&&e.classList.add("page-multiple"),o+=r,e.classList.add("page-"+(o%2==0?"even":"odd"));const i={pageNumber:o,mainTitle:u,pageTitle:a};let d;[e.children[0],e.children[2]].forEach((e=>{for(const t in i)i[t]&&(e.dataset[t]=i[t])})),a=h([...n(m,e)].pop())||a,n(".footnotes",e).forEach(((t,n)=>{0===n?(d=t,e.children[1].after(t)):(d.append(...t.children),t.remove())}))})),n('#TOC a[href^="#"]').forEach((e=>{const n=t(`.pagesjs-page:has(${e.getAttribute("href")}) .pagesjs-header`);e.dataset.pageNumber=n?n.dataset.pageNumber:""}))}addEventListener("beforeprint",E);let j=sessionStorage.getItem("pagesjs");j&&E(),addEventListener("keypress",(e=>"p"===e.key&&(E(),j=j?"":"1",sessionStorage.setItem("pagesjs",j),j||location.reload())))})(document); | ||
(e=>{function t(t,n=e){return n?.querySelector(t)}function n(t,n=e){return n?n.querySelectorAll(t):[]}function r(e){return e.childElementCount}const i=e.createElement("div"),a=n("h1").length>1;i.className="pagesjs-page",i.innerHTML='<div class="pagesjs-header"></div>\n<div class="pagesjs-body"></div>\n<div class="pagesjs-footer"></div>';let s,o,l,c=[];function d(e){return e&&!t(".pagesjs-body",e)&&e.insertAdjacentHTML("afterbegin",i.innerHTML),s=e||i.cloneNode(!0),o=s.children[1],c.length&&s.classList.add(...c),s}function f(e){if(!e)return!1;const t=""===e.innerText.trim();return t&&e.remove(),t}function p(e){if(e.classList.contains("pagesjs-page"))return s.after(d(e)),void(r(e)>3&&(o.append(...[...e.children].slice(3)),s.after(d())));if(s.scrollHeight>l){const[e,t]=[s,o];e.after(d()),r(t)>1&&o.append(t.lastChild)}o.append(e),g(e)}function g(e,t,n,i){const a=e.tagName,c="CODE"===a;if((!c||!t)&&s.scrollHeight<=l)return;const p=i||s,h=e.cloneNode();if(t?t.append(h):(o.append(h),p.after(d()),o.append(e)),"PRE"===a){const t=e.firstElementChild;"CODE"==t?.tagName&&/\n/.test(t.innerHTML)&&g(t,h,e,p)}else"DIV"===a&&1===r(e)&&g(e.firstElementChild,h,e,p);const u=h.previousElementSibling;if(["UL","BLOCKQUOTE"].indexOf(a)>-1&&r(e)>1)for(;;){const t=e.firstChild;if(!t)break;if(h.append(t),p.scrollHeight>l){(u||r(h)>1)&&e.insertBefore(t,e.firstChild);break}}if(c){const t=e.innerHTML.split("\n"),r=[];for(let e of t)if(r.push(e),h.innerHTML=r.join("\n"),p.scrollHeight>l){r.pop(),h.innerHTML=r.join("\n");break}if(r.length>0&&(e.innerHTML=t.slice(r.length).join("\n"),f(n)))return}(!f(h)||c||u)&&g(t?n:e)}function h(e){return e&&(e.dataset.shortTitle||e.innerText)}const u=h(t("h1.title, .frontmatter h1, .title, h1")),m=(a?"h1":"h2")+":not(.frontmatter *)",b=["top","bottom"].map((t=>+getComputedStyle(e.documentElement).getPropertyValue(`--paper-margin-${t}`).replace("px","")||0));function E(){if("complete"!==e.readyState)return setTimeout(E,10);const r=e.body.classList;if(r.contains("pagesjs"))return;r.add("pagesjs"),a&&r.add("page-book"),e.body.insertAdjacentElement("afterbegin",d()),l=s.clientHeight||window.innerHeight,n(":is(#TOC, .footnotes, .chapter-before, .chapter-after):is(.side-left, .side-right).side").forEach((e=>{e.classList.remove("side","side-left","side-right")})),r.add("pagesjs-filling"),[t(".frontmatter"),t("#TOC"),t(".abstract")].forEach((e=>{e&&(p(e),a&&s.after(d()))})),n(".body").forEach((e=>{c=["chapter","appendix"].filter((t=>e.classList.contains(t))),a&&(""===s.innerText?d(s):s.after(d())),[...e.children].map(p),f(e.parentNode),f(e)})),r.remove("pagesjs-filling"),n('#TOC a[href^="#"]').forEach((t=>{const n=e.createElement("span"),r=t.firstElementChild;for(r?.classList.contains("section-number")?r.after(n):t.insertAdjacentElement("afterbegin",n);n.nextSibling;)n.append(n.nextSibling);t.insertAdjacentHTML("beforeend",'<span class="dot-leader"></span>'),t.dataset.pageNumber="000"}));let i,o=0;n(".pagesjs-page").forEach((e=>{if(f(e))return;if(a){if(t(".frontmatter",e))return;t(m,e)&&(i="")}const r=function(e){let t=+e.dataset.pagesOffset;if(t)return t;const r=e.scrollHeight;if(t=Math.ceil(r/l),t<=1)return t;const i=b.concat([...n("thead",e)].map((e=>+e.offsetHeight))).reduce(((e,t)=>e+t));if(!i)return t;function a(){return Math.ceil((r+(t-1)*i)/l)}let s=a();for(;s>t;)t=s,s=a();return t}(e);r>1&&e.classList.add("page-multiple"),o+=r,e.classList.add("page-"+(o%2==0?"even":"odd"));const s={pageNumber:o,mainTitle:u,pageTitle:i};let c;[e.children[0],e.children[2]].forEach((e=>{for(const t in s)s[t]&&(e.dataset[t]=s[t])})),i=h([...n(m,e)].pop())||i,n(".footnotes",e).forEach(((t,n)=>{0===n?(c=t,e.children[1].after(t)):(c.append(...t.children),t.remove())}))})),n('#TOC a[href^="#"]').forEach((e=>{const n=t(`.pagesjs-page:has(${e.getAttribute("href")}) .pagesjs-header`);e.dataset.pageNumber=n?n.dataset.pageNumber:""}))}addEventListener("beforeprint",E);let j=sessionStorage.getItem("pagesjs");j&&E(),addEventListener("keypress",(e=>"p"===e.key&&(E(),j=j?"":"1",sessionStorage.setItem("pagesjs",j),j||location.reload())))})(document); |
{ | ||
"name": "@xiee/utils", | ||
"version": "1.13.34", | ||
"version": "1.13.35", | ||
"description": "Miscellaneous tools and utilities to manipulate HTML pages", | ||
@@ -5,0 +5,0 @@ "scripts": { |
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
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
213688
2438