@xiee/utils 1.1.2 → 1.1.5

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
@@ -58,6 +58,13 @@ to render unique `id`s for the footnote items (the `id` is `fn:-`).
58
58
 
59
59
  Fix the table of contents generated by lower versions of Hugo.
60
60
 
61
+ ## hash-notes.js
62
+
63
+ Convert HTML comments of the form `<!--# comments -->` to
64
+ `<span class="hash-note">comments</span>`. If such comments are found, the
65
+ document body will gain classes `has-notes` and `hide-notes`. You can use CSS to
66
+ style the notes or hide/show them as you wish.
67
+
61
68
  ## header-link.js
62
69
 
63
70
  Add anchor links to all section headers (e.g., `<h2>`) that have nonempty `id`
@@ -0,0 +1,25 @@
1
+ /* Add circled numbers to h2 and TOC items generated by Hugo */
2
+ #TableOfContents, #TableOfContents ~ p:first-of-type { counter-reset: circled-numbers; }
3
+ h2::before, #TableOfContents > ul > li > ul > li::before {
4
+ counter-increment: circled-numbers;
5
+ content: counter(circled-numbers);
6
+ border-radius: 50%;
7
+ border: 1px dashed #999;
8
+ color: #333;
9
+ background: white;
10
+ display: inline-block;
11
+ min-width: 1.75em;
12
+ line-height: 1.75em;
13
+ margin-right: .5em;
14
+ text-align: center;
15
+ font-size: .8em;
16
+ }
17
+ h2::before {
18
+ vertical-align: bottom;
19
+ color: white;
20
+ border: none;
21
+ background: #999;
22
+ }
23
+ #TableOfContents > ul > li > ul > li {
24
+ display: inline-block;
25
+ }
package/js/fix-toc.js CHANGED
@@ -1,7 +1,24 @@
1
+ // fix some issues in TOC generated by Hugo (Blackfriday or Goldmark)
1
2
  (function() {
2
- var toc = document.getElementById('TableOfContents');
3
+ const toc = document.getElementById('TableOfContents');
3
4
  if (!toc) return;
4
- var li, ul = toc.querySelector('ul');
5
+ // fix header ids and a hrefs if a header contains a link
6
+ toc.querySelectorAll('a[href^="#"]').forEach((a) => {
7
+ let id = a.getAttribute('href').replace(/^#/, '');
8
+ const h = document.getElementById(id);
9
+ // remove the URL from the id
10
+ id = id.replace(/-https?-.+$/, '');
11
+ if (h) h.id = id; a.href = '#' + id;
12
+ // if the TOC item has two <a>'s, remove the second if the first is empty
13
+ if (a.innerHTML !== '') return;
14
+ const a2 = a.nextElementSibling;
15
+ if (!a2 || a2.tagName !== 'A') return;
16
+ a.innerHTML = a2.innerHTML;
17
+ a2.remove();
18
+ });
19
+ // Blackfriday may generate a TOC that has an empty bullet when all headings
20
+ // are h2 and there is no h1: https://github.com/gohugoio/hugo/issues/1778#issuecomment-420036687
21
+ let li, ul = toc.querySelector('ul');
5
22
  if (ul.childElementCount !== 1) return;
6
23
  li = ul.firstElementChild;
7
24
  if (li.tagName !== 'LI') return;
@@ -0,0 +1,20 @@
1
+ // convert <!--# comments --> to <span class="hash-notes">comments</span>
2
+ (function(d) {
3
+ function toSpan(el) {
4
+ const t = el.textContent, r = /^#[\s\n]+([\s\S]+)[\s\n]+$/;
5
+ if (!r.test(t)) return;
6
+ d.body.classList.add('has-notes', 'hide-notes');
7
+ // use <p> if the comment's parent is not <p>; otherwise use inline <span>
8
+ const s = d.createElement(el.parentNode.nodeName === 'P' ? 'span' : 'p');
9
+ s.className = 'hash-note';
10
+ s.innerText = t.replace(r, '$1');
11
+ el.before(s);
12
+ el.remove();
13
+ };
14
+ function findComments(el) {
15
+ el.childNodes.forEach(node => {
16
+ node.nodeType === Node.COMMENT_NODE ? toSpan(node) : findComments(node);
17
+ });
18
+ };
19
+ findComments(d.body);
20
+ })(document);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xiee/utils",
3
- "version": "1.1.2",
3
+ "version": "1.1.5",
4
4
  "description": "Miscellaneous tools and utilities to manipulate HTML pages",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"