Skip to content

Commit 8c593a4

Browse files
committed
feat(base64-wrapping): support for wrapping base64 strings
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
1 parent ff24bdb commit 8c593a4

9 files changed

Lines changed: 79 additions & 25 deletions

dist/showdown.js

Lines changed: 34 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "showdown",
3-
"version": "1.7.3",
3+
"version": "1.7.4",
44
"description": "A Markdown to HTML converter written in Javascript",
55
"author": "Estevão Santos",
66
"homepage": "http://showdownjs.github.io/showdown/",

src/subParsers/images.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ showdown.subParser('images', function (text, options, globals) {
88

99
var inlineRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<?([\S]+?(?:\([\S]*?\)[\S]*?)?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g,
1010
crazyRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<([^>]*)>(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(?:(["'])([^"]*?)\6))?[ \t]?\)/g,
11-
referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[(.*?)]()()()()()/g,
11+
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,
12+
referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[([\s\S]*?)]()()()()()/g,
1213
refShortcutRegExp = /!\[([^\[\]]+)]()()()()()/g;
1314

15+
function writeImageTagBase64 (wholeMatch, altText, linkId, url, width, height, m5, title) {
16+
url = url.replace(/\s/g, '');
17+
return writeImageTag (wholeMatch, altText, linkId, url, width, height, m5, title);
18+
}
19+
1420
function writeImageTag (wholeMatch, altText, linkId, url, width, height, m5, title) {
1521

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

8288
// Next, handle inline images: ![alt text](url =<width>x<height> "optional title")
89+
90+
// base64 encoded images
91+
text = text.replace(base64RegExp, writeImageTagBase64);
92+
8393
// cases with crazy urls like ./image/cat1).png
8494
text = text.replace(crazyRegExp, writeImageTag);
8595

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

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

92102
text = globals.converter._dispatch('images.after', text, options, globals);

src/subParsers/stripLinkDefinitions.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66
showdown.subParser('stripLinkDefinitions', function (text, options, globals) {
77
'use strict';
88

9-
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;
9+
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,
10+
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;
1011

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

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

1824
if (blankLines) {
1925
// Oops, found blank lines, so it's not a title.
@@ -33,7 +39,12 @@ showdown.subParser('stripLinkDefinitions', function (text, options, globals) {
3339
}
3440
// Completely remove the definition from the text
3541
return '';
36-
});
42+
};
43+
44+
// first we try to find base64 link references
45+
text = text.replace(base64Regex, replaceFunc);
46+
47+
text = text.replace(regex, replaceFunc);
3748

3849
// attacklab: strip sentinel
3950
text = text.replace(/¨0/, '');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAYSURBVBhXYwCC/2AAZYEoOAMs8Z+BgQEAXdcR7/Q1gssAAAAASUVORK5CYII=" alt="foo" /></p>
2+
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAYSURBVBhXYwCC/2AAZYEoOAMs8Z+BgQEAXdcR7/Q1gssAAAAASUVORK5CYII=" alt="bar" /></p>
3+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
![foo](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAAXNSR0IArs4c6QAAAARnQU1BAA
2+
Cxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAYSURBVBhXYwCC/2AAZYEoOAMs8Z+BgQEAXdcR7/Q1gssAAAAASUVORK5CYII=)
3+
4+
![bar][]
5+
6+
7+
[bar]:
8+
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAAXNSR0IArs4c6QAAAARnQU1BAA
9+
Cxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAYSURBVBhXYwCC/2AAZYEoOAMs8Z+BgQEAXdcR7/Q1gssAAAAASUVORK5CYII=

0 commit comments

Comments
 (0)