Skip to content

Commit 3acbb7f

Browse files
authored
fix: fenced code doesn't need a trailing newline (#2756)
* fix: fenced code doesn't need a trailing newline * build marked for demo * clean up
1 parent d1f1319 commit 3acbb7f

File tree

7 files changed

+71
-31
lines changed

7 files changed

+71
-31
lines changed

lib/marked.cjs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ function _createClass(Constructor, protoProps, staticProps) {
2828
});
2929
return Constructor;
3030
}
31+
function _extends() {
32+
_extends = Object.assign ? Object.assign.bind() : function (target) {
33+
for (var i = 1; i < arguments.length; i++) {
34+
var source = arguments[i];
35+
for (var key in source) {
36+
if (Object.prototype.hasOwnProperty.call(source, key)) {
37+
target[key] = source[key];
38+
}
39+
}
40+
}
41+
return target;
42+
};
43+
return _extends.apply(this, arguments);
44+
}
3145
function _unsupportedIterableToArray(o, minLen) {
3246
if (!o) return;
3347
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
@@ -1101,7 +1115,7 @@ var Tokenizer = /*#__PURE__*/function () {
11011115
var block = {
11021116
newline: /^(?: *(?:\n|$))+/,
11031117
code: /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,
1104-
fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
1118+
fences: /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
11051119
hr: /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/,
11061120
heading: /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/,
11071121
blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
@@ -1143,13 +1157,13 @@ block.blockquote = edit(block.blockquote).replace('paragraph', block.paragraph).
11431157
* Normal Block Grammar
11441158
*/
11451159

1146-
block.normal = merge({}, block);
1160+
block.normal = _extends({}, block);
11471161

11481162
/**
11491163
* GFM Block Grammar
11501164
*/
11511165

1152-
block.gfm = merge({}, block.normal, {
1166+
block.gfm = _extends({}, block.normal, {
11531167
table: '^ *([^\\n ].*\\|.*)\\n' // Header
11541168
+ ' {0,3}(?:\\| *)?(:?-+:? *(?:\\| *:?-+:? *)*)(?:\\| *)?' // Align
11551169
+ '(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells
@@ -1167,7 +1181,7 @@ block.gfm.paragraph = edit(block._paragraph).replace('hr', block.hr).replace('he
11671181
* Pedantic grammar (original John Gruber's loose markdown specification)
11681182
*/
11691183

1170-
block.pedantic = merge({}, block.normal, {
1184+
block.pedantic = _extends({}, block.normal, {
11711185
html: edit('^ *(?:comment *(?:\\n|\\s*$)' + '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
11721186
+ '|<tag(?:"[^"]*"|\'[^\']*\'|\\s[^\'"/>\\s]*)*?/?> *(?:\\n{2,}|\\s*$))').replace('comment', block._comment).replace(/tag/g, '(?!(?:' + 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub' + '|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)' + '\\b)\\w+(?!:|[^\\w\\s@]*@)\\b').getRegex(),
11731187
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
@@ -1242,13 +1256,13 @@ inline.reflinkSearch = edit(inline.reflinkSearch, 'g').replace('reflink', inline
12421256
* Normal Inline Grammar
12431257
*/
12441258

1245-
inline.normal = merge({}, inline);
1259+
inline.normal = _extends({}, inline);
12461260

12471261
/**
12481262
* Pedantic Inline Grammar
12491263
*/
12501264

1251-
inline.pedantic = merge({}, inline.normal, {
1265+
inline.pedantic = _extends({}, inline.normal, {
12521266
strong: {
12531267
start: /^__|\*\*/,
12541268
middle: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
@@ -1269,7 +1283,7 @@ inline.pedantic = merge({}, inline.normal, {
12691283
* GFM Inline Grammar
12701284
*/
12711285

1272-
inline.gfm = merge({}, inline.normal, {
1286+
inline.gfm = _extends({}, inline.normal, {
12731287
escape: edit(inline.escape).replace('])', '~|])').getRegex(),
12741288
_extended_email: /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/,
12751289
url: /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,
@@ -1282,7 +1296,7 @@ inline.gfm.url = edit(inline.gfm.url, 'i').replace('email', inline.gfm._extended
12821296
* GFM + Line Breaks Inline Grammar
12831297
*/
12841298

1285-
inline.breaks = merge({}, inline.gfm, {
1299+
inline.breaks = _extends({}, inline.gfm, {
12861300
br: edit(inline.br).replace('{2,}', '*').getRegex(),
12871301
text: edit(inline.gfm.text).replace('\\b_', '\\b_| {2,}\\n').replace(/\{2,\}/g, '*').getRegex()
12881302
});

lib/marked.esm.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ class Tokenizer {
11091109
const block = {
11101110
newline: /^(?: *(?:\n|$))+/,
11111111
code: /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,
1112-
fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
1112+
fences: /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
11131113
hr: /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/,
11141114
heading: /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/,
11151115
blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
@@ -1184,17 +1184,18 @@ block.blockquote = edit(block.blockquote)
11841184
* Normal Block Grammar
11851185
*/
11861186

1187-
block.normal = merge({}, block);
1187+
block.normal = { ...block };
11881188

11891189
/**
11901190
* GFM Block Grammar
11911191
*/
11921192

1193-
block.gfm = merge({}, block.normal, {
1193+
block.gfm = {
1194+
...block.normal,
11941195
table: '^ *([^\\n ].*\\|.*)\\n' // Header
11951196
+ ' {0,3}(?:\\| *)?(:?-+:? *(?:\\| *:?-+:? *)*)(?:\\| *)?' // Align
11961197
+ '(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells
1197-
});
1198+
};
11981199

11991200
block.gfm.table = edit(block.gfm.table)
12001201
.replace('hr', block.hr)
@@ -1222,7 +1223,8 @@ block.gfm.paragraph = edit(block._paragraph)
12221223
* Pedantic grammar (original John Gruber's loose markdown specification)
12231224
*/
12241225

1225-
block.pedantic = merge({}, block.normal, {
1226+
block.pedantic = {
1227+
...block.normal,
12261228
html: edit(
12271229
'^ *(?:comment *(?:\\n|\\s*$)'
12281230
+ '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
@@ -1246,7 +1248,7 @@ block.pedantic = merge({}, block.normal, {
12461248
.replace('|list', '')
12471249
.replace('|html', '')
12481250
.getRegex()
1249-
});
1251+
};
12501252

12511253
/**
12521254
* Inline-Level Grammar
@@ -1348,13 +1350,14 @@ inline.reflinkSearch = edit(inline.reflinkSearch, 'g')
13481350
* Normal Inline Grammar
13491351
*/
13501352

1351-
inline.normal = merge({}, inline);
1353+
inline.normal = { ...inline };
13521354

13531355
/**
13541356
* Pedantic Inline Grammar
13551357
*/
13561358

1357-
inline.pedantic = merge({}, inline.normal, {
1359+
inline.pedantic = {
1360+
...inline.normal,
13581361
strong: {
13591362
start: /^__|\*\*/,
13601363
middle: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
@@ -1373,20 +1376,21 @@ inline.pedantic = merge({}, inline.normal, {
13731376
reflink: edit(/^!?\[(label)\]\s*\[([^\]]*)\]/)
13741377
.replace('label', inline._label)
13751378
.getRegex()
1376-
});
1379+
};
13771380

13781381
/**
13791382
* GFM Inline Grammar
13801383
*/
13811384

1382-
inline.gfm = merge({}, inline.normal, {
1385+
inline.gfm = {
1386+
...inline.normal,
13831387
escape: edit(inline.escape).replace('])', '~|])').getRegex(),
13841388
_extended_email: /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/,
13851389
url: /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,
13861390
_backpedal: /(?:[^?!.,:;*_'"~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_'"~)]+(?!$))+/,
13871391
del: /^(~~?)(?=[^\s~])([\s\S]*?[^\s~])\1(?=[^~]|$)/,
13881392
text: /^([`~]+|[^`~])(?:(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\<!\[`*~_]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)))/
1389-
});
1393+
};
13901394

13911395
inline.gfm.url = edit(inline.gfm.url, 'i')
13921396
.replace('email', inline.gfm._extended_email)
@@ -1395,13 +1399,14 @@ inline.gfm.url = edit(inline.gfm.url, 'i')
13951399
* GFM + Line Breaks Inline Grammar
13961400
*/
13971401

1398-
inline.breaks = merge({}, inline.gfm, {
1402+
inline.breaks = {
1403+
...inline.gfm,
13991404
br: edit(inline.br).replace('{2,}', '*').getRegex(),
14001405
text: edit(inline.gfm.text)
14011406
.replace('\\b_', '\\b_| {2,}\\n')
14021407
.replace(/\{2,\}/g, '*')
14031408
.getRegex()
1404-
});
1409+
};
14051410

14061411
/**
14071412
* smartypants text replacement

lib/marked.umd.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@
3232
});
3333
return Constructor;
3434
}
35+
function _extends() {
36+
_extends = Object.assign ? Object.assign.bind() : function (target) {
37+
for (var i = 1; i < arguments.length; i++) {
38+
var source = arguments[i];
39+
for (var key in source) {
40+
if (Object.prototype.hasOwnProperty.call(source, key)) {
41+
target[key] = source[key];
42+
}
43+
}
44+
}
45+
return target;
46+
};
47+
return _extends.apply(this, arguments);
48+
}
3549
function _unsupportedIterableToArray(o, minLen) {
3650
if (!o) return;
3751
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
@@ -1105,7 +1119,7 @@
11051119
var block = {
11061120
newline: /^(?: *(?:\n|$))+/,
11071121
code: /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,
1108-
fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
1122+
fences: /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
11091123
hr: /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/,
11101124
heading: /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/,
11111125
blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
@@ -1147,13 +1161,13 @@
11471161
* Normal Block Grammar
11481162
*/
11491163

1150-
block.normal = merge({}, block);
1164+
block.normal = _extends({}, block);
11511165

11521166
/**
11531167
* GFM Block Grammar
11541168
*/
11551169

1156-
block.gfm = merge({}, block.normal, {
1170+
block.gfm = _extends({}, block.normal, {
11571171
table: '^ *([^\\n ].*\\|.*)\\n' // Header
11581172
+ ' {0,3}(?:\\| *)?(:?-+:? *(?:\\| *:?-+:? *)*)(?:\\| *)?' // Align
11591173
+ '(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells
@@ -1171,7 +1185,7 @@
11711185
* Pedantic grammar (original John Gruber's loose markdown specification)
11721186
*/
11731187

1174-
block.pedantic = merge({}, block.normal, {
1188+
block.pedantic = _extends({}, block.normal, {
11751189
html: edit('^ *(?:comment *(?:\\n|\\s*$)' + '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
11761190
+ '|<tag(?:"[^"]*"|\'[^\']*\'|\\s[^\'"/>\\s]*)*?/?> *(?:\\n{2,}|\\s*$))').replace('comment', block._comment).replace(/tag/g, '(?!(?:' + 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub' + '|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)' + '\\b)\\w+(?!:|[^\\w\\s@]*@)\\b').getRegex(),
11771191
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
@@ -1246,13 +1260,13 @@
12461260
* Normal Inline Grammar
12471261
*/
12481262

1249-
inline.normal = merge({}, inline);
1263+
inline.normal = _extends({}, inline);
12501264

12511265
/**
12521266
* Pedantic Inline Grammar
12531267
*/
12541268

1255-
inline.pedantic = merge({}, inline.normal, {
1269+
inline.pedantic = _extends({}, inline.normal, {
12561270
strong: {
12571271
start: /^__|\*\*/,
12581272
middle: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
@@ -1273,7 +1287,7 @@
12731287
* GFM Inline Grammar
12741288
*/
12751289

1276-
inline.gfm = merge({}, inline.normal, {
1290+
inline.gfm = _extends({}, inline.normal, {
12771291
escape: edit(inline.escape).replace('])', '~|])').getRegex(),
12781292
_extended_email: /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/,
12791293
url: /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,
@@ -1286,7 +1300,7 @@
12861300
* GFM + Line Breaks Inline Grammar
12871301
*/
12881302

1289-
inline.breaks = merge({}, inline.gfm, {
1303+
inline.breaks = _extends({}, inline.gfm, {
12901304
br: edit(inline.br).replace('{2,}', '*').getRegex(),
12911305
text: edit(inline.gfm.text).replace('\\b_', '\\b_| {2,}\\n').replace(/\{2,\}/g, '*').getRegex()
12921306
});

marked.min.js

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

src/rules.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
export const block = {
1111
newline: /^(?: *(?:\n|$))+/,
1212
code: /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,
13-
fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
13+
fences: /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
1414
hr: /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/,
1515
heading: /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/,
1616
blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<pre>
2+
<code>
3+
no newline at end of file
4+
</code>
5+
</pre>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
```
2+
no newline at end of file

0 commit comments

Comments
 (0)