Skip to content

Commit 611ce85

Browse files
committed
refactor(rebuild): remove unused deps and create proper remark plugin
1 parent ea0660d commit 611ce85

File tree

4 files changed

+48
-39
lines changed

4 files changed

+48
-39
lines changed

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"remark-autolink-headings": "^5.0.0",
8989
"remark-loader": "^0.3.0",
9090
"remark-mermaid": "^0.2.0",
91+
"remark-slug": "^5.0.0",
9192
"request-promise": "^4.2.2",
9293
"sass-loader": "^6.0.6",
9394
"sitemap-static": "^0.4.2",
@@ -97,6 +98,7 @@
9798
"tap-parser": "^6.0.1",
9899
"through2": "^2.0.3",
99100
"uglifyjs-webpack-plugin": "^1.1.6",
101+
"unist-util-visit": "^1.3.0",
100102
"url-loader": "^0.5.9",
101103
"webpack": "^3.10.0",
102104
"webpack-dev-server": "^2.9.7",
@@ -118,11 +120,7 @@
118120
"react-hot-loader": "^4.0.0-beta.12",
119121
"react-router": "^4.2.0",
120122
"react-router-dom": "^4.2.2",
121-
"remark-parse": "^5.0.0",
122-
"remark-slug": "^5.0.0",
123123
"tool-list": "^0.11.0",
124-
"unified": "^6.1.6",
125-
"unist-util-visit": "^1.3.0",
126124
"webpack.vote": "^0.1.2",
127125
"whatwg-fetch": "^2.0.3"
128126
}

src/utilities/remark-anchors.js

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const visit = require('unist-util-visit');
2+
3+
/**
4+
* A remark plugin to extract anchors markdown headers
5+
*
6+
* @param {object} options - ...
7+
* @return {function} - ...
8+
*/
9+
module.exports = function extractAnchors(options = {}) {
10+
let { anchors, levels } = options
11+
12+
if ( !Array.isArray(anchors) ) {
13+
throw new Error('Missing or malformed `anchors` in options.')
14+
}
15+
16+
return function transformer(ast) {
17+
visit(ast, 'heading', visitor);
18+
};
19+
20+
function visitor(node) {
21+
// TODO: Default header `levels` and check it to filter the `push`ed anchors
22+
options.anchors.push({
23+
title: node.children[0].value,
24+
id: node.data.id
25+
});
26+
}
27+
}

src/utilities/tree-plugin-enhancer.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const fs = require('fs');
22
const FrontMatter = require('front-matter');
3-
const RemarkAnchors = require('./remark-anchors');
3+
const remark = require('remark');
4+
const slug = require('remark-slug');
5+
6+
// TODO: Extract these to separate packages
7+
const ExtractAnchors = require('./remark-extract-anchors');
48

59
module.exports = function(item, options) {
610
item.url = item.path
@@ -10,16 +14,27 @@ module.exports = function(item, options) {
1014
.replace(/^$/, '/');
1115

1216
if (item.type === 'file') {
17+
let anchors = [];
18+
let content = fs.readFileSync(item.path, 'utf8');
19+
let { attributes } = FrontMatter(content);
20+
1321
// remove underscore from fetched files
1422
if (item.name[0] === '_') {
1523
item.name = item.name.replace('_', '');
1624
item.url = item.url.replace('_', '');
1725
}
1826

19-
let content = fs.readFileSync(item.path, 'utf8');
20-
let { attributes } = FrontMatter(content);
27+
remark()
28+
.use(slug)
29+
.use(ExtractAnchors, { anchors })
30+
.process(content, (err, file) => {
31+
if (err) {
32+
throw err;
33+
}
34+
});
35+
36+
item.anchors = anchors;
2137

2238
Object.assign(item, attributes);
23-
item.anchors = RemarkAnchors(content);
2439
}
2540
}

0 commit comments

Comments
 (0)