Skip to content

Commit 97ad966

Browse files
committed
Messages: Support React elements in the message (start-end tags)
1 parent fd693d6 commit 97ad966

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/message.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function replaceElements(componentProps, formatted) {
101101
var element = elements[key];
102102

103103
ret.forEach(function(string, i) {
104-
var aux;
104+
var aux, contents, regexp, regexp2;
105105

106106
// Insert array into the correct ret position.
107107
function replaceRetItem(array) {
@@ -119,6 +119,20 @@ function replaceElements(componentProps, formatted) {
119119
replaceRetItem(aux);
120120
return; // continue;
121121
}
122+
123+
// Start-end tags, e.g., `[foo]content[/foo]`.
124+
regexp = new RegExp("\\[" + key + "\\][\\s\\S]*?\\[\\/" + key + "\\]", "g");
125+
regexp2 = new RegExp("\\[" + key + "\\]([\\s\\S]*?)\\[\\/" + key + "\\]");
126+
aux = string.split(regexp);
127+
if (aux.length > 1) {
128+
contents = string.match(regexp).map(function(content) {
129+
return content.replace(regexp2, "$1");
130+
});
131+
aux = spreadElementsInBetweenItems(aux, function(i) {
132+
return React.cloneElement(element, {}, contents[i]);
133+
});
134+
replaceRetItem(aux);
135+
}
122136
});
123137

124138
return ret;

0 commit comments

Comments
 (0)