Skip to content

Commit

Permalink
Merge pull request Expensify#712 from blazejkustra/fix/add-logger-to-…
Browse files Browse the repository at this point in the history
…expensimark
  • Loading branch information
francoisl authored Jun 4, 2024
2 parents 5002fbf + 901bb35 commit 43d1bf3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions lib/ExpensiMark.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type Logger from './Logger';

declare type Replacement = (...args: string[], extras?: ExtrasObject) => string;
declare type Name =
| 'codeFence'
Expand Down Expand Up @@ -39,6 +41,9 @@ declare type ExtrasObject = {
accountIDToName?: Record<string, string>;
};
export default class ExpensiMark {
static Log: Logger;
static setLogger(logger: Logger): void;

rules: Rule[];
htmlToMarkdownRules: Rule[];
htmlToTextRules: Rule[];
Expand Down
25 changes: 20 additions & 5 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@ import * as _ from 'underscore';
import Str from './str';
import * as Constants from './CONST';
import * as UrlPatterns from './Url';
import Log from './Log';
import Logger from './Logger';

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');

const SLACK_SPAN_NEW_LINE_TAG = '<span class="c-mrkdwn__br" data-stringify-type="paragraph-break" style="box-sizing: inherit; display: block; height: unset;"></span>';

export default class ExpensiMark {
static Log = new Logger({
serverLoggingCallback: _.noop,
// eslint-disable-next-line no-console
clientLoggingCallback: (message) => console.warn(message),
isDebug: true,
});

/**
* Set the logger to use for logging inside of the ExpensiMark class
* @param {Object} logger - The logger object to use
*/
static setLogger(logger) {
ExpensiMark.Log = logger;
}

constructor() {
/**
* The list of regex replacements to do on a comment. Check the link regex is first so links are processed
Expand Down Expand Up @@ -494,7 +509,7 @@ export default class ExpensiMark {
replacement: (match, g1, offset, string, extras) => {
const reportToNameMap = extras.reportIDToName;
if (!reportToNameMap || !reportToNameMap[g1]) {
Log.alert('[ExpensiMark] Missing report name', {reportID: g1});
ExpensiMark.Log.alert('[ExpensiMark] Missing report name', {reportID: g1});
return '#Hidden';
}

Expand All @@ -508,7 +523,7 @@ export default class ExpensiMark {
if (g1) {
const accountToNameMap = extras.accountIDToName;
if (!accountToNameMap || !accountToNameMap[g1]) {
Log.alert('[ExpensiMark] Missing account name', {accountID: g1});
ExpensiMark.Log.alert('[ExpensiMark] Missing account name', {accountID: g1});
return '@Hidden';
}

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

Expand All @@ -579,7 +594,7 @@ export default class ExpensiMark {
replacement: (match, g1, offset, string, extras) => {
const accountToNameMap = extras.accountIDToName;
if (!accountToNameMap || !accountToNameMap[g1]) {
Log.alert('[ExpensiMark] Missing account name', {accountID: g1});
ExpensiMark.Log.alert('[ExpensiMark] Missing account name', {accountID: g1});
return '@Hidden';
}
return `@${extras.accountIDToName[g1]}`;
Expand Down

0 comments on commit 43d1bf3

Please sign in to comment.