Skip to content
This repository has been archived by the owner on Aug 9, 2020. It is now read-only.

Add TOC max-depth custom config support & styling #174

Open
wants to merge 2 commits 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 docs/api/config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = new Promise((resolve, reject) => {
| table\_of_contents | object | | |
| table\_of_contents.page | boolean | `true` | Whether to enable the table of contents for headers in the page. |
| table\_of_contents.folder | boolean | `true` | Whether to enable the table of contents for pages in a folder (shown in the [index page](/index-files).) |
| table\_of_contents.max_depth | number | `2` | Defines a maximum header level depth to render in the TOC. |
| syntax | object | | |
| syntax.theme | string | `atom-one-light` | The [syntax highlighting theme](/syntax-highlighting/#choosing-a-style) to use. |
| syntax.renderer | string | `hljs` | The [syntax highlighting renderer](/syntax-highlighting/#choosing-a-renderer) to use. Options are `hljs` or `prism`. |
Expand Down
3 changes: 2 additions & 1 deletion src/core/hydrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { mergeLeftByKey } = require('../utils/merge')
const { getContent } = require('./filesystem')
const { walkSource } = require('./source')
const Sitemap = require('./sitemap')
const DEFAULT_TOC_DEPTH = 2

async function getMetaData (item, parentItems) {
const data = item.type === 'file'
Expand Down Expand Up @@ -54,7 +55,7 @@ async function tableOfContents (toc, { input, items }) {
if (input) {
if (toc.page) {
toc.page = markdownToc(await getContent(input))
.json.filter(i => i.lvl <= 2)
.json.filter(i => i.lvl <= (toc.max_depth || DEFAULT_TOC_DEPTH))
}

if (toc.folder) {
Expand Down
4 changes: 1 addition & 3 deletions themes/default/markdown/overrides/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,5 @@ export default function (props) {

const element = React.createElement(levels[level], { style }, children)

return level <= 2
? <Link href={`#${itemId}`} id={itemId}>{element}</Link>
: element
return <Link href={`#${itemId}`} id={itemId}>{element}</Link>
}
4 changes: 2 additions & 2 deletions themes/default/toc/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ const Toc = (props) => {

// Create TOC hierarchy and link to headers
const items = props.items.map(t => (
<li key={`${props.items}-${t.slug}`}>
<li key={`${props.items}-${t.slug}`} className={`level-${t.lvl}`}>
<a href={`#${t.slug}`}>
{t.content}
</a>
</li>
))

return (
<PageItem sticky={props.sticky}>
<PageItem sticky={props.sticky} items={props.items}>
<div>
<h5>Table of Contents</h5>
<ul>{items}</ul>
Expand Down
6 changes: 6 additions & 0 deletions themes/default/toc/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export const Wrapper = styled('nav')`
margin: 60px 0 0 5px;
position: relative;
`
const generateListItemLevels = items => items.map(({ lvl }) => `
li.level-${lvl} {
padding-left: ${(lvl - 1) * 8}px;
}`).join(' ')

export const PageItem = styled('nav')`
position: relative;
Expand Down Expand Up @@ -44,6 +48,8 @@ export const PageItem = styled('nav')`
display: block;
}

${props => props.items && generateListItemLevels(props.items)}

a {
text-decoration: none;
color: #626469;
Expand Down