diff --git a/lib/util/replaceContentBlockReference.js b/lib/util/replaceContentBlockReference.js index cc3441212..04d12bfcf 100644 --- a/lib/util/replaceContentBlockReference.js +++ b/lib/util/replaceContentBlockReference.js @@ -38,88 +38,69 @@ export const ReplaceContentBlockReference = { * @returns {string} replaced string */ replaceContentBlockReference(str, from, to) { - const regexById_double = /ContentBlockById\(\s*"([0-9]+)"\s*\)/gim; - const regexByKey_double = /ContentBlockByKey\(\s*"([a-z0-9-/._]+)"\s*\)/gim; - const regexByName_double = /ContentBlockByName\(\s*"([ a-z0-9-\\._]+)"\s*\)/gim; - - const regexById_single = /ContentBlockById\(\s*'([0-9]+)'\s*\)/gim; - const regexByKey_single = /ContentBlockByKey\(\s*'([a-z0-9-/._]+)'\s*\)/gim; - const regexByName_single = /ContentBlockByName\(\s*'([ a-z0-9-\\._]+)'\s*\)/gim; - - switch (from) { - case 'id': { - const doubleReplaced = str.replaceAll(regexById_double, (match, id) => { - const asset = this._getAssetById(id); - return this._replaceWith(asset, to); - }); - return doubleReplaced.replaceAll(regexById_single, (match, id) => { - const asset = this._getAssetById(id); - return this._replaceWith(asset, to); - }); - } - case 'key': { - const doubleReplaced = str.replaceAll(regexByKey_double, (match, key) => { - const asset = this._getAssetByKey(key); - return this._replaceWith(asset, to); - }); - return doubleReplaced.replaceAll(regexByKey_single, (match, key) => { - const asset = this._getAssetByKey(key); - return this._replaceWith(asset, to); - }); - } - case 'name': { - const doubleReplaced = str.replaceAll(regexByName_double, (match, pathName) => { - const asset = this._getAssetByPathName(pathName); - return this._replaceWith(asset, to); - }); - return doubleReplaced.replaceAll(regexByName_single, (match, pathName) => { - const asset = this._getAssetByPathName(pathName); - return this._replaceWith(asset, to); - }); - } - } - }, - /** - * - * @private - * @param {string} pathName r__folder_Path +'/'+ name - * @returns {{id: number, key: string, pathName: string}} asset object - */ - _getAssetByPathName(pathName) { - // TODO - return { id: 9999, key: 'my-key', pathName: pathName }; - }, - /** - * - * @private - * @param {number} id id of asset - * @returns {{id: number, key: string, pathName: string}} asset object - */ - _getAssetById(id) { - // TODO - return { - id: id, - key: 'my-key', - pathName: `Content Builder\\Release 2 - BUILD\\Content Blocks Library\\NEW\\03. Header and banner\\Referred contentblock\\RS_Dev_Header_blue`, + const regexBy = { + id: [ + /ContentBlockById\(\s*"([0-9]+)"\s*\)/gim, + /ContentBlockById\(\s*'([0-9]+)'\s*\)/gim, + ], + key: [ + /ContentBlockByKey\(\s*"([a-z0-9-/._]+)"\s*\)/gim, + /ContentBlockByKey\(\s*'([a-z0-9-/._]+)'\s*\)/gim, + ], + name: [ + /ContentBlockByName\(\s*"([ a-z0-9-\\._]+)"\s*\)/gim, + /ContentBlockByName\(\s*'([ a-z0-9-\\._]+)'\s*\)/gim, + ], }; + let result = str; + for (const regex of regexBy[from]) { + result = result.replaceAll(regex, (match, identifier) => { + const asset = this.getAssetBy[from](identifier); + return this._replaceWith(asset, to); + }); + } + return result; }, - /** - * - * @private - * @param {string} key customerKey field of asset - * @returns {{id: number, key: string, pathName: string}} asset object - */ - _getAssetByKey(key) { - // TODO - return { - id: 9999, - key: key, - pathName: `Content Builder\\Release 2 - BUILD\\Content Blocks Library\\NEW\\03. Header and banner\\Referred contentblock\\RS_Dev_Header_blue`, - }; + _getAssetBy: { + /** + * + * @private + * @param {string} pathName r__folder_Path +'/'+ name + * @returns {{id: number, key: string, pathName: string}} asset object + */ + name(pathName) { + // TODO + return { id: 9999, key: 'my-key', pathName: pathName }; + }, + /** + * + * @private + * @param {number} id id of asset + * @returns {{id: number, key: string, pathName: string}} asset object + */ + id(id) { + // TODO + return { + id: id, + key: 'my-key', + pathName: `Content Builder\\Release 2 - BUILD\\Content Blocks Library\\NEW\\03. Header and banner\\Referred contentblock\\RS_Dev_Header_blue`, + }; + }, + /** + * + * @private + * @param {string} key customerKey field of asset + * @returns {{id: number, key: string, pathName: string}} asset object + */ + key(key) { + // TODO + return { + id: 9999, + key: key, + pathName: `Content Builder\\Release 2 - BUILD\\Content Blocks Library\\NEW\\03. Header and banner\\Referred contentblock\\RS_Dev_Header_blue`, + }; + }, }, - - /* const fixedString = testStringId.replaceAll(regexById, replaceIdWithName); - */ /** * * @private