-
Notifications
You must be signed in to change notification settings - Fork 533
自定义语法
sunsonliu edited this page Oct 19, 2023
·
10 revisions
通过一个例子来了解cherry的自定义语法机制,如下:
定义一个自定义语法:
/**
* 自定义一个语法,识别形如 ***ABC*** 的内容,并将其替换成 <span style="color: red"><strong>ABC</strong></span>
*/
var CustomHookA = Cherry.createSyntaxHook('important', Cherry.constants.HOOKS_TYPE_LIST.SEN, {
makeHtml(str) {
return str.replace(this.RULE.reg, function(whole, m1, m2) {
return `<span style="color: red;"><strong>${m2}</strong></span>`;
});
},
rule(str) {
return { reg: /(\*\*\*)([^\*]+)\1/g };
},
});将这个语法配置到cherry配置中:
new Cherry({
id: 'markdown-container',
value: '## hello world',
fileUpload: myFileUpload,
customSyntax: {
importHook: {
syntaxClass: CustomHookA, // 将自定义语法对象挂载到 importHook.syntaxClass上
force: false, // true: 当cherry自带的语法中也有一个“importHook”时,用自定义的语法覆盖默认语法; false:不覆盖
before: 'fontEmphasis', // 定义该自定义语法的执行顺序,当前例子表明在加粗/斜体语法前执行该自定义语法
},
},
toolbars: {
...
},
});效果如下图:
