2019-10-20 16:59:02 +02:00
|
|
|
import React from "react";
|
|
|
|
|
import { withRouter, RouteComponentProps } from "react-router-dom";
|
|
|
|
|
import Markdown from "react-markdown/with-html";
|
|
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { binder } from "@scm-manager/ui-extensions";
|
|
|
|
|
import SyntaxHighlighter from "./SyntaxHighlighter";
|
|
|
|
|
import MarkdownHeadingRenderer from "./MarkdownHeadingRenderer";
|
2019-03-12 18:46:44 +01:00
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
type Props = RouteComponentProps & {
|
2019-10-19 16:38:07 +02:00
|
|
|
content: string;
|
|
|
|
|
renderContext?: object;
|
2019-10-20 16:59:02 +02:00
|
|
|
renderers?: any;
|
2019-10-19 16:38:07 +02:00
|
|
|
skipHtml?: boolean;
|
2019-10-20 16:59:02 +02:00
|
|
|
enableAnchorHeadings?: boolean;
|
2019-03-12 18:46:44 +01:00
|
|
|
};
|
|
|
|
|
|
2019-10-08 16:42:08 +02:00
|
|
|
const MarkdownWrapper = styled.div`
|
2019-10-09 13:37:40 +02:00
|
|
|
> .content {
|
2019-10-19 16:38:07 +02:00
|
|
|
> h1,
|
|
|
|
|
h2,
|
|
|
|
|
h3,
|
|
|
|
|
h4,
|
|
|
|
|
h5,
|
|
|
|
|
h6 {
|
2019-10-08 16:42:08 +02:00
|
|
|
margin: 0.5rem 0;
|
|
|
|
|
font-size: 0.9rem;
|
|
|
|
|
}
|
2019-10-09 13:37:40 +02:00
|
|
|
> h1 {
|
2019-10-08 16:42:08 +02:00
|
|
|
font-weight: 700;
|
|
|
|
|
}
|
2019-10-09 13:37:40 +02:00
|
|
|
> h2 {
|
2019-10-08 16:42:08 +02:00
|
|
|
font-weight: 600;
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
> h3,
|
|
|
|
|
h4,
|
|
|
|
|
h5,
|
|
|
|
|
h6 {
|
2019-10-08 16:42:08 +02:00
|
|
|
font-weight: 500;
|
|
|
|
|
}
|
2019-10-09 13:37:40 +02:00
|
|
|
& strong {
|
2019-10-08 16:42:08 +02:00
|
|
|
font-weight: 500;
|
2019-09-16 16:50:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-10-08 16:42:08 +02:00
|
|
|
`;
|
2019-03-12 18:46:44 +01:00
|
|
|
|
2019-09-16 16:50:08 +02:00
|
|
|
class MarkdownView extends React.Component<Props> {
|
2019-10-20 16:59:02 +02:00
|
|
|
static defaultProps: Partial<Props> = {
|
2019-10-17 12:07:20 +02:00
|
|
|
enableAnchorHeadings: false,
|
2019-10-20 16:59:02 +02:00
|
|
|
skipHtml: false
|
2019-05-07 10:11:26 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
contentRef: HTMLDivElement | null | undefined;
|
2019-05-21 10:22:53 +02:00
|
|
|
|
2019-05-07 10:11:26 +02:00
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-21 10:22:53 +02:00
|
|
|
componentDidUpdate() {
|
|
|
|
|
// we have to use componentDidUpdate, because we have to wait until all
|
|
|
|
|
// children are rendered and componentDidMount is called before the
|
|
|
|
|
// markdown content was rendered.
|
|
|
|
|
const hash = this.props.location.hash;
|
|
|
|
|
if (this.contentRef && hash) {
|
|
|
|
|
// we query only child elements, to avoid strange scrolling with multiple
|
|
|
|
|
// markdown elements on one page.
|
|
|
|
|
const element = this.contentRef.querySelector(hash);
|
|
|
|
|
if (element && element.scrollIntoView) {
|
|
|
|
|
element.scrollIntoView();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-12 18:46:44 +01:00
|
|
|
render() {
|
2019-09-16 16:50:08 +02:00
|
|
|
const {
|
|
|
|
|
content,
|
|
|
|
|
renderers,
|
|
|
|
|
renderContext,
|
2019-10-17 12:07:20 +02:00
|
|
|
enableAnchorHeadings,
|
2019-10-20 16:59:02 +02:00
|
|
|
skipHtml
|
2019-09-16 16:50:08 +02:00
|
|
|
} = this.props;
|
2019-03-12 18:46:44 +01:00
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
const rendererFactory = binder.getExtension("markdown-renderer-factory");
|
2019-03-12 18:46:44 +01:00
|
|
|
let rendererList = renderers;
|
|
|
|
|
|
2019-09-16 16:50:08 +02:00
|
|
|
if (rendererFactory) {
|
2019-03-12 18:46:44 +01:00
|
|
|
rendererList = rendererFactory(renderContext);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-16 16:50:08 +02:00
|
|
|
if (!rendererList) {
|
2019-03-12 18:46:44 +01:00
|
|
|
rendererList = {};
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 10:11:26 +02:00
|
|
|
if (enableAnchorHeadings) {
|
|
|
|
|
rendererList.heading = MarkdownHeadingRenderer;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-16 16:50:08 +02:00
|
|
|
if (!rendererList.code) {
|
2019-03-12 18:46:44 +01:00
|
|
|
rendererList.code = SyntaxHighlighter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2019-10-08 16:42:08 +02:00
|
|
|
<MarkdownWrapper ref={el => (this.contentRef = el)}>
|
2019-05-21 10:22:53 +02:00
|
|
|
<Markdown
|
|
|
|
|
className="content"
|
2019-10-17 12:07:20 +02:00
|
|
|
skipHtml={skipHtml}
|
|
|
|
|
escapeHtml={skipHtml}
|
2019-05-21 10:22:53 +02:00
|
|
|
source={content}
|
|
|
|
|
renderers={rendererList}
|
|
|
|
|
/>
|
2019-10-08 16:42:08 +02:00
|
|
|
</MarkdownWrapper>
|
2019-03-12 18:46:44 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 16:42:08 +02:00
|
|
|
export default withRouter(MarkdownView);
|