2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2020-03-24 09:32:44 +01:00
|
|
|
import React, { FC } from "react";
|
2019-10-20 16:59:02 +02:00
|
|
|
import { withRouter, RouteComponentProps } from "react-router-dom";
|
2019-10-21 14:10:48 +02:00
|
|
|
// @ts-ignore
|
2019-10-20 16:59:02 +02:00
|
|
|
import Markdown from "react-markdown/with-html";
|
|
|
|
|
import { binder } from "@scm-manager/ui-extensions";
|
2020-03-24 07:38:07 +01:00
|
|
|
import ErrorBoundary from "./ErrorBoundary";
|
2019-10-20 16:59:02 +02:00
|
|
|
import SyntaxHighlighter from "./SyntaxHighlighter";
|
|
|
|
|
import MarkdownHeadingRenderer from "./MarkdownHeadingRenderer";
|
2020-05-19 15:51:33 +02:00
|
|
|
import { create } from "./MarkdownLinkRenderer";
|
2020-03-24 09:32:44 +01:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
import Notification from "./Notification";
|
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;
|
2020-05-19 15:51:33 +02:00
|
|
|
// basePath for markdown links
|
|
|
|
|
basePath?: string;
|
2019-03-12 18:46:44 +01:00
|
|
|
};
|
|
|
|
|
|
2020-03-24 09:32:44 +01:00
|
|
|
const xmlMarkupSample = `\`\`\`xml
|
|
|
|
|
<your>
|
|
|
|
|
<xml>
|
|
|
|
|
<content/>
|
|
|
|
|
</xml>
|
|
|
|
|
</your>
|
|
|
|
|
\`\`\``;
|
|
|
|
|
|
|
|
|
|
const MarkdownErrorNotification: FC = () => {
|
|
|
|
|
const [t] = useTranslation("commons");
|
|
|
|
|
return (
|
|
|
|
|
<Notification type="danger">
|
|
|
|
|
<div className="content">
|
|
|
|
|
<p className="subtitle">{t("markdownErrorNotification.title")}</p>
|
|
|
|
|
<p>{t("markdownErrorNotification.description")}</p>
|
|
|
|
|
<pre>
|
|
|
|
|
<code>{xmlMarkupSample}</code>
|
|
|
|
|
</pre>
|
|
|
|
|
<p>
|
|
|
|
|
{t("markdownErrorNotification.spec")}:{" "}
|
|
|
|
|
<a href="https://github.github.com/gfm/" target="_blank">
|
|
|
|
|
GitHub Flavored Markdown Spec
|
|
|
|
|
</a>
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</Notification>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
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() {
|
2020-05-19 15:51:33 +02:00
|
|
|
const { content, renderers, renderContext, enableAnchorHeadings, skipHtml, basePath } = 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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-19 15:51:33 +02:00
|
|
|
if (basePath && !rendererList.link) {
|
|
|
|
|
rendererList.link = create(basePath);
|
|
|
|
|
}
|
2020-05-14 12:23:27 +02:00
|
|
|
|
2019-09-16 16:50:08 +02:00
|
|
|
if (!rendererList.code) {
|
2019-03-12 18:46:44 +01:00
|
|
|
rendererList.code = SyntaxHighlighter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2020-03-24 09:32:44 +01:00
|
|
|
<ErrorBoundary fallback={MarkdownErrorNotification}>
|
2020-03-24 07:38:07 +01:00
|
|
|
<div ref={el => (this.contentRef = el)}>
|
|
|
|
|
<Markdown
|
2020-03-24 12:01:36 +01:00
|
|
|
className="content is-word-break"
|
2020-03-24 07:38:07 +01:00
|
|
|
skipHtml={skipHtml}
|
|
|
|
|
escapeHtml={skipHtml}
|
|
|
|
|
source={content}
|
|
|
|
|
renderers={rendererList}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</ErrorBoundary>
|
2019-03-12 18:46:44 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 16:42:08 +02:00
|
|
|
export default withRouter(MarkdownView);
|