Skip to content

Commit

Permalink
Rendering markdown images with attributes. Fixes #774 (#776)
Browse files Browse the repository at this point in the history
* Rendering Markdown images attributes.
* Fixing text replacement and adding new test.
  • Loading branch information
Carlos Reyes authored and danmarshall committed Nov 20, 2017
1 parent a4ae445 commit a16c082
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/FormattedText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ const renderMarkdown = (
// convert <br> tags to blank lines for markdown
.replace(/<br\s*\/?>/ig, '\n')
// URL encode all links
.replace(/\[(.*?)\]\((.*?)\)/ig, (match, text, url) => `[${text}](${markdownIt.normalizeLink(url)})`);
.replace(/\[(.*?)\]\((.*?)( +".*?"){0,1}\)/ig, (match, text, url, title) => `[${text}](${markdownIt.normalizeLink(url)}${title === undefined ? '' : title})`);

const arr = src.split(/\n *\n|\r\n *\r\n|\r *\r/);
const ma = arr.map(a => markdownIt.render(a));

__html = ma.join('<br/>');
} else {
// replace spaces with non-breaking space Unicode characters
// Replace spaces with non-breaking space Unicode characters
__html = text.replace(/ */, '\u00A0');
}

Expand Down
18 changes: 18 additions & 0 deletions test/commands_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,24 @@ var commands_map: CommandValuesMap = {
sendActivity(conversationId, server_content.mar_card);
}
},
"markdown-image-no-title": {
client: function () {
var img = document.querySelectorAll('img')[0] as HTMLImageElement;
return document.querySelectorAll('img')[0].getAttribute('src').indexOf('surface1.jpg') >= 0 && img.getAttribute('title') === null;
},
server: function (conversationId, sendActivity) {
sendActivity(conversationId, server_content.mar_card);
}
},
"markdown-image-title": {
client: function () {
var img = document.querySelectorAll('img')[0] as HTMLImageElement;
return img.getAttribute('src').indexOf('surface1.jpg?abc=123%20456') >= 0 && img.getAttribute('title').indexOf('Title for Surface!') >= 0;
},
server: function (conversationId, sendActivity) {
sendActivity(conversationId, server_content.mar_encode_card);
}
},
"markdown-newlines-single": {
client: function () {
var last_bubble = document.querySelector('.wc-message-wrapper:last-child .wc-message.wc-message-from-bot .format-markdown');
Expand Down
4 changes: 2 additions & 2 deletions test/server_content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export var mar_card: dl.Message = {
timestamp: new Date().toUTCString(),
channelId: "webchat",
textFormat: "markdown",
text: "## Basic formatting\r\n\r\nParagraphs can be written like so. A paragraph is the \r\nbasic block of Markdown. \r\nA paragraph is what text will turn \r\ninto when there is no reason it should become anything else.\r\n\r\nParagraphs must be separated by a blank line. Basic formatting of *italics* and **bold** is supported. This *can be **nested** like* so.\r\n\r\n#### Lists\r\n\r\n### Ordered list\r\n\r\n1. one\r\n2. two\r\n3. three\r\n4. four\r\n\r\n### Unordered list\r\n\r\n* An item\r\n* Another item\r\n* Yet another item\r\n* And there's more...\r\n\r\n## Paragraph modifiers\r\n\r\n### Code block\r\n\r\n```\r\nCode blocks are very useful for developers and other \r\npeople who look at code or other things that are written \r\nin plain text. As you can see, it uses a fixed-width font.\r\n```\r\n\r\nYou can also make `inline code` to add code into other things.\r\n\r\n### Quote\r\n\r\n> Here is a quote. What this is should be self explanatory. \r\n> Quotes are automatically indented when they are used.\r\n\r\n# h1\r\n## h2\r\n### h3\r\n#### h4\r\n\r\n### Headings *can* also contain **formatting**\r\n\r\n## URLs\r\n\r\nURLs can be made in a handful of ways:\r\n\r\n* A named link to [MarkItDown][3]. The easiest way to do these is to select what you want to make a link and hit `Ctrl+L`.\r\n* Another named link to [MarkItDown](http://www.markitdown.net/)\r\n* Some links have [query strings that need encoding](https://bing.com?q=some value)\r\n* Sometimes you just want a URL like <http://www.markitdown.net/>.\r\n\r\n## Images\r\nThis is an image\r\n\r\n![Image of Yaktocat](https://octodex.github.com/images/yaktocat.png)\r\n\r\n\r\n## Horizontal rule\r\n\r\nA horizontal rule is a line that goes across the middle of the page.\r\n\r\n---\r\n\r\nIt's sometimes handy for breaking things up.\r\n\r\n\r\n## Table\r\n|header1|header 2|\r\n|----|----|\r\n| cell 1 | cell 2|\r\n| cell three | cell four|\r\n\r\n## Whitespace\r\n\r\nHere's a line.\r\n\r\nThis has the standard two newlines before it.\r\n\r\n\r\n\r\nThis has four newlines before it.\r\n\r\n\r\n\r\n\r\n\r\nThis has six newlines before it.\r\n\r\n<br/><br/><br/><br/>This has two newlines and four &lt;br/&gt; tags before it."
text: "## Basic formatting\r\n\r\nParagraphs can be written like so. A paragraph is the \r\nbasic block of Markdown. \r\nA paragraph is what text will turn \r\ninto when there is no reason it should become anything else.\r\n\r\nParagraphs must be separated by a blank line. Basic formatting of *italics* and **bold** is supported. This *can be **nested** like* so.\r\n\r\n#### Lists\r\n\r\n### Ordered list\r\n\r\n1. one\r\n2. two\r\n3. three\r\n4. four\r\n\r\n### Unordered list\r\n\r\n* An item\r\n* Another item\r\n* Yet another item\r\n* And there's more...\r\n\r\n## Paragraph modifiers\r\n\r\n### Code block\r\n\r\n```\r\nCode blocks are very useful for developers and other \r\npeople who look at code or other things that are written \r\nin plain text. As you can see, it uses a fixed-width font.\r\n```\r\n\r\nYou can also make `inline code` to add code into other things.\r\n\r\n### Quote\r\n\r\n> Here is a quote. What this is should be self explanatory. \r\n> Quotes are automatically indented when they are used.\r\n\r\n# h1\r\n## h2\r\n### h3\r\n#### h4\r\n\r\n### Headings *can* also contain **formatting**\r\n\r\n## URLs\r\n\r\nURLs can be made in a handful of ways:\r\n\r\n* A named link to [MarkItDown][3]. The easiest way to do these is to select what you want to make a link and hit `Ctrl+L`.\r\n* Another named link to [MarkItDown](http://www.markitdown.net/)\r\n* Some links have [query strings that need encoding](https://bing.com?q=some value)\r\n* Sometimes you just want a URL like <http://www.markitdown.net/>.\r\n\r\n## Images\r\nThis is an image\r\n\r\n![Alt for Surface](" + asset_url + "surface1.jpg)\r\n\r\n\r\n## Horizontal rule\r\n\r\nA horizontal rule is a line that goes across the middle of the page.\r\n\r\n---\r\n\r\nIt's sometimes handy for breaking things up.\r\n\r\n\r\n## Table\r\n|header1|header 2|\r\n|----|----|\r\n| cell 1 | cell 2|\r\n| cell three | cell four|\r\n\r\n## Whitespace\r\n\r\nHere's a line.\r\n\r\nThis has the standard two newlines before it.\r\n\r\n\r\n\r\nThis has four newlines before it.\r\n\r\n\r\n\r\n\r\n\r\nThis has six newlines before it.\r\n\r\n<br/><br/><br/><br/>This has two newlines and four &lt;br/&gt; tags before it."
}

export var mar_encode_card: dl.Message = {
Expand All @@ -338,7 +338,7 @@ export var mar_encode_card: dl.Message = {
timestamp: new Date().toUTCString(),
channelId: "webchat",
textFormat: "markdown",
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.)"
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.)\r\n\r\n\r\nImages must encode URL component with title attribute\r\n\r\n\r\n![Alt for Surface](" + asset_url + "surface1.jpg?abc=123 456 \"Title for Surface!\")\r\n\r\n\r\n"
}

export var mar_newlines_single_card: dl.Message = {
Expand Down

0 comments on commit a16c082

Please sign in to comment.