-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Only shows the first section for each file because we only have values per file. - rewrite dashboard client side with the same logic as toc - css and js cleanup - katex only loads when content is in the viewport closes #1062 #1063
- Loading branch information
1 parent
666e4b2
commit a08442a
Showing
12 changed files
with
203 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// [ | ||
// { text: "Foo", id: "foo", tagName: 'h1', children: [ | ||
// { text: "Xyz", id: "xyz", tagName: 'h2', children: [ | ||
// { text: "Bar", id: "bar", tagName: 'h3, children:[] } | ||
// { text: "Bar", id: "bar", tagName: 'h3, children:[] } | ||
// ]} | ||
// ]}, | ||
// { text: "Baz", id: "baz", tag: 'h1', children: [] } | ||
// ] | ||
|
||
function buildTocModel (contentSelector) { | ||
const model = [] | ||
const headingList = document.querySelector(contentSelector).querySelectorAll('h1,h2,h3,h4,h5,h6') | ||
let parents = [{tagName: 'H0', children: model}] | ||
let prevSibling = null | ||
for (let el of headingList) { | ||
let node = { | ||
id: el.id, | ||
tagName: el.tagName, | ||
text: cleanHeadingText(el), | ||
page: Boolean(el.dataset.page), | ||
dashboardWeight: el.dataset.dashboardWeight, | ||
dashboardAudit: el.dataset.dashboardAudit, | ||
dashboardState: el.dataset.dashboardState, | ||
children: [] | ||
} | ||
if (!prevSibling || headingNum(node) === headingNum(prevSibling)) { | ||
parents[parents.length - 1].children.push(node) | ||
prevSibling = node | ||
|
||
// is h3 > h2 ? | ||
} else if (headingNum(node) > headingNum(prevSibling)) { | ||
parents.push(prevSibling) | ||
prevSibling.children.push(node) | ||
prevSibling = node | ||
} else { | ||
// h2 or h1 after an h3... gotta find out how far to unwind, parents may not be contiguous in a bad doc, so we walk. | ||
let prevParent = parents.pop() | ||
while (headingNum(node) <= headingNum(prevParent)) { | ||
prevParent = parents.pop() | ||
} | ||
prevParent.children.push(node) | ||
parents.push(prevParent) | ||
prevSibling = node | ||
} | ||
} | ||
return model | ||
} | ||
|
||
function cleanHeadingText (el) { | ||
return el.textContent.trim() | ||
} | ||
|
||
function headingNum (el) { | ||
return Number(el.tagName[1]) | ||
} | ||
|
||
export { | ||
buildTocModel | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {html, render} from 'lit-html'; | ||
|
||
const tableData = (model, depth, output=[]) => { | ||
const index = depth.length-1 | ||
for (const node of model) { | ||
depth[index] += 1 | ||
output.push({number: depth.join("."), ...node}) | ||
if (node.children) { | ||
tableData(node.children, [...depth, 0], output) | ||
} | ||
} | ||
return output | ||
} | ||
|
||
function buildDashboard(selector, model) { | ||
const data = tableData(model, [0]) | ||
const tpl = html` | ||
<table id="dashboard" class="sort Dashboard tablesort"> | ||
<thead> | ||
<tr> | ||
<th>Section</th> | ||
<th>Weight</th> | ||
<th>State</th> | ||
<th>Theory Audit</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
${data.map((i)=> i.page ? html` | ||
<tr> | ||
<td class="Dashboard-section">${i.number} <a href="#${i.id}">${i.text}</a></td> | ||
<td>${i.dashboardWeight}</td> | ||
<td class="text-black bg-na bg-${i.dashboardState}">${i.dashboardState}</td> | ||
<td class="text-transparent ${i.dashboardAudit > 0 ? 'bg-stable' : 'bg-incorrect'}">${i.dashboardAudit}</td> | ||
</tr> | ||
`: '')} | ||
</tbody> | ||
</table> | ||
` | ||
render(tpl, document.querySelector(selector)) | ||
} | ||
|
||
export { | ||
buildDashboard | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import renderMathInElement from 'katex/dist/contrib/auto-render.mjs' | ||
|
||
function renderKatex (target) { | ||
console.log('init katex for ', target) | ||
renderMathInElement(target, { | ||
ignoredTags: ["script", "noscript", "style", "textarea"], | ||
throwOnError: false, | ||
delimiters: [ | ||
{left: "$$", right: "$$", display: true}, | ||
{left: "$", right: "$", display: false}, | ||
{left: "\\(", right: "\\)", display: false}, | ||
{left: "\\[", right: "\\]", display: true} | ||
] | ||
}) | ||
} | ||
|
||
export { | ||
renderKatex | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
function initToc ({tocSelector, model}) { | ||
const toc = buildTocDom(model) | ||
document.querySelector(tocSelector).appendChild(toc) | ||
} | ||
|
||
function buildTocDom (model) { | ||
const parent = document.createDocumentFragment() | ||
buildList(parent, model, 0) | ||
return parent | ||
} | ||
|
||
function buildList (parent, children, depth) { | ||
let ol = createList(depth) | ||
parent.append(ol) | ||
for (node of children) { | ||
let li = document.createElement('li') | ||
let a = document.createElement('a') | ||
a.setAttribute('href', '#' + node.id) | ||
a.innerText = node.text | ||
li.appendChild(a) | ||
ol.append(li) | ||
if (node.children) { | ||
buildList(li, node.children, depth + 1) | ||
} | ||
} | ||
} | ||
|
||
function createList(depth) { | ||
const ol = document.createElement('ol') | ||
ol.className = 'depth-' + depth | ||
return ol | ||
} | ||
|
||
export { | ||
initToc | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
{{ $js := resources.Get "main.js" | js.Build "main.js"}} | ||
<script defer src="{{ $js.RelPermalink }}" integrity="{{ $js.Data.Integrity }}"></script> | ||
|
||
{{ $js := resources.Get "katex.js" | js.Build "katex.js"}} | ||
{{ $opts := dict "targetPath" "main.js" "minify" "true" }} | ||
{{ $js := resources.Get "js/main.js" | js.Build $opts}} | ||
<script defer src="{{ $js.RelPermalink }}" integrity="{{ $js.Data.Integrity }}"></script> | ||
|
||
<pwa-update swpath="{{ "/sw.js" | relURL }}"></pwa-update> |
Oops, something went wrong.