mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 07:25:44 +01:00
scm-ui: new repository layout
This commit is contained in:
47
scm-ui/ui-components/src/MarkdownHeadingRenderer.js
Normal file
47
scm-ui/ui-components/src/MarkdownHeadingRenderer.js
Normal file
@@ -0,0 +1,47 @@
|
||||
// @flow
|
||||
import * as React from "react";
|
||||
import { withRouter } from "react-router-dom";
|
||||
import { withContextPath } from "./urls";
|
||||
|
||||
/**
|
||||
* Adds anchor links to markdown headings.
|
||||
*
|
||||
* @see <a href="https://github.com/rexxars/react-markdown/issues/69">Headings are missing anchors / ids</a>
|
||||
*/
|
||||
|
||||
type Props = {
|
||||
children: React.Node,
|
||||
level: number,
|
||||
location: any
|
||||
};
|
||||
|
||||
function flatten(text: string, child: any) {
|
||||
return typeof child === "string"
|
||||
? text + child
|
||||
: React.Children.toArray(child.props.children).reduce(flatten, text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns heading text into a anchor id
|
||||
*
|
||||
* @VisibleForTesting
|
||||
*/
|
||||
export function headingToAnchorId(heading: string) {
|
||||
return heading.toLowerCase().replace(/\W/g, "-");
|
||||
}
|
||||
|
||||
function MarkdownHeadingRenderer(props: Props) {
|
||||
const children = React.Children.toArray(props.children);
|
||||
const heading = children.reduce(flatten, "");
|
||||
const anchorId = headingToAnchorId(heading);
|
||||
const headingElement = React.createElement("h" + props.level, {}, props.children);
|
||||
const href = withContextPath(props.location.pathname + "#" + anchorId);
|
||||
|
||||
return (
|
||||
<a id={`${anchorId}`} className="anchor" href={href}>
|
||||
{headingElement}
|
||||
</a>
|
||||
);
|
||||
}
|
||||
|
||||
export default withRouter(MarkdownHeadingRenderer);
|
||||
Reference in New Issue
Block a user