Skip to content

Commit a4ae445

Browse files
Carlos Reyesdanmarshall
Carlos Reyes
authored andcommitted
Markdown newlines Fixes #562 (#773)
* Implementing markdown-newlines. * Implementing markdown newlines test. * Fixing regex and test. * Cleaning testOnly array. * Markdown newlines tests.
1 parent ac7a557 commit a4ae445

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

src/FormattedText.tsx

+10-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const renderPlainText = (text: string) => {
2626
return <span className="format-plain">{elements}</span>;
2727
}
2828

29-
const markdownIt = new MarkdownIt({ html: false, linkify: true, typographer: true });
29+
const markdownIt = new MarkdownIt({ html: false, xhtmlOut: true, breaks: true, linkify: true, typographer: true });
3030

3131
//configure MarkdownIt to open links in new tab
3232
//from https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer
@@ -58,11 +58,15 @@ const renderMarkdown = (
5858

5959
if (text.trim()) {
6060
const src = text
61-
// convert <br> tags to blank lines for markdown
62-
.replace(/<br\s*\/?>/ig, '\r\n\r\n')
63-
// URL encode all links
64-
.replace(/\[(.*?)\]\((.*?)\)/ig, (match, text, url) => `[${text}](${markdownIt.normalizeLink(url)})`);
65-
__html = markdownIt.render(src);
61+
// convert <br> tags to blank lines for markdown
62+
.replace(/<br\s*\/?>/ig, '\n')
63+
// URL encode all links
64+
.replace(/\[(.*?)\]\((.*?)\)/ig, (match, text, url) => `[${text}](${markdownIt.normalizeLink(url)})`);
65+
66+
const arr = src.split(/\n *\n|\r\n *\r\n|\r *\r/);
67+
const ma = arr.map(a => markdownIt.render(a));
68+
69+
__html = ma.join('<br/>');
6670
} else {
6771
// replace spaces with non-breaking space Unicode characters
6872
__html = text.replace(/ */, '\u00A0');

test/commands_map.ts

+30-3
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ var commands_map: CommandValuesMap = {
203203
if (!link) return false;
204204

205205
//check if value is encoded
206-
var is_file = link.href.indexOf("test.txt") >= 0;
206+
var is_file = link.href.indexOf("test.txt") >= 0;
207207
link.click();
208208

209209
return is_file && window.location.href.indexOf("localhost") !== -1;
@@ -265,6 +265,33 @@ var commands_map: CommandValuesMap = {
265265
sendActivity(conversationId, server_content.mar_card);
266266
}
267267
},
268+
"markdown-newlines-single": {
269+
client: function () {
270+
var last_bubble = document.querySelector('.wc-message-wrapper:last-child .wc-message.wc-message-from-bot .format-markdown');
271+
return last_bubble.getElementsByTagName('p').length === 2 && last_bubble.getElementsByTagName('br').length === 3;
272+
},
273+
server: function (conversationId, sendActivity) {
274+
sendActivity(conversationId, server_content.mar_newlines_single_card);
275+
}
276+
},
277+
"markdown-newlines-double": {
278+
client: function () {
279+
var last_bubble = document.querySelector('.wc-message-wrapper:last-child .wc-message.wc-message-from-bot .format-markdown');
280+
return last_bubble.getElementsByTagName('p').length === 4 && last_bubble.getElementsByTagName('br').length === 3;
281+
},
282+
server: function (conversationId, sendActivity) {
283+
sendActivity(conversationId, server_content.mar_newlines_double_card);
284+
}
285+
},
286+
"markdown-newlines-double-double": {
287+
client: function () {
288+
var last_bubble = document.querySelector('.wc-message-wrapper:last-child .wc-message.wc-message-from-bot .format-markdown');
289+
return last_bubble.getElementsByTagName('p').length === 4 && last_bubble.getElementsByTagName('br').length === 5;
290+
},
291+
server: function (conversationId, sendActivity) {
292+
sendActivity(conversationId, server_content.mar_newlines_ddouble_card);
293+
}
294+
},
268295
"markdown-url-needs-encoding": {
269296
client: function () {
270297
var links = document.querySelectorAll('.wc-message-wrapper:last-child .wc-message.wc-message-from-bot a');
@@ -377,7 +404,7 @@ var commands_map: CommandValuesMap = {
377404
"upload": {
378405
do: function (nightmare) {
379406
try {
380-
const upload = <(selector: string, paths: string[]) => Nightmare>(<any> nightmare.upload.bind(nightmare));
407+
const upload = <(selector: string, paths: string[]) => Nightmare>(<any>nightmare.upload.bind(nightmare));
381408

382409
upload('#wc-upload-input', [
383410
path.resolve(__dirname, 'assets', 'surface1.jpg'),
@@ -393,7 +420,7 @@ var commands_map: CommandValuesMap = {
393420
var img = document.querySelectorAll('.wc-message-wrapper:last-child .wc-message.wc-message-from-bot img')[0] as HTMLImageElement;
394421
return img.src.indexOf('/uploads') >= 0;
395422
},
396-
server: function(conversationId, sendActivity){
423+
server: function (conversationId, sendActivity) {
397424
sendActivity(conversationId, server_content.upload_txt);
398425
}
399426
},

test/server_content.ts

+27
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,33 @@ export var mar_encode_card: dl.Message = {
341341
text: "Some links are [already encoded](https://bing.com?q=some%20value) and some have [query strings that need encoding](https://bing.com?q=some value) and these can occur [multiple](https://bing.com?q=some%20value) [times](https://bing.com?q=some value) in one message. (Links can even appear [within](https://bing.com?q=some%20value) [parentheses](https://bing.com?q=some value) too.)"
342342
}
343343

344+
export var mar_newlines_single_card: dl.Message = {
345+
type: "message",
346+
from: bot,
347+
timestamp: new Date().toUTCString(),
348+
channelId: "webchat",
349+
textFormat: "markdown",
350+
text: "**Windows** single newline->\r\n<-finish. \r\n\r\n**Unix** single newline->\n<-finish."
351+
}
352+
353+
export var mar_newlines_double_card: dl.Message = {
354+
type: "message",
355+
from: bot,
356+
timestamp: new Date().toUTCString(),
357+
channelId: "webchat",
358+
textFormat: "markdown",
359+
text: "**Windows** double newline->\r\n \r\n<-finish. \r\n\r\n**Unix** double newline->\n \n<-finish."
360+
}
361+
362+
export var mar_newlines_ddouble_card: dl.Message = {
363+
type: "message",
364+
from: bot,
365+
timestamp: new Date().toUTCString(),
366+
channelId: "webchat",
367+
textFormat: "markdown",
368+
text: "**Windows** double-double newline->\r\n \r\n \r\n \r\n<-finish. \r\n\r\n**Unix** double-double newline->\n \n \n \n<-finish."
369+
}
370+
344371
/*
345372
* Activity for SignIn
346373
*

0 commit comments

Comments
 (0)