forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_html_to_component.jsx
112 lines (99 loc) · 3.87 KB
/
message_html_to_component.jsx
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import {Parser, ProcessNodeDefinitions} from 'html-to-react';
import AtMention from 'components/at_mention';
import LatexBlock from 'components/latex_block';
import SizeAwareImage from 'components/size_aware_image';
import PostEmoji from 'components/post_emoji';
/*
* Converts HTML to React components using html-to-react.
* The following options can be specified:
* - mentions - If specified, mentions are replaced with the AtMention component. Defaults to true.
* - emoji - If specified, emoji text is replaced with the PostEmoji component. Defaults to true.
* - images - If specified, markdown images are replaced with the image component. Defaults to true.
* - imageProps - If specified, any extra props that should be passed into the image component.
* - latex - If specified, latex is replaced with the LatexBlock component. Defaults to true.
*/
export function messageHtmlToComponent(html, isRHS, options = {}) {
if (!html) {
return null;
}
const parser = new Parser();
const processNodeDefinitions = new ProcessNodeDefinitions(React);
function isValidNode() {
return true;
}
const processingInstructions = [];
if (!('mentions' in options) || options.mentions) {
const mentionAttrib = 'data-mention';
processingInstructions.push({
replaceChildren: true,
shouldProcessNode: (node) => node.attribs && node.attribs[mentionAttrib],
processNode: (node, children) => {
const mentionName = node.attribs[mentionAttrib];
const callAtMention = (
<AtMention
mentionName={mentionName}
isRHS={isRHS}
hasMention={true}
>
{children}
</AtMention>
);
return callAtMention;
},
});
}
if (!('emoji' in options) || options.emoji) {
const emojiAttrib = 'data-emoticon';
processingInstructions.push({
replaceChildren: true,
shouldProcessNode: (node) => node.attribs && node.attribs[emojiAttrib],
processNode: (node) => {
const emojiName = node.attribs[emojiAttrib];
const callPostEmoji = (
<PostEmoji
name={emojiName}
/>
);
return callPostEmoji;
},
});
}
if (!('images' in options) || options.images) {
processingInstructions.push({
shouldProcessNode: (node) => node.type === 'tag' && node.name === 'img',
processNode: (node) => {
const {
class: className,
...attribs
} = node.attribs;
return (
<SizeAwareImage
className={className}
dimensions={options.imagesMetadata[attribs.src]}
{...attribs}
{...options.imageProps}
/>
);
},
});
}
if (!('latex' in options) || options.latex) {
processingInstructions.push({
shouldProcessNode: (node) => node.attribs && node.attribs['data-latex'],
processNode: (node) => {
return (
<LatexBlock content={node.attribs['data-latex']}/>
);
},
});
}
processingInstructions.push({
shouldProcessNode: () => true,
processNode: processNodeDefinitions.processDefaultNode,
});
return parser.parseWithInstructions(html, isValidNode, processingInstructions);
}
export default messageHtmlToComponent;