client/markdown: allow to specify image size

This commit is contained in:
rr- 2016-12-22 23:12:58 +01:00
parent 6bf5764c6c
commit 32d498c74b
2 changed files with 39 additions and 2 deletions

View file

@ -28,3 +28,11 @@
</tr>
</tbody>
</table>
<p>You can also specify the size of embedded images like this:</p>
<ul>
<li><code>![alt](href =WIDTHx "title")</code></li>
<li><code>![alt](href =xHEIGHT "title")</code></li>
<li><code>![alt](href =WIDTHxHEIGHT "title")</code></li>
</ul>

View file

@ -115,8 +115,37 @@ class StrikeThroughWrapper extends BaseMarkdownWrapper {
}
}
function formatMarkdown(text) {
function createRenderer() {
function sanitize(str) {
return str.replace(/&<"/g, function (m) {
if (m === '&') {
return '&amp;';
}
if (m === '<') {
return '&lt;';
}
return '&quot;';
});
}
const renderer = new marked.Renderer();
renderer.image = (href, title, alt) => {
let [_, url, width, height] =
/^(.+?)(?:\s=\s*(\d*)\s*x\s*(\d*)\s*)?$/.exec(href);
let res = '<img src="' + sanitize(url) + '" alt="' + sanitize(alt);
if (width) {
res += '" width="' + width;
}
if (height) {
res += '" height="' + height;
}
return res + '">';
}
return renderer;
}
function formatMarkdown(text) {
const renderer = createRenderer();
const options = {
renderer: renderer,
breaks: true,
@ -145,7 +174,7 @@ function formatMarkdown(text) {
}
function formatInlineMarkdown(text) {
const renderer = new marked.Renderer();
const renderer = createRenderer();
const options = {
renderer: renderer,
breaks: true,