Skip to content

Commit 0284cf2

Browse files
committed
feat(site) Anchors on sidebar
1 parent acbbe02 commit 0284cf2

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

md.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var markdown = require('remark');
2+
var slug = require('remark-slug');
3+
var visit = require('unist-util-visit');
4+
5+
module.exports = function getAnchors(content) {
6+
let anchors = [];
7+
8+
markdown()
9+
.use(slug)
10+
.use(pl)
11+
.process(content, (err, file) => {
12+
if (err) {
13+
throw err;
14+
}
15+
});
16+
17+
function pl() {
18+
return function transformer(ast) {
19+
visit(ast, 'heading', visitor);
20+
};
21+
22+
function visitor(node) {
23+
anchors.push({
24+
title: node.children[0].value,
25+
id: node.data.id
26+
});
27+
}
28+
}
29+
30+
return anchors;
31+
};

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@
114114
"react-hot-loader": "^4.0.0-beta.12",
115115
"react-router": "^4.2.0",
116116
"react-router-dom": "^4.2.2",
117+
"remark-parse": "^5.0.0",
118+
"remark-slug": "^5.0.0",
117119
"tool-list": "^0.11.0",
120+
"unified": "^6.1.6",
121+
"unist-util-visit": "^1.3.0",
118122
"webpack.vote": "^0.1.2",
119123
"whatwg-fetch": "^2.0.3"
120124
}

src/components/Page/Page.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Page extends React.Component {
7171
if ( this.props.content instanceof Promise ) {
7272
this.props.content
7373
.then(content => this.setState({
74-
content
74+
content: content.default
7575
}))
7676
.catch(error => this.setState({
7777
content: 'Error loading content.'

src/components/Sidebar/Sidebar.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export default class Sidebar extends Component {
1515
let { fixed, availableHeight, maxWidth } = this.state;
1616
let group;
1717

18+
console.log(pages);
19+
1820
return (
1921
<nav
2022
className={ `sidebar ${className}` }

webpack.common.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require('fs');
22
const path = require('path');
33
const webpack = require('webpack');
44
const FrontMatter = require('front-matter');
5+
const getAnchors = require('./md');
56
const CleanPlugin = require('clean-webpack-plugin');
67
const CopyWebpackPlugin = require('copy-webpack-plugin');
78
const ExtractTextPlugin = require('extract-text-webpack-plugin');
@@ -35,7 +36,9 @@ module.exports = (env = {}) => ({
3536
options: {
3637
plugins: [
3738
// TODO: Add necessary remark plugins
39+
require('remark-slug'),
3840
require('remark-autolink-headings'),
41+
require('remark-html'),
3942
require('remark-mermaid')
4043
]
4144
}
@@ -121,7 +124,7 @@ module.exports = (env = {}) => ({
121124
let content = fs.readFileSync(item.path, 'utf8')
122125
let { attributes } = FrontMatter(content)
123126
Object.assign(item, attributes)
124-
item.anchors = [] // TODO: Add actual anchors
127+
item.anchors = getAnchors(content)
125128

126129
} else {
127130
// TODO: Add directory (section) attributes and index url (if necessary)

0 commit comments

Comments
 (0)