Skip to content

Commit e833c5c

Browse files
authored
refactor(Link): use new builder methods to register marks (#1219)
1 parent 20cb732 commit e833c5c

1 file changed

Lines changed: 42 additions & 45 deletions

File tree

  • packages/editor/src/extensions/markdown/Link/LinkSpecs

packages/editor/src/extensions/markdown/Link/LinkSpecs/index.ts

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {Mark, Node} from 'prosemirror-model';
22

3-
import type {ExtensionAuto} from '../../../../core';
4-
import {markTypeFactory} from '../../../../utils/schema';
3+
import type {ExtensionAuto} from '#core';
4+
import {markTypeFactory} from 'src/utils/schema';
55

66
export const linkMarkName = 'link';
77
export const linkType = markTypeFactory(linkMarkName);
@@ -15,10 +15,10 @@ export enum LinkAttr {
1515
}
1616

1717
export const LinkSpecs: ExtensionAuto = (builder) => {
18-
builder.addMark(
19-
linkMarkName,
20-
() => ({
21-
spec: {
18+
builder
19+
.addMarkSpec(
20+
linkMarkName,
21+
() => ({
2222
attrs: {
2323
[LinkAttr.Href]: {},
2424
[LinkAttr.Title]: {default: null},
@@ -40,48 +40,45 @@ export const LinkSpecs: ExtensionAuto = (builder) => {
4040
toDOM(node) {
4141
return ['a', node.attrs];
4242
},
43+
}),
44+
builder.Priority.High,
45+
)
46+
.addMarkdownTokenParserSpec('link', () => ({
47+
name: linkMarkName,
48+
type: 'mark',
49+
getAttrs: (tok) => ({
50+
href: tok.attrGet('href'),
51+
title: tok.attrGet('title') || null,
52+
}),
53+
}))
54+
.addMarkSerializerSpec(linkMarkName, () => ({
55+
open(state, mark, parent, index) {
56+
// FIXME: Verify and use Node instead of Fragment
57+
state.isAutolink = isPlainURL(mark, parent as any, index, 1);
58+
if (state.isAutolink) {
59+
if (mark.attrs[LinkAttr.RawLink]) return '';
60+
return '<';
61+
}
62+
return '[';
4363
},
44-
toMd: {
45-
open(state, mark, parent, index) {
46-
// FIXME: Verify and use Node instead of Fragment
47-
state.isAutolink = isPlainURL(mark, parent as any, index, 1);
48-
if (state.isAutolink) {
49-
if (mark.attrs[LinkAttr.RawLink]) return '';
50-
return '<';
51-
}
52-
return '[';
53-
},
54-
close(state, mark) {
55-
if (state.isAutolink) {
56-
state.isAutolink = undefined;
57-
if (mark.attrs[LinkAttr.RawLink]) return '';
58-
59-
return '>';
60-
}
64+
close(state, mark) {
65+
if (state.isAutolink) {
6166
state.isAutolink = undefined;
62-
return (
63-
'](' +
64-
escapeParenthesesInUrl(mark.attrs[LinkAttr.Href]) +
65-
(mark.attrs[LinkAttr.Title]
66-
? ' ' + state.quote(mark.attrs[LinkAttr.Title])
67-
: '') +
68-
')'
69-
);
70-
},
71-
},
72-
fromMd: {
73-
tokenSpec: {
74-
name: linkMarkName,
75-
type: 'mark',
76-
getAttrs: (tok) => ({
77-
href: tok.attrGet('href'),
78-
title: tok.attrGet('title') || null,
79-
}),
80-
},
67+
if (mark.attrs[LinkAttr.RawLink]) return '';
68+
69+
return '>';
70+
}
71+
state.isAutolink = undefined;
72+
return (
73+
'](' +
74+
escapeParenthesesInUrl(mark.attrs[LinkAttr.Href]) +
75+
(mark.attrs[LinkAttr.Title]
76+
? ' ' + state.quote(mark.attrs[LinkAttr.Title])
77+
: '') +
78+
')'
79+
);
8180
},
82-
}),
83-
builder.Priority.High,
84-
);
81+
}));
8582
};
8683

8784
function isPlainURL(link: Mark, parent: Node, index: number, side: number) {

0 commit comments

Comments
 (0)