Skip to content

Commit

Permalink
feat(base64-wrapping): support for wrapping base64 strings
Browse files Browse the repository at this point in the history
Wrapping base64 strings, which are usually extremely long lines of text, is now supported.
Newlines can be added arbitrarily, as long as they appear after the comma (,) character.

Closes #429
  • Loading branch information
tivie committed Sep 8, 2017
1 parent ff24bdb commit 8c593a4
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 25 deletions.
47 changes: 34 additions & 13 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "showdown",
"version": "1.7.3",
"version": "1.7.4",
"description": "A Markdown to HTML converter written in Javascript",
"author": "Estevão Santos",
"homepage": "http://showdownjs.github.io/showdown/",
Expand Down
14 changes: 12 additions & 2 deletions src/subParsers/images.js
Expand Up @@ -8,9 +8,15 @@ showdown.subParser('images', function (text, options, globals) {

var inlineRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<?([\S]+?(?:\([\S]*?\)[\S]*?)?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g,
crazyRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<([^>]*)>(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(?:(["'])([^"]*?)\6))?[ \t]?\)/g,
referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[(.*?)]()()()()()/g,
base64RegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<?(data:.+?\/.+?;base64,[A-Za-z0-9+/=\n]+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g,
referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[([\s\S]*?)]()()()()()/g,
refShortcutRegExp = /!\[([^\[\]]+)]()()()()()/g;

function writeImageTagBase64 (wholeMatch, altText, linkId, url, width, height, m5, title) {
url = url.replace(/\s/g, '');
return writeImageTag (wholeMatch, altText, linkId, url, width, height, m5, title);
}

function writeImageTag (wholeMatch, altText, linkId, url, width, height, m5, title) {

var gUrls = globals.gUrls,
Expand Down Expand Up @@ -80,13 +86,17 @@ showdown.subParser('images', function (text, options, globals) {
text = text.replace(referenceRegExp, writeImageTag);

// Next, handle inline images: ![alt text](url =<width>x<height> "optional title")

// base64 encoded images
text = text.replace(base64RegExp, writeImageTagBase64);

// cases with crazy urls like ./image/cat1).png
text = text.replace(crazyRegExp, writeImageTag);

// normal cases
text = text.replace(inlineRegExp, writeImageTag);

// handle reference-style shortcuts: |[img text]
// handle reference-style shortcuts: ![img text]
text = text.replace(refShortcutRegExp, writeImageTag);

text = globals.converter._dispatch('images.after', text, options, globals);
Expand Down
19 changes: 15 additions & 4 deletions src/subParsers/stripLinkDefinitions.js
Expand Up @@ -6,14 +6,20 @@
showdown.subParser('stripLinkDefinitions', function (text, options, globals) {
'use strict';

var regex = /^ {0,3}\[(.+)]:[ \t]*\n?[ \t]*<?([^>\s]+)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n+|(?=¨0))/gm;
var regex = /^ {0,3}\[(.+)]:[ \t]*\n?[ \t]*<?([^>\s]+)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n+|(?=¨0))/gm,
base64Regex = /^ {0,3}\[(.+)]:[ \t]*\n?[ \t]*<?(data:.+?\/.+?;base64,[A-Za-z0-9+/=\n]+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n\n|(?=¨0)|(?=\n\[))/gm;

// attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
text += '¨0';

text = text.replace(regex, function (wholeMatch, linkId, url, width, height, blankLines, title) {
var replaceFunc = function (wholeMatch, linkId, url, width, height, blankLines, title) {
linkId = linkId.toLowerCase();
globals.gUrls[linkId] = showdown.subParser('encodeAmpsAndAngles')(url, options, globals); // Link IDs are case-insensitive
if (url.match(/^data:.+?\/.+?;base64,/)) {
// remove newlines
globals.gUrls[linkId] = url.replace(/\s/g, '');
} else {
globals.gUrls[linkId] = showdown.subParser('encodeAmpsAndAngles')(url, options, globals); // Link IDs are case-insensitive
}

if (blankLines) {
// Oops, found blank lines, so it's not a title.
Expand All @@ -33,7 +39,12 @@ showdown.subParser('stripLinkDefinitions', function (text, options, globals) {
}
// Completely remove the definition from the text
return '';
});
};

// first we try to find base64 link references
text = text.replace(base64Regex, replaceFunc);

text = text.replace(regex, replaceFunc);

// attacklab: strip sentinel
text = text.replace(/¨0/, '');
Expand Down
3 changes: 3 additions & 0 deletions test/issues/#429.multiline-base64-image-support.html
@@ -0,0 +1,3 @@
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAYSURBVBhXYwCC/2AAZYEoOAMs8Z+BgQEAXdcR7/Q1gssAAAAASUVORK5CYII=" alt="foo" /></p>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAYSURBVBhXYwCC/2AAZYEoOAMs8Z+BgQEAXdcR7/Q1gssAAAAASUVORK5CYII=" alt="bar" /></p>

9 changes: 9 additions & 0 deletions test/issues/#429.multiline-base64-image-support.md
@@ -0,0 +1,9 @@
![foo](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAAXNSR0IArs4c6QAAAARnQU1BAA
Cxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAYSURBVBhXYwCC/2AAZYEoOAMs8Z+BgQEAXdcR7/Q1gssAAAAASUVORK5CYII=)

![bar][]


[bar]:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAAXNSR0IArs4c6QAAAARnQU1BAA
Cxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAYSURBVBhXYwCC/2AAZYEoOAMs8Z+BgQEAXdcR7/Q1gssAAAAASUVORK5CYII=

0 comments on commit 8c593a4

Please sign in to comment.