Skip to content

Commit 6a0479c

Browse files
committed
chore(rebuild): start parsing anchors
@montogeek started this work, I just rebased on the latest `rebuild` and cleaned things up slightly. I also renamed the misspelled enhancer utility.
1 parent c22bfbd commit 6a0479c

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"prismjs": "^1.9.0",
8585
"raw-loader": "^0.5.1",
8686
"redirect-webpack-plugin": "^0.1.1",
87+
"remark": "^9.0.0",
8788
"remark-autolink-headings": "^5.0.0",
8889
"remark-loader": "^0.3.0",
8990
"remark-mermaid": "^0.2.0",
@@ -116,7 +117,11 @@
116117
"react-hot-loader": "^4.0.0-beta.12",
117118
"react-router": "^4.2.0",
118119
"react-router-dom": "^4.2.2",
120+
"remark-parse": "^5.0.0",
121+
"remark-slug": "^5.0.0",
119122
"tool-list": "^0.11.0",
123+
"unified": "^6.1.6",
124+
"unist-util-visit": "^1.3.0",
120125
"webpack.vote": "^0.1.2",
121126
"whatwg-fetch": "^2.0.3"
122127
}

src/utilities/remark-anchors.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var remark = 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+
remark()
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+
};

src/utilities/treePluginEnhacer.js renamed to src/utilities/tree-plugin-enhancer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs');
22
const FrontMatter = require('front-matter');
3+
const RemarkAnchors = require('./remark-anchors');
34

45
module.exports = function(item, options) {
56
item.url = item.path
@@ -19,6 +20,6 @@ module.exports = function(item, options) {
1920
let { attributes } = FrontMatter(content);
2021

2122
Object.assign(item, attributes);
22-
item.anchors = []; // TODO: Add actual anchors
23+
item.anchors = RemarkAnchors(content);
2324
}
2425
}

webpack.common.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const CleanPlugin = require('clean-webpack-plugin');
55
const CopyWebpackPlugin = require('copy-webpack-plugin');
66
const ExtractTextPlugin = require('extract-text-webpack-plugin');
77
const DirectoryTreePlugin = require('directory-tree-webpack-plugin');
8-
const treePluginEnhacer = require('./src/utilities/treePluginEnhacer.js');
8+
const TreeEnhancer = require('./src/utilities/tree-plugin-enhancer.js');
99

1010
module.exports = (env = {}) => ({
1111
devtool: 'source-map',
@@ -35,6 +35,7 @@ module.exports = (env = {}) => ({
3535
options: {
3636
plugins: [
3737
// TODO: Add necessary remark plugins
38+
require('remark-slug'),
3839
require('remark-autolink-headings'),
3940
require('remark-mermaid')
4041
]
@@ -109,7 +110,7 @@ module.exports = (env = {}) => ({
109110
dir: 'src/content',
110111
path: 'src/_content.json',
111112
extensions: /\.md/,
112-
enhance: treePluginEnhacer,
113+
enhance: TreeEnhancer,
113114
filter: item => item.name !== 'images',
114115
sort: (a, b) => {
115116
let group1 = (a.group || '').toLowerCase();

0 commit comments

Comments
 (0)