Skip to content

Commit

Permalink
Use markdown-toc (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
microbouji authored and JoelMarcey committed Apr 4, 2018
1 parent 632ccfb commit c437f7b
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 26 deletions.
27 changes: 10 additions & 17 deletions lib/core/getTOC.js
Expand Up @@ -6,6 +6,7 @@
*/

const Remarkable = require('remarkable');
const mdToc = require('markdown-toc');
const toSlug = require('./toSlug');

const tagToLevel = tag => Number(tag.slice(1));
Expand All @@ -14,7 +15,7 @@ const tagToLevel = tag => Number(tag.slice(1));
* Returns a table of content from the headings
*
* @return array
* Array of heading objects with `hashLink`, `text` and `children` fields
* Array of heading objects with `hashLink`, `content` and `children` fields
*
*/
module.exports = (content, headingTags = 'h2', subHeadingTags = 'h3') => {
Expand All @@ -24,30 +25,22 @@ module.exports = (content, headingTags = 'h2', subHeadingTags = 'h3') => {
: [];

const md = new Remarkable();
const tokens = md.parse(content, {});
const headings = [];
for (let i = 0; i < tokens.length; i++) {
if (
tokens[i].type == 'heading_open' &&
headingLevels.concat(subHeadingLevels).includes(tokens[i].hLevel)
) {
headings.push({
hLevel: tokens[i].hLevel,
text: tokens[i + 1].content,
});
}
}
const headings = mdToc(content, {
filter: function(str, ele) {
return headingLevels.concat(subHeadingLevels).includes(ele.lvl);
},
}).json;

const toc = [];
let current;
headings.forEach(heading => {
const entry = {
hashLink: toSlug(heading.text),
text: heading.text,
hashLink: toSlug(heading.content),
content: md.renderInline(mdToc.titleize(heading.content)),
children: [],
};

if (headingLevels.includes(heading.hLevel)) {
if (headingLevels.includes(heading.lvl)) {
toc.push(entry);
current = entry;
} else {
Expand Down
11 changes: 9 additions & 2 deletions lib/core/nav/OnPageNav.js
Expand Up @@ -10,15 +10,22 @@ const React = require('react');
const siteConfig = require(process.cwd() + '/siteConfig.js');
const getTOC = require('../getTOC');

const Link = ({hashLink, text}) => <a href={`#${hashLink}`}>{text}</a>;
const Link = ({hashLink, content}) => (
<a
href={`#${hashLink}`}
dangerouslySetInnerHTML={{
__html: content,
}}
/>
);

const Headings = ({headings}) => {
if (!headings.length) return null;
return (
<ul className="toc-headings">
{headings.map((heading, i) => (
<li key={i}>
<Link hashLink={heading.hashLink} text={heading.text} />
<Link hashLink={heading.hashLink} content={heading.content} />
<Headings headings={heading.children} />
</li>
))}
Expand Down
6 changes: 3 additions & 3 deletions lib/static/css/main.css
Expand Up @@ -1549,16 +1549,16 @@ nav.toc .toggleNav .navBreadcrumb h2 {

.onPageNav ul li {
font-size: 12px;
line-height: 14px;
padding-bottom: 12px;
line-height: 17px;
padding-bottom: 9px;
}

.onPageNav ul ul {
padding: 8px 0 0 20px;
}

.onPageNav ul ul li {
padding-bottom: 8px;
padding-bottom: 5px;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -42,6 +42,7 @@
"fs-extra": "^5.0.0",
"glob": "^7.1.2",
"highlight.js": "^9.12.0",
"markdown-toc": "^1.2.0",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-dom-factories": "^1.0.1",
Expand Down

0 comments on commit c437f7b

Please sign in to comment.