Skip to content

Commit f36f676

Browse files
author
MarkedJS bot
committed
🗜️ build [skip ci]
1 parent dddf9ae commit f36f676

File tree

3 files changed

+270
-59
lines changed

3 files changed

+270
-59
lines changed

lib/marked.esm.js

Lines changed: 143 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -787,25 +787,49 @@ var Tokenizer_1 = class Tokenizer {
787787
}
788788
}
789789

790-
strong(src) {
791-
const cap = this.rules.inline.strong.exec(src);
792-
if (cap) {
793-
return {
794-
type: 'strong',
795-
raw: cap[0],
796-
text: cap[4] || cap[3] || cap[2] || cap[1]
797-
};
790+
strong(src, maskedSrc, prevChar = '') {
791+
let match = this.rules.inline.strong.start.exec(src);
792+
793+
if (match && (!match[1] || (match[1] && (prevChar === '' || this.rules.inline.punctuation.exec(prevChar))))) {
794+
maskedSrc = maskedSrc.slice(-1 * src.length);
795+
const endReg = match[0] === '**' ? this.rules.inline.strong.endAst : this.rules.inline.strong.endUnd;
796+
797+
endReg.lastIndex = 0;
798+
799+
let cap;
800+
while ((match = endReg.exec(maskedSrc)) != null) {
801+
cap = this.rules.inline.strong.middle.exec(maskedSrc.slice(0, match.index + 3));
802+
if (cap) {
803+
return {
804+
type: 'strong',
805+
raw: src.slice(0, cap[0].length),
806+
text: src.slice(2, cap[0].length - 2)
807+
};
808+
}
809+
}
798810
}
799811
}
800812

801-
em(src) {
802-
const cap = this.rules.inline.em.exec(src);
803-
if (cap) {
804-
return {
805-
type: 'em',
806-
raw: cap[0],
807-
text: cap[6] || cap[5] || cap[4] || cap[3] || cap[2] || cap[1]
808-
};
813+
em(src, maskedSrc, prevChar = '') {
814+
let match = this.rules.inline.em.start.exec(src);
815+
816+
if (match && (!match[1] || (match[1] && (prevChar === '' || this.rules.inline.punctuation.exec(prevChar))))) {
817+
maskedSrc = maskedSrc.slice(-1 * src.length);
818+
const endReg = match[0] === '*' ? this.rules.inline.em.endAst : this.rules.inline.em.endUnd;
819+
820+
endReg.lastIndex = 0;
821+
822+
let cap;
823+
while ((match = endReg.exec(maskedSrc)) != null) {
824+
cap = this.rules.inline.em.middle.exec(maskedSrc.slice(0, match.index + 2));
825+
if (cap) {
826+
return {
827+
type: 'em',
828+
raw: src.slice(0, cap[0].length),
829+
text: src.slice(1, cap[0].length - 1)
830+
};
831+
}
832+
}
809833
}
810834
}
811835

@@ -1101,19 +1125,74 @@ const inline = {
11011125
link: /^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/,
11021126
reflink: /^!?\[(label)\]\[(?!\s*\])((?:\\[\[\]]?|[^\[\]\\])+)\]/,
11031127
nolink: /^!?\[(?!\s*\])((?:\[[^\[\]]*\]|\\[\[\]]|[^\[\]])*)\](?:\[\])?/,
1104-
strong: /^__([^\s_])__(?!_)|^\*\*([^\s*])\*\*(?!\*)|^__([^\s][\s\S]*?[^\s])__(?!_)|^\*\*([^\s][\s\S]*?[^\s])\*\*(?!\*)/,
1105-
em: /^_([^\s_])_(?!_)|^_([^\s_<][\s\S]*?[^\s_])_(?!_|[^\s,punctuation])|^_([^\s_<][\s\S]*?[^\s])_(?!_|[^\s,punctuation])|^\*([^\s*<\[])\*(?!\*)|^\*([^\s<"][\s\S]*?[^\s\[\*])\*(?![\]`punctuation])|^\*([^\s*"<\[][\s\S]*[^\s])\*(?!\*)/,
1128+
reflinkSearch: 'reflink|nolink(?!\\()',
1129+
strong: {
1130+
start: /^(?:(\*\*(?=[*punctuation]))|\*\*)(?![\s])|__/, // (1) returns if starts w/ punctuation
1131+
middle: /^\*\*(?:(?:(?!overlapSkip)(?:[^*]|\\\*)|overlapSkip)|\*(?:(?!overlapSkip)(?:[^*]|\\\*)|overlapSkip)*?\*)+?\*\*$|^__(?![\s])((?:(?:(?!overlapSkip)(?:[^_]|\\_)|overlapSkip)|_(?:(?!overlapSkip)(?:[^_]|\\_)|overlapSkip)*?_)+?)__$/,
1132+
endAst: /[^punctuation\s]\*\*(?!\*)|[punctuation]\*\*(?!\*)(?:(?=[punctuation\s]|$))/, // last char can't be punct, or final * must also be followed by punct (or endline)
1133+
endUnd: /[^\s]__(?!_)(?:(?=[punctuation\s])|$)/ // last char can't be a space, and final _ must preceed punct or \s (or endline)
1134+
},
1135+
em: {
1136+
start: /^(?:(\*(?=[punctuation]))|\*)(?![*\s])|_/, // (1) returns if starts w/ punctuation
1137+
middle: /^\*(?:(?:(?!overlapSkip)(?:[^*]|\\\*)|overlapSkip)|\*(?:(?!overlapSkip)(?:[^*]|\\\*)|overlapSkip)*?\*)+?\*$|^_(?![_\s])(?:(?:(?!overlapSkip)(?:[^_]|\\_)|overlapSkip)|_(?:(?!overlapSkip)(?:[^_]|\\_)|overlapSkip)*?_)+?_$/,
1138+
endAst: /[^punctuation\s]\*(?!\*)|[punctuation]\*(?!\*)(?:(?=[punctuation\s]|$))/, // last char can't be punct, or final * must also be followed by punct (or endline)
1139+
endUnd: /[^\s]_(?!_)(?:(?=[punctuation\s])|$)/ // last char can't be a space, and final _ must preceed punct or \s (or endline)
1140+
},
11061141
code: /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,
11071142
br: /^( {2,}|\\)\n(?!\s*$)/,
11081143
del: noopTest$1,
1109-
text: /^(`+|[^`])(?:[\s\S]*?(?:(?=[\\<!\[`*]|\b_|$)|[^ ](?= {2,}\n))|(?= {2,}\n))/
1144+
text: /^(`+|[^`])(?:[\s\S]*?(?:(?=[\\<!\[`*]|\b_|$)|[^ ](?= {2,}\n))|(?= {2,}\n))/,
1145+
punctuation: /^([\s*punctuation])/
11101146
};
11111147

11121148
// list of punctuation marks from common mark spec
1113-
// without ` and ] to workaround Rule 17 (inline code blocks/links)
1114-
// without , to work around example 393
1115-
inline._punctuation = '!"#$%&\'()*+\\-./:;<=>?@\\[^_{|}~';
1116-
inline.em = edit$1(inline.em).replace(/punctuation/g, inline._punctuation).getRegex();
1149+
// without * and _ to workaround cases with double emphasis
1150+
inline._punctuation = '!"#$%&\'()+\\-.,/:;<=>?@\\[\\]`^{|}~';
1151+
inline.punctuation = edit$1(inline.punctuation).replace(/punctuation/g, inline._punctuation).getRegex();
1152+
1153+
// sequences em should skip over [title](link), `code`, <html>
1154+
inline._blockSkip = '\\[[^\\]]*?\\]\\([^\\)]*?\\)|`[^`]*?`|<[^>]*?>';
1155+
inline._overlapSkip = '__[^_]*?__|\\*\\*\\[^\\*\\]*?\\*\\*';
1156+
1157+
inline.em.start = edit$1(inline.em.start)
1158+
.replace(/punctuation/g, inline._punctuation)
1159+
.getRegex();
1160+
1161+
inline.em.middle = edit$1(inline.em.middle)
1162+
.replace(/punctuation/g, inline._punctuation)
1163+
.replace(/overlapSkip/g, inline._overlapSkip)
1164+
.getRegex();
1165+
1166+
inline.em.endAst = edit$1(inline.em.endAst, 'g')
1167+
.replace(/punctuation/g, inline._punctuation)
1168+
.getRegex();
1169+
1170+
inline.em.endUnd = edit$1(inline.em.endUnd, 'g')
1171+
.replace(/punctuation/g, inline._punctuation)
1172+
.getRegex();
1173+
1174+
inline.strong.start = edit$1(inline.strong.start)
1175+
.replace(/punctuation/g, inline._punctuation)
1176+
.getRegex();
1177+
1178+
inline.strong.middle = edit$1(inline.strong.middle)
1179+
.replace(/punctuation/g, inline._punctuation)
1180+
.replace(/blockSkip/g, inline._blockSkip)
1181+
.getRegex();
1182+
1183+
inline.strong.endAst = edit$1(inline.strong.endAst, 'g')
1184+
.replace(/punctuation/g, inline._punctuation)
1185+
.getRegex();
1186+
1187+
inline.strong.endUnd = edit$1(inline.strong.endUnd, 'g')
1188+
.replace(/punctuation/g, inline._punctuation)
1189+
.getRegex();
1190+
1191+
inline.blockSkip = edit$1(inline._blockSkip, 'g')
1192+
.getRegex();
1193+
1194+
inline.overlapSkip = edit$1(inline._overlapSkip, 'g')
1195+
.getRegex();
11171196

11181197
inline._escapes = /\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/g;
11191198

@@ -1145,6 +1224,11 @@ inline.reflink = edit$1(inline.reflink)
11451224
.replace('label', inline._label)
11461225
.getRegex();
11471226

1227+
inline.reflinkSearch = edit$1(inline.reflinkSearch, 'g')
1228+
.replace('reflink', inline.reflink)
1229+
.replace('nolink', inline.nolink)
1230+
.getRegex();
1231+
11481232
/**
11491233
* Normal Inline Grammar
11501234
*/
@@ -1156,8 +1240,18 @@ inline.normal = merge$1({}, inline);
11561240
*/
11571241

11581242
inline.pedantic = merge$1({}, inline.normal, {
1159-
strong: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
1160-
em: /^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/,
1243+
strong: {
1244+
start: /^__|\*\*/,
1245+
middle: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
1246+
endAst: /\*\*(?!\*)/g,
1247+
endUnd: /__(?!_)/g
1248+
},
1249+
em: {
1250+
start: /^_|\*/,
1251+
middle: /^()\*(?=\S)([\s\S]*?\S)\*(?!\*)|^_(?=\S)([\s\S]*?\S)_(?!_)/,
1252+
endAst: /\*(?!\*)/g,
1253+
endUnd: /_(?!_)/g
1254+
},
11611255
link: edit$1(/^!?\[(label)\]\((.*?)\)/)
11621256
.replace('label', inline._label)
11631257
.getRegex(),
@@ -1516,9 +1610,29 @@ var Lexer_1 = class Lexer {
15161610
/**
15171611
* Lexing/Compiling
15181612
*/
1519-
inlineTokens(src, tokens = [], inLink = false, inRawBlock = false) {
1613+
inlineTokens(src, tokens = [], inLink = false, inRawBlock = false, prevChar = '') {
15201614
let token;
15211615

1616+
// String with links masked to avoid interference with em and strong
1617+
let maskedSrc = src;
1618+
let match;
1619+
1620+
// Mask out reflinks
1621+
if (this.tokens.links) {
1622+
const links = Object.keys(this.tokens.links);
1623+
if (links.length > 0) {
1624+
while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
1625+
if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
1626+
maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
1627+
}
1628+
}
1629+
}
1630+
}
1631+
// Mask out other blocks
1632+
while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
1633+
maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
1634+
}
1635+
15221636
while (src) {
15231637
// escape
15241638
if (token = this.tokenizer.escape(src)) {
@@ -1557,15 +1671,15 @@ var Lexer_1 = class Lexer {
15571671
}
15581672

15591673
// strong
1560-
if (token = this.tokenizer.strong(src)) {
1674+
if (token = this.tokenizer.strong(src, maskedSrc, prevChar)) {
15611675
src = src.substring(token.raw.length);
15621676
token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
15631677
tokens.push(token);
15641678
continue;
15651679
}
15661680

15671681
// em
1568-
if (token = this.tokenizer.em(src)) {
1682+
if (token = this.tokenizer.em(src, maskedSrc, prevChar)) {
15691683
src = src.substring(token.raw.length);
15701684
token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
15711685
tokens.push(token);
@@ -1611,6 +1725,7 @@ var Lexer_1 = class Lexer {
16111725
// text
16121726
if (token = this.tokenizer.inlineText(src, inRawBlock, smartypants)) {
16131727
src = src.substring(token.raw.length);
1728+
prevChar = token.raw.slice(-1);
16141729
tokens.push(token);
16151730
continue;
16161731
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy