feat: text macros
This commit is contained in:
@@ -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,
|
||||
)
|
||||
|
||||
@@ -223,7 +223,7 @@ ${text.trim()}
|
||||
}
|
||||
|
||||
return {
|
||||
html: this.renderMarkdownContent(doc.document.text),
|
||||
html: doc.document.text,
|
||||
...doc,
|
||||
document: doc.document,
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
71
src/utils/macros.util.ts
Normal file
71
src/utils/macros.util.ts
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user