This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
message_html_to_component.jsx
143 lines (126 loc) · 5.15 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// 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 LinkTooltip from 'components/link_tooltip/link_tooltip';
import MarkdownImage from 'components/markdown_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.
* - imagesMetadata - the dimensions of the image as retrieved from post.metadata.images.
* - hasPluginTooltips - If specified, the LinkTooltip component is placed inside links. Defaults to false.
*/
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 = [
// Workaround to fix MM-14931
{
replaceChildren: false,
shouldProcessNode: (node) => node.type === 'tag' && node.name === 'input' && node.attribs.type === 'checkbox',
processNode: (node) => {
const attribs = node.attribs || {};
node.attribs.checked = Boolean(attribs.checked);
return React.createElement('input', {...node.attribs});
},
},
];
if (options.hasPluginTooltips) {
const hrefAttrib = 'href';
processingInstructions.push({
replaceChildren: true,
shouldProcessNode: (node) => node.type === 'tag' && node.name === 'a' && node.attribs[hrefAttrib],
processNode: (node, children) => {
return (
<LinkTooltip
href={node.attribs[hrefAttrib]}
title={children[0]}
/>
);
},
});
}
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];
return <PostEmoji name={emojiName}/>;
},
});
}
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 (
<MarkdownImage
className={className}
imageMetadata={options.imagesMetadata && options.imagesMetadata[attribs.src]}
{...attribs}
{...options.imageProps}
postId={options.postId}
imageIsLink={html.includes('<a')}
postType={options.postType}
/>
);
},
});
}
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;