Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static logger to ExpensiMark #712

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading