Skip to content

Commit 4db32dc

Browse files
committed
Links are masked only once per inline string
1 parent 56b6f5e commit 4db32dc

4 files changed

Lines changed: 27 additions & 25 deletions

File tree

src/Lexer.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,20 @@ module.exports = class Lexer {
322322
inlineTokens(src, tokens = [], inLink = false, inRawBlock = false, prevChar = '') {
323323
let token;
324324

325+
// String with links masked to avoid interference with em and strong
326+
let maskedSrc = src;
327+
if (this.tokens.links) {
328+
const links = Object.keys(this.tokens.links);
329+
if (links.length > 0) {
330+
let match;
331+
while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
332+
if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
333+
maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
334+
}
335+
}
336+
}
337+
}
338+
325339
while (src) {
326340
// escape
327341
if (token = this.tokenizer.escape(src)) {
@@ -360,15 +374,15 @@ module.exports = class Lexer {
360374
}
361375

362376
// strong
363-
if (token = this.tokenizer.strong(src, prevChar, this.tokens.links)) {
377+
if (token = this.tokenizer.strong(src, maskedSrc, prevChar)) {
364378
src = src.substring(token.raw.length);
365379
token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
366380
tokens.push(token);
367381
continue;
368382
}
369383

370384
// em
371-
if (token = this.tokenizer.em(src, prevChar, this.tokens.links)) {
385+
if (token = this.tokenizer.em(src, maskedSrc, prevChar)) {
372386
src = src.substring(token.raw.length);
373387
token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
374388
tokens.push(token);

src/Tokenizer.js

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,6 @@ function indentCodeCompensation(raw, text) {
5858
.join('\n');
5959
}
6060

61-
function maskReflinks(text, links) {
62-
if (links) {
63-
links = Object.keys(links).filter(l => l.match(/[*_]/));
64-
if (links.length > 0) {
65-
let match;
66-
while ((match = this.rules.inline.reflinkSearch.exec(text)) != null) {
67-
if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
68-
text = text.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + text.slice(this.rules.inline.reflinkSearch.lastIndex);
69-
}
70-
}
71-
}
72-
}
73-
return text;
74-
}
75-
7661
/**
7762
* Tokenizer
7863
*/
@@ -504,13 +489,12 @@ module.exports = class Tokenizer {
504489
}
505490
}
506491

507-
strong(src, prevChar = '', links) {
492+
strong(src, maskedSrc, prevChar = '') {
508493
let cap = this.rules.inline.preStrong.exec(src);
509494

510495
if (cap) {
511-
const text = maskReflinks(src, links);
512-
513-
cap = this.rules.inline.strong.exec(text);
496+
maskedSrc = maskedSrc.slice(-1*src.length);
497+
cap = this.rules.inline.strong.exec(maskedSrc);
514498

515499
if (cap) {
516500
if (!cap[1] || (cap[1] && (prevChar === '' || this.rules.inline.punctuation.exec(prevChar)))) {
@@ -524,13 +508,12 @@ module.exports = class Tokenizer {
524508
}
525509
}
526510

527-
em(src, prevChar = '', links) {
511+
em(src, maskedSrc, prevChar = '') {
528512
let cap = this.rules.inline.preEm.exec(src);
529513

530514
if (cap) {
531-
const text = maskReflinks(src, links);
532-
533-
cap = this.rules.inline.em.exec(text);
515+
maskedSrc = maskedSrc.slice(-1*src.length);
516+
cap = this.rules.inline.em.exec(maskedSrc);
534517

535518
if (cap) {
536519
if (!cap[1] || (cap[1] && (prevChar === '' || this.rules.inline.punctuation.exec(prevChar)))) {

test/specs/new/em_and_reflinks.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
<p><em>Hello [not</em>reflink] guys*!</p>
33
<p><em>Hello [not</em>a<em>reflink] guys</em>!</p>
44
<p><em>Hello<a href="theaddress">reflink*bottom</a>guys</em>!</p>
5+
<p><em>Hello<a href="theaddress">reflinknoem</a>guys</em>!</p>

test/specs/new/em_and_reflinks.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@
88

99
*Hello [reflink*bottom] guys*!
1010

11+
*Hello [reflinknoem] guys*!
12+
1113
[reflink*bottom]: theaddress
14+
15+
[reflinknoem]: theaddress

0 commit comments

Comments
 (0)