jekyll-ham 0.3.3 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,10 +23,19 @@ function parseRawEntry(originalContent, onThisPageEntry, siteTitle, siteTagline)
23
23
  var headingTags = ['H2', 'H3', 'H4', 'H5', 'H6'];
24
24
  var i, j;
25
25
  var taglinePlaced = false;
26
- var children = originalContent.childNodes;
26
+ var originalChildren = originalContent.childNodes;
27
27
  var currentAccordionId = '';
28
- for (i = 0; i < children.length; i++){
29
- var node = children[i];
28
+
29
+ var onThisPageEntryDraft = document.createElement('ul');
30
+ onThisPageEntryDraft.className = 'list-group';
31
+
32
+ var firstH2 = true;
33
+ var accordionContainer = document.createElement('div');
34
+ accordionContainer.className = 'accordion';
35
+
36
+ // Scan the content to generate On This Page list and style blockquotes
37
+ for (i = 0; i < originalChildren.length; i++) {
38
+ var node = originalChildren[i];
30
39
  if (node.nodeName == 'H1' && !taglinePlaced && typeof siteTitle === 'string' && siteTitle.length > 0){
31
40
  var newTagline = document.createElement('span');
32
41
  newTagline.textContent = 'from ' + siteTitle;
@@ -47,23 +56,80 @@ function parseRawEntry(originalContent, onThisPageEntry, siteTitle, siteTagline)
47
56
  }
48
57
 
49
58
  // Create "On This Page entry"
59
+ var newLi = document.createElement('li');
60
+ newLi.className = 'list-group-item';
50
61
  var newA = document.createElement('a');
51
62
  newA.href = '#' + node.id;
52
- newA.className = 'sidebar-link text-truncate';
63
+ newA.className = 'nav-link text-truncate';
53
64
  newA.setAttribute('onclick', 'expandAccordion(\'' + currentAccordionId + '\')');
54
65
  for (j = 2; j < currentHeadingLevel; j++){
55
66
  var newSpan = document.createElement('span');
56
- newSpan.className = 'ml-10 d-inline-block';
67
+ if (j == currentHeadingLevel - 1) {
68
+ newSpan.className = 'me-1 d-inline-block text-body-tertiary';
69
+ newSpan.textContent = '↳';
70
+ } else {
71
+ newSpan.className = 'ms-3 d-inline-block';
72
+ }
57
73
  newSpan.setAttribute('aria-hidden', true);
58
74
  newA.appendChild(newSpan);
59
75
  }
60
76
  newA.appendChild(document.createTextNode(node.textContent));
61
- onThisPageEntry.appendChild(newA);
77
+ newLi.appendChild(newA)
78
+ onThisPageEntryDraft.appendChild(newLi);
79
+ }
80
+ if (node.nodeName == 'BLOCKQUOTE') {
81
+ var blockquoteChildren = node.childNodes;
82
+ node.className = 'alert';
83
+ var firstP = true;
84
+ for (j = 0; j < blockquoteChildren.length; j++) {
85
+ // Support GitHub-style Note, Important, and Warning blockquote style
86
+ if (blockquoteChildren[j].nodeName == 'P' && firstP) {
87
+ switch (blockquoteChildren[j].textContent) {
88
+ case '[!NOTE]':
89
+ node.className += ' alert-primary';
90
+ blockquoteChildren[j].className = ' fs-5 fw-bolder text-primary';
91
+ blockquoteChildren[j].innerHTML = '<i class="bi bi-info-circle"></i> Note';
92
+ break;
93
+ case '[!TIP]':
94
+ node.className += ' alert-success';
95
+ blockquoteChildren[j].className = ' fs-5 fw-bolder text-success';
96
+ blockquoteChildren[j].innerHTML = '<i class="bi bi-lightbulb"></i> Tip';
97
+ break;
98
+ case '[!IMPORTANT]':
99
+ node.className += ' alert-info';
100
+ blockquoteChildren[j].className = ' fs-5 fw-bolder text-info';
101
+ blockquoteChildren[j].innerHTML = '<i class="bi bi-exclamation-diamond"></i> Important';
102
+ break;
103
+ case '[!WARNING]':
104
+ node.className += ' alert-warning';
105
+ blockquoteChildren[j].className = ' fs-5 fw-bolder text-warning';
106
+ blockquoteChildren[j].innerHTML = '<i class="bi bi-exclamation-triangle"></i> Warning';
107
+ break;
108
+ case '[!CAUTION]':
109
+ node.className += ' alert-danger';
110
+ blockquoteChildren[j].className = ' fs-5 fw-bolder text-danger';
111
+ blockquoteChildren[j].innerHTML = '<i class="bi bi-exclamation-octagon"></i> Caution';
112
+ break;
113
+ default:
114
+ node.className += ' alert-secondary';
115
+ }
116
+ firstP = false;
117
+ }
118
+ }
62
119
  }
63
120
  }
