Skip to content

Commit

Permalink
Merge pull request #1 from UziTech/mask-reflinks
Browse files Browse the repository at this point in the history
mask reflinks in a separate function
  • Loading branch information
calculuschild authored Jun 30, 2020
2 parents 2a45677 + d233fd5 commit 56b6f5e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
40 changes: 17 additions & 23 deletions src/Tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ function indentCodeCompensation(raw, text) {
.join('\n');
}

function maskReflinks(text, links) {
if (links) {
links = Object.keys(links).filter(l => l.match(/[*_]/));
if (links.length > 0) {
let match;
while ((match = this.rules.inline.reflinkSearch.exec(text)) != null) {
if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
text = text.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + text.slice(this.rules.inline.reflinkSearch.lastIndex);
}
}
}
}
return text;
}

/**
* Tokenizer
*/
Expand Down Expand Up @@ -493,17 +508,7 @@ module.exports = class Tokenizer {
let cap = this.rules.inline.preStrong.exec(src);

if (cap) {
let text = src;
if (links) {
links = Object.keys(links);
const reg = /(?:\[.*?\]\[.*?\])|(?:\[.*?\](?!\())/g;
let match;
while ((match = reg.exec(text)) != null) {
if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
text = text.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + text.slice(reg.lastIndex);
}
}
}
const text = maskReflinks(src, links);

cap = this.rules.inline.strong.exec(text);

Expand All @@ -523,18 +528,7 @@ module.exports = class Tokenizer {
let cap = this.rules.inline.preEm.exec(src);

if (cap) {
let text = src;

if (links) {
links = Object.keys(links);
const reg = /(?:\[.*?\]\[.*?\])|(?:\[.*?\](?!\())/g;
let match;
while ((match = reg.exec(text)) != null) {
if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
text = text.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + text.slice(reg.lastIndex);
}
}
}
const text = maskReflinks(src, links);

cap = this.rules.inline.em.exec(text);

Expand Down
6 changes: 6 additions & 0 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ const inline = {
link: /^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/,
reflink: /^!?\[(label)\]\[(?!\s*\])((?:\\[\[\]]?|[^\[\]\\])+)\]/,
nolink: /^!?\[(?!\s*\])((?:\[[^\[\]]*\]|\\[\[\]]|[^\[\]])*)\](?:\[\])?/,
reflinkSearch: 'reflink|nolink(?!\\()',
preStrong: /^(?:\*\*|__)/,
strong: /^(?:(\*\*(?=[*punctuation]))|\*\*)(?![\s])((?:(?:(?!emSkip)(?:[^*]|[\\\s]\*)|emSkip)|(?:(?:(?!emSkip)(?:[^*]|[\\\s]\*)|emSkip)*?(?<!\\)\*){2})+?)(?:(?<![punctuation\s])\*\*(?!\*)|(?<=[punctuation])\*\*(?!\*)(?:(?=[punctuation\s]|$)))|^__(?![\s])((?:(?:(?!emSkip)(?:[^_]|[\\\s]_)|emSkip)|(?:(?:(?!emSkip)(?:[^_]|[\\\s]_)|emSkip)*?(?<!\\)_){2})+?)(?:(?<![\s])__(?!_)(?:(?=[punctuation\s])|$))/,
preEm: /^[*_]/,
Expand Down Expand Up @@ -228,6 +229,11 @@ inline.reflink = edit(inline.reflink)
.replace('label', inline._label)
.getRegex();

inline.reflinkSearch = edit(inline.reflinkSearch, 'g')
.replace('reflink', inline.reflink)
.replace('nolink', inline.nolink)
.getRegex();

/**
* Normal Inline Grammar
*/
Expand Down

0 comments on commit 56b6f5e

Please sign in to comment.