Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add collapsible toc #190

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions background/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ md.storage.defaults = (compilers) => {
emoji: false,
scroll: true,
toc: false,
tocc: false,
mathjax: false,
autoreload: false,
mermaid: false,
Expand Down
26 changes: 26 additions & 0 deletions content/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,29 @@ img.emojione {
/* prevent img stretch */
width: auto;
}

details>summary {

list-style: none;
}
summary::-webkit-details-marker {
display: none
}

summary {
position: relative;
}

summary::before {
font-size: 14px;
content: "▷";
position: absolute;
top: 50%;
-ms-transform: translateY(-50%);
transform: translateY(-50%);
left: 5%;
}

details[open]>summary:before {
content: "▽";
}
60 changes: 60 additions & 0 deletions content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ var oncreate = {

if (state.content.toc && !state.toc) {
state.toc = toc()
if (!state.content.tocc)
m.redraw()
}

if (state.content.tocc) {
state.toc = tocc()
m.redraw()
}

Expand Down Expand Up @@ -247,6 +253,60 @@ var toc = (
return html
}, '')

var tocc = (
link = (header) => '<a href="#' + header.id + '">' + header.title + '</a>') => {
let toc_array = Array.from($('#_html').childNodes)
.filter((node) => /h[1-6]/i.test(node.tagName))
.map((node) => ({
id: node.getAttribute('id'),
level: parseInt(node.tagName.replace('H', '')),
title: node.innerText.replace(/</g, '&lt;').replace(/>/g, '&gt;')
}))

let html = ''

let add_div_toc = (i) => {
html += '<div class="_ul">'
html += link(toc_array[i])
html += '</div>'
}

let create_toc_html_recursive = (i) => {
if (i === toc_array.length - 1) {
html += '<div class="_ul">'.repeat(toc_array[i].level)
html += link(toc_array[i])
html += '</div>'.repeat(toc_array[i].level)
return i
}
let curr_level = toc_array[i].level
for (; i < toc_array.length; i++) {
if (toc_array[i].level < curr_level) {
html += '</div></details>'
return i-1
}else if (i === toc_array.length - 1){
add_div_toc(i)
return i
} else if (toc_array[i+1].level < curr_level) {
add_div_toc(i)
html += '</div></details>'
return i
} else if (toc_array[i+1].level > curr_level) {
html += '<details><summary class="_ul">'
html += link(toc_array[i])
html += '</summary><div class="_ul">'
i = create_toc_html_recursive(++i)
} else if (toc_array[i+1].level === curr_level) {
add_div_toc(i)
}
}
return i
}

create_toc_html_recursive(0)
return html
}


if (document.readyState === 'complete') {
mount()
}
Expand Down
1 change: 1 addition & 0 deletions popup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var state = {
emoji: 'Convert emoji :shortnames: into EmojiOne images',
scroll: 'Remember scroll position',
toc: 'Generate Table of Contents',
tocc: 'Generate Table Of Contents collapsible',
mathjax: 'Render MathJax formulas',
mermaid: 'Mermaid diagrams',
}
Expand Down