Added markdown anchor support

This commit is contained in:
2018-12-11 23:33:54 +11:00
parent 171fef13a2
commit 92a9736983
6 changed files with 25 additions and 5 deletions
+17
View File
@@ -0,0 +1,17 @@
import React from "react";
//https://github.com/rexxars/react-markdown/issues/69
function headingRenderer(props) {
function flatten(text, child) {
return typeof child === 'string' ? text + child : React.Children.toArray(child.props.children).reduce(flatten, text);
}
let children = React.Children.toArray(props.children);
let text = children.reduce(flatten, '');
let slug = text.toLowerCase().replace(/\W/g, '-');
return React.createElement('h' + props.level, {id: slug}, props.children);
}
export default {
heading: headingRenderer
};