Skip to content

Commit

Permalink
Merge pull request #696 from software-mansion-labs/war-in/fix-missing…
Browse files Browse the repository at this point in the history
…-extras-default

Add missing default value to `extras`
  • Loading branch information
iwiznia authored May 14, 2024
2 parents 923dd7f + dc3276e commit 18fa764
Show file tree
Hide file tree
Showing 5 changed files with 609 additions and 35 deletions.
8 changes: 8 additions & 0 deletions __tests__/ExpensiMark-HTMLToText-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ test('Mention user html to text', () => {
testString = '<mention-user>@[email protected]</mention-user>';
expect(parser.htmlToText(testString)).toBe('@[email protected]');

// When there is `accountID` and no `extras`, `@Hidden` should be returned
testString = '<mention-user accountID="1234"/>';
expect(parser.htmlToText(testString)).toBe('@Hidden');

const extras = {
accountIdToName: {
'1234': '[email protected]',
Expand All @@ -171,6 +175,10 @@ test('Mention report html to text', () => {
testString = '<mention-report>#room-NAME</mention-report>';
expect(parser.htmlToText(testString)).toBe('#room-NAME');

// When there is `reportID` and no `extras`, `#Hidden` should be returned
testString = '<mention-report reportID="1234"/>';
expect(parser.htmlToText(testString)).toBe('#Hidden');

const extras = {
reportIdToName: {
'1234': '#room-name',
Expand Down
8 changes: 8 additions & 0 deletions __tests__/ExpensiMark-Markdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,10 @@ test('Mention user html to markdown', () => {
testString = '<mention-user>@[email protected]</mention-user>';
expect(parser.htmlToMarkdown(testString)).toBe('@[email protected]');

// When there is `accountID` and no `extras`, `@Hidden` should be returned
testString = '<mention-user accountID="1234"/>';
expect(parser.htmlToMarkdown(testString)).toBe('@Hidden');

const extras = {
accountIdToName: {
'1234': '[email protected]',
Expand All @@ -781,6 +785,10 @@ test('Mention report html to markdown', () => {
testString = '<mention-report>#room-NAME</mention-report>';
expect(parser.htmlToMarkdown(testString)).toBe('#room-NAME');

// When there is `reportID` and no `extras`, `#Hidden` should be returned
testString = '<mention-report reportID="1234"/>';
expect(parser.htmlToText(testString)).toBe('#Hidden');

const extras = {
reportIdToName: {
'1234': '#room-name',
Expand Down
15 changes: 10 additions & 5 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as _ from 'underscore';
import Str from './str';
import * as Constants from './CONST';
import * as UrlPatterns from './Url';
import Log from './Log';

const MARKDOWN_LINK_REGEX = new RegExp(`\\[([^\\][]*(?:\\[[^\\][]*][^\\][]*)*)]\\(${UrlPatterns.MARKDOWN_URL_REGEX}\\)(?![^<]*(<\\/pre>|<\\/code>))`, 'gi');
const MARKDOWN_IMAGE_REGEX = new RegExp(`\\!(?:\\[([^\\][]*(?:\\[[^\\][]*][^\\][]*)*)])?\\(${UrlPatterns.MARKDOWN_URL_REGEX}\\)(?![^<]*(<\\/pre>|<\\/code>))`, 'gi');
Expand Down Expand Up @@ -473,7 +474,8 @@ export default class ExpensiMark {
replacement: (match, g1, offset, string, extras) => {
const reportToNameMap = extras.reportIdToName;
if (!reportToNameMap || !reportToNameMap[g1]) {
return '';
Log.alert('[ExpensiMark] Missing report name', {reportID: g1});
return '#Hidden';
}

return reportToNameMap[g1];
Expand All @@ -485,7 +487,8 @@ export default class ExpensiMark {
replacement: (match, g1, offset, string, extras) => {
const accountToNameMap = extras.accountIdToName;
if (!accountToNameMap || !accountToNameMap[g1]) {
return '';
Log.alert('[ExpensiMark] Missing account name', {accountID: g1});
return '@Hidden';
}

return `@${extras.accountIdToName[g1]}`;
Expand Down Expand Up @@ -540,7 +543,8 @@ export default class ExpensiMark {
replacement: (match, g1, offset, string, extras) => {
const reportToNameMap = extras.reportIdToName;
if (!reportToNameMap || !reportToNameMap[g1]) {
return '';
Log.alert('[ExpensiMark] Missing report name', {reportID: g1});
return '#Hidden';
}

return reportToNameMap[g1];
Expand All @@ -552,7 +556,8 @@ export default class ExpensiMark {
replacement: (match, g1, offset, string, extras) => {
const accountToNameMap = extras.accountIdToName;
if (!accountToNameMap || !accountToNameMap[g1]) {
return '';
Log.alert('[ExpensiMark] Missing account name', {accountID: g1});
return '@Hidden';
}

return `@${extras.accountIdToName[g1]}`;
Expand Down Expand Up @@ -895,7 +900,7 @@ export default class ExpensiMark {
*
* @returns {String}
*/
htmlToText(htmlString, extras) {
htmlToText(htmlString, extras = {}) {
let replacedText = htmlString;
const processRule = (rule) => {
// if replacement is a function, we want to pass optional extras to it
Expand Down
Loading

0 comments on commit 18fa764

Please sign in to comment.