Skip to content

Commit

Permalink
feat: #724 增加流式会话场景的适配
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsonliu committed Mar 2, 2024
1 parent 340bf41 commit ac98580
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/Cherry.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ const defaultConfig = {
* - 一般编辑权限可控的场景(如api文档系统)可以允许iframe、script等标签
*/
htmlWhiteList: '',
/**
* 适配流式会话的场景,开启后将具备以下特性:
* 1. 代码块自动闭合,相当于强制 `engine.syntax.codeBlock.selfClosing=true`
* 2. 文章末尾的段横线标题语法(`\n-`)失效
*
* 后续如果有新的需求,可提issue反馈
*/
flowSessionContext: true,
},
// 内置语法配置
syntax: {
Expand Down
3 changes: 2 additions & 1 deletion src/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export default class Engine {
this.md5StrMap = {};
this.markdownParams = markdownParams;
this.currentStrMd5 = [];
this.htmlWhiteListAppend = markdownParams.engine.global.htmlWhiteList;
this.globalConfig = markdownParams.engine.global;
this.htmlWhiteListAppend = this.globalConfig.htmlWhiteList;
}

initMath(opts) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/hooks/CodeBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export default class CodeBlock extends ParagraphBase {
beforeMakeHtml(str, sentenceMakeFunc, markdownParams) {
let $str = str;

if (this.selfClosing) {
if (this.selfClosing || this.$engine.globalConfig.flowSessionContext) {
$str = this.$dealUnclosingCode($str);
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/hooks/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ export default class Header extends ParagraphBase {

beforeMakeHtml(str) {
let $str = str;
if (this.$engine.globalConfig.flowSessionContext) {
// 适配流式会话的场景,文章末尾的段横线标题语法(`\n-`)失效
$str = $str.replace(/(\n\s*-{1,})\s*$/, '$1 ');
}
// atx 优先
if (this.test($str, ATX_HEADER)) {
$str = $str.replace(this.RULE[ATX_HEADER].reg, (match, lines, level, text) => {
Expand Down
8 changes: 8 additions & 0 deletions types/cherry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ export interface CherryEngineOptions {
* - 一般编辑权限可控的场景(如api文档系统)可以允许iframe、script等标签
*/
htmlWhiteList?: string;
/**
* 适配流式会话的场景,开启后将具备以下特性:
* 1. 代码块自动闭合,相当于强制 `engine.syntax.codeBlock.selfClosing=true`
* 2. 文章末尾的段横线标题语法(`\n-`)失效
*
* 后续如果有新的需求,可提issue反馈
*/
flowSessionContext?: boolean;
};
/** 内置语法配置 */
syntax?: Record<string, Record<string, any> | false>;
Expand Down

0 comments on commit ac98580

Please sign in to comment.