Skip to content

Commit

Permalink
handle extras object as lats argument of replacement function
Browse files Browse the repository at this point in the history
  • Loading branch information
robertKozik committed Apr 18, 2024
1 parent d8a754d commit 82c6f21
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,14 +460,28 @@ export default class ExpensiMark {
},
{
name: 'roomMention',
regex: /<mention-report reportID="(\d+)" \/>/gi,
replacement: (match, g1, extras) => extras.reportIdToName[g1]
regex: /<mention-report reportID="(\d+)" *\/>/gi,
replacement: (match, g1, offset, string, extras) => {
const reportToNameMap = extras.reportIdToName;
if (!reportToNameMap || !reportToNameMap[g1]) {
return '';
}

return reportToNameMap[g1];
},
},
{
name: 'userMention',
regex: /<mention-user accountID="(\d+)" \/>/gi,
replacement: (match, g1, extras) => extras.accountIdToName[g1]
}
regex: /<mention-user accountID="(\d+)" *\/>/gi,
replacement: (match, g1, offset, string, extras) => {
const accountToNameMap = extras.accountIdToName;
if (!accountToNameMap || !accountToNameMap[g1]) {
return '';
}

return `@${extras.accountIdToName[g1]}`;
},
},
];

/**
Expand Down Expand Up @@ -513,13 +527,27 @@ export default class ExpensiMark {
},
{
name: 'roomMention',
regex: /<mention-report reportID="(\d+)" \/>/gi,
replacement: (match, g1, extras) => extras.reportIdToName[g1]
regex: /<mention-report reportID="(\d+)" *\/>/gi,
replacement: (match, g1, offset, string, extras) => {
const reportToNameMap = extras.reportIdToName;
if (!reportToNameMap || !reportToNameMap[g1]) {
return '';
}

return reportToNameMap[g1];
},
},
{
name: 'userMention',
regex: /<mention-user accountID="(\d+)" \/>/gi,
replacement: (match, g1, extras) => extras.accountIdToName[g1]
regex: /<mention-user accountID="(\d+)" *\/>/gi,
replacement: (match, g1, offset, string, extras) => {
const accountToNameMap = extras.accountIdToName;
if (!accountToNameMap || !accountToNameMap[g1]) {
return '';
}

return `@${extras.accountIdToName[g1]}`;
},
},
{
name: 'stripTag',
Expand Down Expand Up @@ -811,7 +839,10 @@ export default class ExpensiMark {
if (rule.pre) {
generatedMarkdown = rule.pre(generatedMarkdown);
}
generatedMarkdown = generatedMarkdown.replace(rule.regex, typeof rule.replacement === "function" && extras ? (match, g1) => rule.replacement(match, g1, extras) : rule.replacement);

// if replacement is a function, we want to pass optional extras to it
const replacementFunction = typeof rule.replacement === 'function' ? (...args) => rule.replacement(...args, extras) : rule.replacement;
generatedMarkdown = generatedMarkdown.replace(rule.regex, replacementFunction);
});
return Str.htmlDecode(this.replaceBlockElementWithNewLine(generatedMarkdown));
}
Expand All @@ -828,7 +859,8 @@ export default class ExpensiMark {
let replacedText = htmlString;

this.htmlToTextRules.forEach((rule) => {
replacedText = replacedText.replace(rule.regex, typeof rule.replacement === "function" && extras ? (match, g1) => rule.replacement(match, g1, extras) : rule.replacement);
const replacementFunction = typeof rule.replacement === 'function' ? (...args) => rule.replacement(...args, extras) : rule.replacement;
replacedText = replacedText.replace(rule.regex, replacementFunction);
});

// Unescaping because the text is escaped in 'replace' function
Expand Down

0 comments on commit 82c6f21

Please sign in to comment.