From 2e30a1f2122f5e49825718494afc2a108e8c85b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?sunsonliu=28=E5=88=98=E9=98=B3=29?= Date: Tue, 29 Oct 2024 11:18:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20#954=20#940=20=E5=A2=9E=E5=8A=A0frontMa?= =?UTF-8?q?tter=E8=AF=AD=E6=B3=95=EF=BC=8C=E5=B9=B6=E5=9C=A8frontMatter?= =?UTF-8?q?=E8=AF=AD=E6=B3=95=E9=87=8C=E6=94=AF=E6=8C=81=E4=BA=86=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E5=85=A8=E5=B1=80=E5=AD=97=E4=BD=93=E5=A4=A7=E5=B0=8F?= =?UTF-8?q?=E7=9A=84=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/HooksConfig.js | 2 ++ src/core/hooks/FrontMatter.js | 51 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/core/hooks/FrontMatter.js diff --git a/src/core/HooksConfig.js b/src/core/HooksConfig.js index 3bd2c4184..3dcf02973 100644 --- a/src/core/HooksConfig.js +++ b/src/core/HooksConfig.js @@ -49,6 +49,7 @@ import Suggester from './hooks/Suggester'; import Ruby from './hooks/Ruby'; import Panel from './hooks/Panel'; import Detail from './hooks/Detail'; +import FrontMatter from './hooks/FrontMatter'; /** * 引擎各语法的配置 * 主要决定支持哪些语法,以及各语法的执行顺序 @@ -57,6 +58,7 @@ const hooksConfig = [ // 段落级 Hook // 引擎会按当前排序顺序执行beforeMake、makeHtml方法 // 引擎会按当前排序逆序执行afterMake方法 + FrontMatter, CodeBlock, InlineCode, MathBlock, diff --git a/src/core/hooks/FrontMatter.js b/src/core/hooks/FrontMatter.js new file mode 100644 index 000000000..b0055157e --- /dev/null +++ b/src/core/hooks/FrontMatter.js @@ -0,0 +1,51 @@ +/** + * Copyright (C) 2021 THL A29 Limited, a Tencent company. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import ParagraphBase from '@/core/ParagraphBase'; +import { compileRegExp } from '@/utils/regexp'; + +export default class FrontMatter extends ParagraphBase { + static HOOK_NAME = 'frontMatter'; + + constructor(options) { + super({ needCache: true }); + } + + beforeMakeHtml(str) { + return str.replace(this.RULE.reg, (match, content) => { + const lineCount = match.match(/\n/g)?.length ?? 0; + const sign = `fontMatter${lineCount}`; + content.replace(/\n\s*(font-size|fontSize): ([0-9a-zA-Z]+)(\n|$)/, (match, m1, m2) => { + this.$engine.$cherry.previewer.getDom().style.fontSize = m2; + return match; + }); + // 预判下是否为json格式,json格式就去掉换行,yaml格式就把换成换成分号 + const dataContent = /^\s*{/.test(content) ? content.replace(/\n/g, '') : content.replace(/\n/g, ';'); + const html = `

`; + const placeHolder = this.pushCache(html, sign, lineCount); + return `${placeHolder}\n`; + }); + } + + makeHtml(str, sentenceMakeFunc) { + return str; + } + + rule() { + const ret = { begin: '^\\s*-{3,}[^\\n]*\\n', end: '\\n-{3,}[^\\n]*\\n', content: '([\\s\\S]+?)' }; + ret.reg = compileRegExp(ret, 'g', true); + return ret; + } +}