Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/late-ants-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cherry-markdown": minor
---

refactor: 重构链接属性配置类型和工具栏选项。现有使用 `hiddenToolbar` 的代码仍然可以正常工作(向后兼容),但 IDE 会显示废弃警告,建议迁移到 `registerHeadlessToolbars`。
5 changes: 3 additions & 2 deletions packages/cherry-markdown/src/Cherry.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ const defaultConfig = {
* @deprecated 不再支持theme的配置,统一在`themeSettings.toolbarTheme`中配置
*/
// theme: 'dark', // light or dark
showToolbar: true, // false:不展示顶部工具栏; true:展示工具栏; toolbars.showToolbar=false 与 toolbars.toolbar=false 等效
showToolbar: true, // false:不展示顶部工具栏和侧边栏; true:展示工具栏; toolbars.showToolbar=false 与 toolbars.toolbar=false 等效
toolbar: [
'bold',
'italic',
Expand Down Expand Up @@ -690,7 +690,8 @@ const defaultConfig = {
sidebar: false,
bubble: ['bold', 'italic', 'underline', 'strikethrough', 'sub', 'sup', 'quote', '|', 'size', 'color'], // array or false
float: ['h1', 'h2', 'h3', '|', 'checklist', 'quote', 'table', 'code'], // array or false
hiddenToolbar: [], // 不展示在编辑器中的工具栏,只使用工具栏的api和快捷键功能
hiddenToolbar: [], // @deprecated 请使用 registerHeadlessToolbars 代替
registerHeadlessToolbars: [], // 不展示在编辑器中的工具栏,只使用工具栏的 api 和快捷键功能
toc: false, // 不展示悬浮目录
// toc: {
// updateLocationHash: false, // 要不要更新URL的hash
Expand Down
9 changes: 6 additions & 3 deletions packages/cherry-markdown/src/Cherry.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,11 +704,14 @@ export default class Cherry extends CherryStatic {
}

createHiddenToolbar() {
if (this.options.toolbars.hiddenToolbar) {
$expectTarget(this.options.toolbars.hiddenToolbar, Array);
// registerHeadlessToolbars 优先级高于 hiddenToolbar
const headlessToolbars =
this.options.toolbars.registerHeadlessToolbars || this.options.toolbars.hiddenToolbar;
if (headlessToolbars) {
$expectTarget(headlessToolbars, Array);
this.hiddenToolbar = new HiddenToolbar({
$cherry: this,
buttonConfig: this.options.toolbars.hiddenToolbar,
buttonConfig: headlessToolbars,
customMenu: this.options.toolbars.customMenu,
});
this.toolbar.collectMenuInfo(this.hiddenToolbar);
Expand Down
4 changes: 4 additions & 0 deletions packages/cherry-markdown/src/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export default class Event {
this.emitter.all.clear();
}

/**
* 根据配置项绑定事件回调
* @param {import('~types/cherry').CherryOptions} options 配置项
*/
bindCallbacksByOptions(options) {
if (options.callback.afterChange) {
this.on(this.Events.afterChange, (msg) => {
Expand Down
28 changes: 25 additions & 3 deletions packages/cherry-markdown/src/core/hooks/AutoLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,31 @@ export default class AutoLink extends SyntaxBase {
super({ config });
this.enableShortLink = !!config.enableShortLink;
this.shortLinkLength = config.shortLinkLength;
// eslint-disable-next-line no-nested-ternary
this.target = config.target ? `target="${config.target}"` : !!config.openNewPage ? 'target="_blank"' : '';
this.rel = config.rel ? `rel="${config.rel}"` : '';
const validTarget = ['_blank', '_parent', '_self', '_top'];
if (config.target && validTarget.includes(config.target)) {
this.target = `target="${config.target}"`;
} else {
this.target = config.openNewPage ? 'target="_blank"' : '';
}
// HTML标准 <a> 标签的 rel 属性有效值
const validRel = [
'alternate',
'author',
'bookmark',
'external',
'help',
'license',
'next',
'nofollow',
'noopener',
'noreferrer',
'opener',
'prev',
'privacy-policy',
'search',
'tag',
];
this.rel = validRel.includes(config.rel) ? `rel="${config.rel}"` : '';
}

/**
Expand Down
28 changes: 25 additions & 3 deletions packages/cherry-markdown/src/core/hooks/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,31 @@ export default class Link extends SyntaxBase {

constructor({ config, globalConfig }) {
super({ config });
// eslint-disable-next-line no-nested-ternary
this.target = config.target ? `target="${config.target}"` : !!config.openNewPage ? 'target="_blank"' : '';
this.rel = config.rel ? `rel="${config.rel}"` : '';
const validTarget = ['_blank', '_parent', '_self', '_top'];
if (config.target && validTarget.includes(config.target)) {
this.target = `target="${config.target}"`;
} else {
this.target = config.openNewPage ? 'target="_blank"' : '';
}
// HTML标准 <a> 标签的 rel 属性有效值
const validRel = [
'alternate',
'author',
'bookmark',
'external',
'help',
'license',
'next',
'nofollow',
'noopener',
'noreferrer',
'opener',
'prev',
'privacy-policy',
'search',
'tag',
];
this.rel = validRel.includes(config.rel) ? `rel="${config.rel}"` : '';
}

/**
Expand Down
Loading
Loading