diff --git a/dump.rdb b/dump.rdb new file mode 100644 index 00000000..8ef3b642 Binary files /dev/null and b/dump.rdb differ diff --git a/src/modules/markdown/markdown.controller.ts b/src/modules/markdown/markdown.controller.ts index d133a1d7..588a9611 100644 --- a/src/modules/markdown/markdown.controller.ts +++ b/src/modules/markdown/markdown.controller.ts @@ -30,6 +30,7 @@ import { IsMaster } from '~/common/decorator/role.decorator' import { ArticleTypeEnum } from '~/constants/article.constant' import { MongoIdDto } from '~/shared/dto/id.dto' import { getShortDateTime } from '~/utils' +import { macros } from '~/utils/macros.util' import { CategoryModel } from '../category/category.model' import { ConfigsService } from '../configs/configs.service' @@ -210,9 +211,11 @@ export class MarkdownController { })() const url = new URL(relativePath!, webUrl) - + const markdownMacros = this.service.renderMarkdownContent( + macros(markdown, document), + ) const structure = await this.service.getRenderedMarkdownHtmlStructure( - markdown, + markdownMacros, document.title, theme, ) diff --git a/src/modules/markdown/markdown.service.ts b/src/modules/markdown/markdown.service.ts index 9dbe80d9..cb3ae0ef 100644 --- a/src/modules/markdown/markdown.service.ts +++ b/src/modules/markdown/markdown.service.ts @@ -223,7 +223,7 @@ ${text.trim()} } return { - html: this.renderMarkdownContent(doc.document.text), + html: doc.document.text, ...doc, document: doc.document, } diff --git a/src/modules/post/post.controller.ts b/src/modules/post/post.controller.ts index c507ba10..34279adf 100644 --- a/src/modules/post/post.controller.ts +++ b/src/modules/post/post.controller.ts @@ -26,6 +26,7 @@ import { CannotFindException } from '~/common/exceptions/cant-find.exception' import { CountingService } from '~/processors/helper/helper.counting.service' import { MongoIdDto } from '~/shared/dto/id.dto' import { addYearCondition } from '~/transformers/db-query.transformer' +import { macros } from '~/utils/macros.util' import { CategoryAndSlugDto, PostQueryDto } from './post.dto' import { PartialPostModel, PostModel } from './post.model' @@ -65,6 +66,7 @@ export class PostController { if (!doc) { throw new CannotFindException() } + doc.text = macros(doc.text, doc) return doc } diff --git a/src/utils/macros.util.ts b/src/utils/macros.util.ts new file mode 100644 index 00000000..64c70d68 --- /dev/null +++ b/src/utils/macros.util.ts @@ -0,0 +1,71 @@ +/* eslint-disable no-useless-escape */ +const Reg = { + '#': /\#(.*?)/g, + $: /\$(.*?)/g, + '?': /\?\??(.*?)\??\?/g, +} + +function ifConditionGramar(condition: string, model: any) { + const conditionStr = condition.split('|') + conditionStr.forEach((item: string, index: string | number) => { + 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 +}