From b09f2319f0239763d7f8670dba7dea50c23c9ca7 Mon Sep 17 00:00:00 2001 From: wibus-wee <1596355173@qq.com> Date: Thu, 14 Apr 2022 19:31:29 +0800 Subject: [PATCH] feat: text macros --- dump.rdb | Bin 0 -> 555 bytes src/modules/markdown/markdown.controller.ts | 7 +- src/modules/markdown/markdown.service.ts | 2 +- src/modules/post/post.controller.ts | 2 + src/utils/macros.util.ts | 71 ++++++++++++++++++++ 5 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 dump.rdb create mode 100644 src/utils/macros.util.ts diff --git a/dump.rdb b/dump.rdb new file mode 100644 index 0000000000000000000000000000000000000000..8ef3b642510c6786bb1273fdf978a60e35b18bae GIT binary patch literal 555 zcmXBPO-K|`902g=e)w%#QB(xZzCthV&SJ4{8^mm7Bt=CM*&+1J%zww1^WHq(ylGcN zQ7JHsN-7Ut+PVc19z56~5 z&M5{`wzBOSMX#H4y0>bj1>JFMkB&nCPr*Vd5GHL zKFEKI5bcQ+!7R6Er(=vz_v*+n^0jkXN4@1S23&=8``a<$iVh0GaIQdHAGBjl1xbP1 zmnk0$qg`#T>lO1txZ^mdWMFj1O=67&B8(i10w<>NEkvD-)D8xLNNY4~KEB54iR*YS zCE&s?!HlC!6 zIFyt8`xaf2qRxn~K { + conditionStr[index] = item.replace(/"/g, '') + conditionStr[index] = conditionStr[index].replace(/\s/g, '') + conditionStr[0] = conditionStr[0].replace(/\?/g, '') + conditionStr[conditionStr.length - 1] = conditionStr[ + conditionStr.length - 1 + ].replace(/\?/g, '') + }) + return ifConditionMacro(conditionStr, model) +} +function ifConditionMacro(args: any, data: any) { + let output: any + const condition = args[0].replace('$', '') + // eslint-disable-next-line no-useless-escape + const operator = condition.match(/>|==|<|\!=/g) + const left = condition.split(operator)[0] + const right = condition.split(operator)[1] + const Value = data[left] + switch (operator[0]) { + case '>': + output = Value > right ? args[1] : args[2] + break + case '==': + output = Value == right ? args[1] : args[2] + break + case '<': + output = Value < right ? args[1] : args[2] + break + case '!=': + output = Value != right ? args[1] : args[2] + break + case '&&': + output = Value && right ? args[1] : args[2] + break + case '||': + output = Value || right ? args[1] : args[2] + break + default: + output = args[1] + break + } + return output +} +export function macros(str: any, model: any): any { + if (str.search(/\[\[(.*?)\]\]/g) != -1) { + str = str.replace(/\[\[(.*?)\]\]/g, (match, condition) => { + if (condition.search(Reg['?']) != -1) { + return ifConditionGramar(condition, model) + } + if (condition.search(Reg['$']) != -1) { + const variable = condition.replace(Reg['$'], '$1').replace(/\s/g, '') + return model[variable] + } + // eslint-disable-next-line no-useless-escape + if (condition.search(Reg['#']) != -1) { + // eslint-disable-next-line no-useless-escape + const functions = condition.replace(Reg['#'], '$1').replace(/\s/g, '') + } + }) + } + return str +}