@xiee/utils 1.6.8 → 1.7.0

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.
package/README.md CHANGED
@@ -126,6 +126,13 @@ class, so that highlight.js will not try to syntax highlight the code in it.
126
126
 
127
127
  Find all section headings (`h1` - `h6`) and number them.
128
128
 
129
+ ## ol-id.js
130
+
131
+ Add IDs of the form `li-N` to items in ordered lists, where `N` is the index of
132
+ a list item. This makes it possible to reference or locate a specific item on a
133
+ page by a hash in the URL. If you hold `Alt` and click on an item, you will get
134
+ the URL with the hash in the address bar of your browser.
135
+
129
136
  ## render-katex.js
130
137
 
131
138
  Simply run `renderMathInElement(document.body)` to render math expression using
package/css/faq.css CHANGED
@@ -18,7 +18,7 @@
18
18
  .faq-list .anchor {
19
19
  display: none;
20
20
  }
21
- .faq-list > li:hover > .anchor {
21
+ .faq-list > li:hover .anchor {
22
22
  display: inline;
23
23
  }
24
24
  .faq-list > li {
package/css/faq.min.css CHANGED
@@ -1 +1 @@
1
- .faq-button{cursor:pointer}.faq-list>li>:first-child{display:block;cursor:pointer;background:#fafafa;margin:-.5em;padding:.5em}.faq-list>:not(.faq-clicked)>*{display:none}.faq-button{float:right;margin-left:1em}.faq-list .anchor{display:none}.faq-list>li:hover>.anchor{display:inline}.faq-list>li{border:1px solid #eee;padding:.5em}
1
+ .faq-button{cursor:pointer}.faq-list>li>:first-child{display:block;cursor:pointer;background:#fafafa;margin:-.5em;padding:.5em}.faq-list>:not(.faq-clicked)>*{display:none}.faq-button{float:right;margin-left:1em}.faq-list .anchor{display:none}.faq-list>li:hover .anchor{display:inline}.faq-list>li{border:1px solid #eee;padding:.5em}
package/js/faq.js CHANGED
@@ -29,11 +29,11 @@
29
29
 
30
30
  // add anchor links after questions
31
31
  for (let i = 0; i < lis.length; i++) {
32
- let li = lis[i], hash = 'faq-' + (id ? id + '-' : '') + (i + 1);
32
+ let li = lis[i];
33
+ li.id = 'faq-' + (id ? id + '-' : '') + (i + 1);
33
34
  let p = li.firstElementChild;
34
- p.innerHTML += ' <span class="anchor" id="' + hash +
35
- '"><a href="#' + hash + '">#</a></span>';
36
- if (location.hash === '#' + hash) {
35
+ p.insertAdjacentHTML('beforeend', ` <span class="anchor"><a href="#${li.id}">#</a></span>`);
36
+ if (location.hash === '#' + li.id) {
37
37
  li.scrollIntoView();
38
38
  li.classList.add(cls_clicked);
39
39
  }
package/js/faq.min.js CHANGED
@@ -1 +1 @@
1
- (t=>{const n="faq-list",e="faq-clicked";function l(l,o){const c=l.children;if(0===c.length)return;for(let t of c)if(t.childElementCount<2)return;let i=l.classList;if(i.length>0&&!i.contains(n))return;i.add(n);const s=t.createElement("span");let a=!1;s.className="faq-button",s.innerText="⊕",s.onclick=function(){a=!a,this.innerText=a?"⊖":"⊕";for(const t of l.children)t.classList.toggle(e,a)},l.before(s);for(let t=0;t<c.length;t++){let n=c[t],l="faq-"+(o?o+"-":"")+(t+1),i=n.firstElementChild;i.innerHTML+=' <span class="anchor" id="'+l+'"><a href="#'+l+'">#</a></span>',location.hash==="#"+l&&(n.scrollIntoView(),n.classList.add(e)),i.onclick=function(t){n.classList.toggle(e)}}}const o=t.querySelectorAll(["div","main","section","article"].map((t=>t+":not(.footnotes) > ol")).join(","));for(let t=0;t<o.length;t++){l(o[t],o.length>1?t+1:0)}})(document);
1
+ (t=>{const e="faq-list",n="faq-clicked";function l(l,o){const i=l.children;if(0===i.length)return;for(let t of i)if(t.childElementCount<2)return;let c=l.classList;if(c.length>0&&!c.contains(e))return;c.add(e);const s=t.createElement("span");let a=!1;s.className="faq-button",s.innerText="⊕",s.onclick=function(){a=!a,this.innerText=a?"⊖":"⊕";for(const t of l.children)t.classList.toggle(n,a)},l.before(s);for(let t=0;t<i.length;t++){let e=i[t];e.id="faq-"+(o?o+"-":"")+(t+1);let l=e.firstElementChild;l.insertAdjacentHTML("beforeend",` <span class="anchor"><a href="#${e.id}">#</a></span>`),location.hash==="#"+e.id&&(e.scrollIntoView(),e.classList.add(n)),l.onclick=function(t){e.classList.toggle(n)}}}const o=t.querySelectorAll(["div","main","section","article"].map((t=>t+":not(.footnotes) > ol")).join(","));for(let t=0;t<o.length;t++){l(o[t],o.length>1?t+1:0)}})(document);
package/js/ol-id.js ADDED
@@ -0,0 +1,23 @@
1
+ // add ID to <ol> items so that they can be referenced via URL hashes like #li-N
2
+ ((d) => {
3
+ // add IDs to top-level <ol>s
4
+ const ols = d.querySelectorAll(':not(li) > ol');
5
+ ols.forEach((ol, i) => {
6
+ ol.querySelectorAll(':scope > li').forEach((li, j) => {
7
+ if (!li.id) li.id = 'li-' + (ols.length > 1 ? i + 1 + '-' : '') + (j + 1);
8
+ });
9
+ });
10
+ // add IDs to all <ol>s
11
+ d.querySelectorAll('ol').forEach((ol, i) => {
12
+ let l = ol.parentNode, id, p; // p: ID prefix
13
+ if (l?.tagName === 'LI') id = l.id;
14
+ p = (id ? id : 'li-' + (i + 1)) + '-';
15
+ ol.querySelectorAll(':scope > li').forEach((li, j) => {
16
+ if (!li.id) li.id = p + (j + 1);
17
+ // Alt + Click => adding hash to URL
18
+ li.addEventListener('click', e => {
19
+ e.altKey && (location.hash = li.id, e.stopPropagation());
20
+ });
21
+ });
22
+ });
23
+ })(document);
@@ -0,0 +1 @@
1
+ (e=>{const l=e.querySelectorAll(":not(li) > ol");l.forEach(((e,o)=>{e.querySelectorAll(":scope > li").forEach(((e,t)=>{e.id||(e.id="li-"+(l.length>1?o+1+"-":"")+(t+1))}))})),e.querySelectorAll("ol").forEach(((e,l)=>{let o,t,c=e.parentNode;"LI"===c?.tagName&&(o=c.id),t=(o||"li-"+(l+1))+"-",e.querySelectorAll(":scope > li").forEach(((e,l)=>{e.id||(e.id=t+(l+1)),e.addEventListener("click",(l=>{l.altKey&&(location.hash=e.id,l.stopPropagation())}))}))}))})(document);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xiee/utils",
3
- "version": "1.6.8",
3
+ "version": "1.7.0",
4
4
  "description": "Miscellaneous tools and utilities to manipulate HTML pages",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"