121
+
122
+ // Rescan the content to create collapsible headings
123
+ var children = originalContent.cloneNode(true).childNodes;
64
124
  for (i = 0; i < children.length; i++){
65
125
  var node = children[i];
66
126
  if (node.nodeName === 'H2'){
127
+ if (firstH2) {
128
+ // Delete the original children to create a new accordion container
129
+ for (j = i; j < originalChildren.length; j++) originalChildren[j].remove();
130
+ firstH2 = false;
131
+ }
132
+
67
133
  var start = i; end = i;
68
134
  var collapseChildren = [];
69
135
  while (end + 1 < children.length && children[end + 1].nodeName !== 'H2') end++;
@@ -77,21 +143,38 @@ function parseRawEntry(originalContent, onThisPageEntry, siteTitle, siteTagline)
77
143
  }
78
144
 
79
145
  // Create actual collapse
80
- var newDetails = document.createElement('details');
81
- newDetails.className = 'collapse-panel w-full _ham_accordion_autoexpand';
82
- var newSummary = document.createElement('summary');
83
- newSummary.className = 'collapse-header font-size-20 h2';
84
- newSummary.innerHTML = node.innerHTML;
85
- var newDiv = document.createElement('div');
86
- newDiv.className = 'collapse-content';
87
- for (j = 0; j < collapseChildren.length; j++) newDiv.appendChild(collapseChildren[j]);
88
- newDetails.appendChild(newSummary);
89
- newDetails.appendChild(newDiv);
90
-
146
+ var newDetails = document.createElement('div')
147
+ newDetails.className = 'accordion-item';
91
148
  newDetails.id = children[i].id;
92
- children[i].outerHTML = newDetails.outerHTML;
149
+ var newSummary = document.createElement('h2');
150
+ newSummary.className = 'accordion-header';
151
+ var newSummaryButton = document.createElement('button');
152
+ newSummaryButton.className = 'accordion-button fs-4';
153
+ newSummaryButton.setAttribute('type', 'button');
154
+ newSummaryButton.setAttribute('data-bs-toggle', 'collapse');
155
+ newSummaryButton.setAttribute('data-bs-target', '#' + children[i].id + "-content");
156
+ newSummaryButton.setAttribute('aria-expanded', 'true');
157
+ newSummaryButton.setAttribute('aria-controls', children[i].id + "-content");
158
+ newSummaryButton.setAttribute('type', 'button');
159
+ newSummaryButton.setAttribute('type', 'button');
160
+ newSummaryButton.innerHTML = node.innerHTML;
161
+ newSummary.appendChild(newSummaryButton);
162
+
163
+ var newCollapse = document.createElement('div');
164
+ newCollapse.className = 'accordion-collapse collapse show';
165
+ newCollapse.id = children[i].id + "-content";
166
+ var newBody = document.createElement('div');
167
+ newBody.className = 'accordion-body';
168
+ for (j = 0; j < collapseChildren.length; j++) newBody.appendChild(collapseChildren[j]);
169
+ newCollapse.appendChild(newBody);
170
+ newDetails.appendChild(newSummary);
171
+ newDetails.appendChild(newCollapse);
172
+ accordionContainer.appendChild(newDetails);
93
173
  }
94
174
  }
175
+
176
+ originalContent.appendChild(accordionContainer);
177
+ onThisPageEntry.appendChild(onThisPageEntryDraft);
95
178
  }
96
179
 
97
180
  var _ham_state_is_all_accordion_collapsed = true;
@@ -110,9 +193,9 @@ function autoexpandAccordion(){
110
193
  }
111
194
  }
112
195
 
113
- function expandAccordion(id){
114
- var el = document.getElementById(id);
115
- el.setAttribute('open', 'true');
196
+ async function expandAccordion(id){
197
+ var el = new bootstrap.Collapse(document.getElementById(id + "-content"), {toggle: false});
198
+ await el.show();
116
199
  }
117
200
 
118
201
  window.addEventListener("load", autoexpandAccordion);