/
MarkdownViewer.js
68 lines (61 loc) · 2.02 KB
/
MarkdownViewer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React from "react";
import MarkdownIt from "markdown-it";
import "./MarkdownViewer.scss";
import defined from "../helpers/defined";
import truncate from "html-truncate";
var DOMPurify = require("dompurify/dist/purify");
class MarkdownViewer extends React.Component {
render() {
let html = markdownToHtml(this.props.markdown);
html = html ? html : "";
if (this.props.truncate === true) {
html = truncate(
html,
this.props.truncateLength ? this.props.truncateLength : 150
);
}
let markdown = { __html: html };
return <div className="markdown" dangerouslySetInnerHTML={markdown} />;
}
}
MarkdownViewer.defaultProps = { markdown: "" };
export default MarkdownViewer;
const md = new MarkdownIt({
html: true,
linkify: true
});
const htmlRegex = /^\s*<[^>]+>/;
export function markdownToHtml(markdownString, allowUnsafeHtml, options) {
if (!defined(markdownString) || markdownString.length === 0) {
return markdownString;
}
// If the text looks like html, don't try to interpret it as Markdown because
// we'll probably break it in the process.
var unsafeHtml;
if (htmlRegex.test(markdownString)) {
unsafeHtml = markdownString;
} else {
// Note this would wrap non-standard tags such as <collapsible>hi</collapsible> in a <p></p>, which is bad.
unsafeHtml = md.render(markdownString);
}
if (allowUnsafeHtml) {
return unsafeHtml;
} else {
return DOMPurify.sanitize(unsafeHtml, options);
}
}
/**
* Tell whether content provided will be truncated or not.
* It's useful when you need to tell whether a toggel button should be shown or not
*/
export function willBeTruncated(
markdownString,
truncateLength,
allowUnsafeHtml,
options
) {
const OrigHtml = markdownToHtml(markdownString);
const TruncatedHtml = truncate(OrigHtml, truncateLength);
if (OrigHtml.trim() === TruncatedHtml.trim()) return false;
return true;
}