-
Notifications
You must be signed in to change notification settings - Fork 706
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Parses heading links * Move code to a different file
- Loading branch information
1 parent
008c6bd
commit 74f3fa6
Showing
3 changed files
with
36 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * as React from "react"; | ||
|
||
// Code from https://github.com/rexxars/react-markdown/issues/69 | ||
function flatten(text: string, child: any): any { | ||
return typeof child === "string" | ||
? text + child | ||
: React.Children.toArray(child.props.children).reduce(flatten, text); | ||
} | ||
|
||
const HeadingRenderer: React.SFC<{}> = (props: any) => { | ||
const children = React.Children.toArray(props.children); | ||
const text = children.reduce(flatten, ""); | ||
const slug = text | ||
.toLowerCase() | ||
.replace(/[^a-zA-Z0-9_\s]/g, "") // remove punctuation | ||
.replace(/\s/g, "-"); // replace spaces with dash | ||
return React.createElement("h" + props.level, { id: slug }, props.children); | ||
}; | ||
|
||
export default HeadingRenderer; |