From 1d67fc947373abb6a458c064e53c3e2bdc2f7be7 Mon Sep 17 00:00:00 2001 From: Blazej Kustra Date: Mon, 3 Jun 2024 19:42:15 +0200 Subject: [PATCH 1/4] Add static logger to ExpensiMark --- lib/ExpensiMark.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index 3400c46b..6cc78715 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -1,8 +1,8 @@ +/* eslint-disable no-console */ 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'); @@ -10,6 +10,22 @@ const MARKDOWN_IMAGE_REGEX = new RegExp(`\\!(?:\\[([^\\][]*(?:\\[[^\\][]*][^\\][ const SLACK_SPAN_NEW_LINE_TAG = ''; export default class ExpensiMark { + static Log = { + info: console.log, + alert: console.warn, + warn: console.warn, + hmmm: console.log, + client: console.log, + }; + + /** + * 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 @@ -494,7 +510,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'; } @@ -508,7 +524,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'; } @@ -566,7 +582,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'; } @@ -579,7 +595,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]}`; From 99bc4f78e9b36dfb8da8d3f5b4d44e275d401372 Mon Sep 17 00:00:00 2001 From: Blazej Kustra Date: Mon, 3 Jun 2024 19:57:22 +0200 Subject: [PATCH 2/4] Add types for ExpensiMark static properties --- lib/ExpensiMark.d.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/ExpensiMark.d.ts b/lib/ExpensiMark.d.ts index fad8f1b6..a10aa20c 100644 --- a/lib/ExpensiMark.d.ts +++ b/lib/ExpensiMark.d.ts @@ -1,3 +1,5 @@ +import type Logger from './Logger'; + declare type Replacement = (...args: string[], extras?: ExtrasObject) => string; declare type Name = | 'codeFence' @@ -39,6 +41,9 @@ declare type ExtrasObject = { accountIDToName?: Record; }; export default class ExpensiMark { + static Log: Logger; + static setLogger(logger: Logger): void; + rules: Rule[]; htmlToMarkdownRules: Rule[]; htmlToTextRules: Rule[]; From 3c39c6f0c0903f50fe8fbd03967982ac08d45c4c Mon Sep 17 00:00:00 2001 From: Blazej Kustra Date: Mon, 3 Jun 2024 20:07:56 +0200 Subject: [PATCH 3/4] Use logger as default --- lib/ExpensiMark.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index 6cc78715..aae58310 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -1,8 +1,8 @@ -/* eslint-disable no-console */ import * as _ from 'underscore'; import Str from './str'; import * as Constants from './CONST'; import * as UrlPatterns from './Url'; +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'); @@ -10,13 +10,11 @@ const MARKDOWN_IMAGE_REGEX = new RegExp(`\\!(?:\\[([^\\][]*(?:\\[[^\\][]*][^\\][ const SLACK_SPAN_NEW_LINE_TAG = ''; export default class ExpensiMark { - static Log = { - info: console.log, - alert: console.warn, - warn: console.warn, - hmmm: console.log, - client: console.log, - }; + static Log = new Logger({ + serverLoggingCallback: _.noop, + clientLoggingCallback: (message) => console.warn(message), + isDebug: true, + }); /** * Set the logger to use for logging inside of the ExpensiMark class From 901bb35b45f62964608ba6dcaf6e1ebf8dda33f5 Mon Sep 17 00:00:00 2001 From: Blazej Kustra Date: Mon, 3 Jun 2024 20:09:11 +0200 Subject: [PATCH 4/4] Disable console.warn --- lib/ExpensiMark.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index aae58310..d5352318 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -12,6 +12,7 @@ const SLACK_SPAN_NEW_LINE_TAG = ' console.warn(message), isDebug: true, });