@milkdown/preset-commonmark 6.3.2 → 6.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/lib/index.d.ts +1 -0
  2. package/lib/index.d.ts.map +1 -1
  3. package/lib/index.es.js +660 -525
  4. package/lib/index.es.js.map +1 -1
  5. package/lib/mark/code-inline.d.ts.map +1 -1
  6. package/lib/mark/em.d.ts.map +1 -1
  7. package/lib/mark/link.d.ts.map +1 -1
  8. package/lib/mark/strong.d.ts.map +1 -1
  9. package/lib/node/bullet-list.d.ts.map +1 -1
  10. package/lib/node/heading.d.ts.map +1 -1
  11. package/lib/node/image.d.ts.map +1 -1
  12. package/lib/node/list-item.d.ts.map +1 -1
  13. package/lib/node/ordered-list.d.ts.map +1 -1
  14. package/lib/plugin/index.d.ts +2 -0
  15. package/lib/plugin/index.d.ts.map +1 -1
  16. package/lib/plugin/inline-sync/config.d.ts +24 -0
  17. package/lib/plugin/inline-sync/config.d.ts.map +1 -0
  18. package/lib/plugin/inline-sync/context.d.ts +12 -0
  19. package/lib/plugin/inline-sync/context.d.ts.map +1 -0
  20. package/lib/plugin/inline-sync/index.d.ts +6 -0
  21. package/lib/plugin/inline-sync/index.d.ts.map +1 -0
  22. package/lib/plugin/inline-sync/regexp.d.ts +3 -0
  23. package/lib/plugin/inline-sync/regexp.d.ts.map +1 -0
  24. package/lib/plugin/inline-sync/replacer.d.ts +5 -0
  25. package/lib/plugin/inline-sync/replacer.d.ts.map +1 -0
  26. package/lib/plugin/inline-sync/utils.d.ts +8 -0
  27. package/lib/plugin/inline-sync/utils.d.ts.map +1 -0
  28. package/package.json +5 -5
  29. package/src/index.ts +1 -0
  30. package/src/mark/code-inline.ts +25 -4
  31. package/src/mark/em.ts +1 -6
  32. package/src/mark/link.ts +1 -19
  33. package/src/mark/strong.ts +1 -5
  34. package/src/node/bullet-list.ts +33 -4
  35. package/src/node/heading.ts +10 -6
  36. package/src/node/image.ts +4 -5
  37. package/src/node/list-item.ts +9 -4
  38. package/src/node/ordered-list.ts +11 -3
  39. package/src/plugin/index.ts +9 -4
  40. package/src/plugin/inline-sync/config.ts +56 -0
  41. package/src/plugin/inline-sync/context.ts +115 -0
  42. package/src/plugin/inline-sync/index.ts +48 -0
  43. package/src/plugin/inline-sync/regexp.ts +6 -0
  44. package/src/plugin/inline-sync/replacer.ts +43 -0
  45. package/src/plugin/inline-sync/utils.ts +73 -0
@@ -0,0 +1,56 @@
1
+ /* Copyright 2021, Milkdown by Mirone. */
2
+ import { createSlice, Ctx } from '@milkdown/core';
3
+ import { Node, NodeType } from '@milkdown/prose/model';
4
+ import { Transaction } from '@milkdown/prose/state';
5
+
6
+ import { swap } from './utils';
7
+
8
+ export type ShouldSyncNode = (context: {
9
+ prevNode: Node;
10
+ nextNode: Node;
11
+ ctx: Ctx;
12
+ tr: Transaction;
13
+ text: string;
14
+ }) => boolean;
15
+
16
+ export type SyncNodePlaceholder = {
17
+ hole: string;
18
+ punctuation: string;
19
+ char: string;
20
+ };
21
+
22
+ export type InlineSyncConfig = {
23
+ placeholderConfig: SyncNodePlaceholder;
24
+ shouldSyncNode: ShouldSyncNode;
25
+ globalNodes: Array<NodeType | string>;
26
+ movePlaceholder: (placeholderToMove: string, text: string) => string;
27
+ };
28
+
29
+ export const defaultConfig: InlineSyncConfig = {
30
+ placeholderConfig: {
31
+ hole: '∅',
32
+ punctuation: '⁂',
33
+ char: '∴',
34
+ },
35
+ globalNodes: ['footnote_definition'],
36
+ shouldSyncNode: ({ prevNode, nextNode }) =>
37
+ prevNode.inlineContent &&
38
+ nextNode &&
39
+ // if node type changes, do not sync
40
+ prevNode.type === nextNode.type &&
41
+ // if two node fully equal, we don't modify them
42
+ !prevNode.eq(nextNode),
43
+ movePlaceholder: (placeholderToMove: string, text: string) => {
44
+ const symbolsNeedToMove = ['*', '_'];
45
+
46
+ let index = text.indexOf(placeholderToMove);
47
+ while (symbolsNeedToMove.includes(text[index - 1] || '') && symbolsNeedToMove.includes(text[index + 1] || '')) {
48
+ text = swap(text, index, index + 1);
49
+ index = index + 1;
50
+ }
51
+
52
+ return text;
53
+ },
54
+ };
55
+
56
+ export const inlineSyncConfigCtx = createSlice<InlineSyncConfig, 'inlineSyncConfig'>(defaultConfig, 'inlineSyncConfig');
@@ -0,0 +1,115 @@
1
+ /* Copyright 2021, Milkdown by Mirone. */
2
+ import { Ctx, parserCtx, serializerCtx } from '@milkdown/core';
3
+ import { Node } from '@milkdown/prose/model';
4
+ import { EditorState } from '@milkdown/prose/state';
5
+ import { pipe } from '@milkdown/utils';
6
+
7
+ import { inlineSyncConfigCtx } from './config';
8
+ import { calculatePlaceholder, keepLink, replacePunctuation } from './utils';
9
+
10
+ export * from './config';
11
+
12
+ export type InlineSyncContext = {
13
+ text: string;
14
+ prevNode: Node;
15
+ nextNode: Node;
16
+ placeholder: string;
17
+ };
18
+
19
+ const getNodeFromSelection = (state: EditorState) => {
20
+ const { selection } = state;
21
+ const { $from } = selection;
22
+ const node = $from.node();
23
+
24
+ return node;
25
+ };
26
+
27
+ const getMarkdown = (ctx: Ctx, state: EditorState, node: Node, globalNode: Node[]) => {
28
+ const serializer = ctx.get(serializerCtx);
29
+ const doc = state.schema.topNodeType.create(undefined, [node, ...globalNode]);
30
+
31
+ const markdown = serializer(doc);
32
+
33
+ return markdown;
34
+ };
35
+
36
+ const addPlaceholder = (ctx: Ctx, markdown: string) => {
37
+ const config = ctx.get(inlineSyncConfigCtx);
38
+ const holePlaceholder = config.placeholderConfig.hole;
39
+
40
+ const [firstLine = '', ...rest] = markdown.split('\n\n');
41
+
42
+ const movePlaceholder = (text: string) => config.movePlaceholder(holePlaceholder, text);
43
+
44
+ const handleText = pipe(replacePunctuation(holePlaceholder), movePlaceholder, keepLink);
45
+
46
+ let text = handleText(firstLine);
47
+ const placeholder = calculatePlaceholder(config.placeholderConfig)(text);
48
+
49
+ text = text.replace(holePlaceholder, placeholder);
50
+
51
+ text = [text, ...rest].join('\n\n');
52
+
53
+ return [text, placeholder] as [markdown: string, placeholder: string];
54
+ };
55
+
56
+ const getNewNode = (ctx: Ctx, text: string) => {
57
+ const parser = ctx.get(parserCtx);
58
+ const parsed = parser(text);
59
+
60
+ if (!parsed) return null;
61
+
62
+ return parsed.firstChild;
63
+ };
64
+
65
+ const collectGlobalNodes = (ctx: Ctx, state: EditorState) => {
66
+ const { globalNodes } = ctx.get(inlineSyncConfigCtx);
67
+ const nodes: Node[] = [];
68
+
69
+ state.doc.descendants((node) => {
70
+ if (globalNodes.includes(node.type.name) || globalNodes.includes(node.type)) {
71
+ nodes.push(node);
72
+ return false;
73
+ }
74
+ return;
75
+ });
76
+
77
+ return nodes;
78
+ };
79
+
80
+ const removeGlobalFromText = (text: string) => text.split('\n\n')[0] || '';
81
+
82
+ export const getContextByState = (ctx: Ctx, state: EditorState): InlineSyncContext | null => {
83
+ try {
84
+ const globalNode = collectGlobalNodes(ctx, state);
85
+ const node = getNodeFromSelection(state);
86
+
87
+ const markdown = getMarkdown(ctx, state, node, globalNode);
88
+ const [text, placeholder] = addPlaceholder(ctx, markdown);
89
+
90
+ const newNode = getNewNode(ctx, text);
91
+
92
+ if (!newNode || node.type !== newNode.type) return null;
93
+
94
+ // @ts-expect-error hijack the node attribute
95
+ newNode.attrs = { ...node.attrs };
96
+
97
+ newNode.descendants((node) => {
98
+ const marks = node.marks;
99
+ const link = marks.find((mark) => mark.type.name === 'link');
100
+ if (link && node.text?.includes(placeholder) && link.attrs['href'].includes(placeholder)) {
101
+ // @ts-expect-error hijack the mark attribute
102
+ link.attrs['href'] = link.attrs['href'].replace(placeholder, '');
103
+ }
104
+ });
105
+
106
+ return {
107
+ text: removeGlobalFromText(text),
108
+ prevNode: node,
109
+ nextNode: newNode,
110
+ placeholder,
111
+ };
112
+ } catch {
113
+ return null;
114
+ }
115
+ };
@@ -0,0 +1,48 @@
1
+ /* Copyright 2021, Milkdown by Mirone. */
2
+ import { Ctx, editorViewCtx } from '@milkdown/core';
3
+ import { Plugin, PluginKey } from '@milkdown/prose/state';
4
+
5
+ import { inlineSyncConfigCtx } from './config';
6
+ import { getContextByState } from './context';
7
+ import { runReplacer } from './replacer';
8
+
9
+ export * from './config';
10
+
11
+ export const inlineSyncPluginKey = new PluginKey('MILKDOWN_INLINE_SYNC');
12
+ export const getInlineSyncPlugin = (ctx: Ctx) => {
13
+ const inlineSyncPlugin = new Plugin<null>({
14
+ key: inlineSyncPluginKey,
15
+ state: {
16
+ init: () => {
17
+ return null;
18
+ },
19
+ apply: (tr, _value, _oldState, newState) => {
20
+ if (!tr.docChanged) return null;
21
+
22
+ const meta = tr.getMeta(inlineSyncPluginKey);
23
+ if (meta) {
24
+ return null;
25
+ }
26
+
27
+ const context = getContextByState(ctx, newState);
28
+ if (!context) return null;
29
+
30
+ const { prevNode, nextNode, text } = context;
31
+
32
+ const { shouldSyncNode } = ctx.get(inlineSyncConfigCtx);
33
+
34
+ if (!shouldSyncNode({ prevNode, nextNode, ctx, tr, text })) return null;
35
+
36
+ requestAnimationFrame(() => {
37
+ const { dispatch, state } = ctx.get(editorViewCtx);
38
+
39
+ runReplacer(ctx, inlineSyncPluginKey, state, dispatch, prevNode.attrs);
40
+ });
41
+
42
+ return null;
43
+ },
44
+ },
45
+ });
46
+
47
+ return inlineSyncPlugin;
48
+ };
@@ -0,0 +1,6 @@
1
+ /* Copyright 2021, Milkdown by Mirone. */
2
+
3
+ export const linkRegexp = /\[(?<span>((www|https:\/\/|http:\/\/)\S+))]\((?<url>\S+)\)/;
4
+
5
+ export const punctuationRegexp = (holePlaceholder: string) =>
6
+ new RegExp(`\\\\(?=[^\\w\\s${holePlaceholder}\\\\]|_)`, 'g');
@@ -0,0 +1,43 @@
1
+ /* Copyright 2021, Milkdown by Mirone. */
2
+ import { Ctx } from '@milkdown/core';
3
+ import { Attrs } from '@milkdown/prose/model';
4
+ import { EditorState, PluginKey, TextSelection, Transaction } from '@milkdown/prose/state';
5
+
6
+ import { inlineSyncConfigCtx } from './config';
7
+ import { getContextByState } from './context';
8
+ import { calcOffset } from './utils';
9
+
10
+ export const runReplacer = (
11
+ ctx: Ctx,
12
+ key: PluginKey,
13
+ state: EditorState,
14
+ dispatch: (tr: Transaction) => void,
15
+ attrs: Attrs,
16
+ ) => {
17
+ const { placeholderConfig } = ctx.get(inlineSyncConfigCtx);
18
+ const holePlaceholder = placeholderConfig.hole;
19
+ // insert a placeholder to restore the selection
20
+ let tr = state.tr.setMeta(key, true).insertText(holePlaceholder, state.selection.from);
21
+
22
+ const nextState = state.apply(tr);
23
+ const context = getContextByState(ctx, nextState);
24
+
25
+ if (!context) return;
26
+
27
+ const { $from } = nextState.selection;
28
+ const from = $from.before();
29
+ const to = $from.after();
30
+
31
+ const offset = calcOffset(context.nextNode, from, context.placeholder);
32
+
33
+ tr = tr
34
+ .replaceWith(from, to, context.nextNode)
35
+ .setNodeMarkup(from, undefined, attrs)
36
+ // delete the placeholder
37
+ .delete(offset + 1, offset + 2);
38
+
39
+ // restore the selection
40
+ tr = tr.setSelection(TextSelection.near(tr.doc.resolve(offset + 1)));
41
+
42
+ dispatch(tr);
43
+ };
@@ -0,0 +1,73 @@
1
+ /* Copyright 2021, Milkdown by Mirone. */
2
+
3
+ import { Node } from '@milkdown/prose/model';
4
+
5
+ import { SyncNodePlaceholder } from './config';
6
+ import { linkRegexp, punctuationRegexp } from './regexp';
7
+
8
+ export const keepLink = (str: string) => {
9
+ let text = str;
10
+ let match = text.match(linkRegexp);
11
+ while (match && match.groups) {
12
+ const { span } = match.groups;
13
+ text = text.replace(linkRegexp, span as string);
14
+
15
+ match = text.match(linkRegexp);
16
+ }
17
+ return text;
18
+ };
19
+
20
+ export const swap = (text: string, first: number, last: number) => {
21
+ const arr = text.split('');
22
+ const temp = arr[first];
23
+ if (arr[first] && arr[last]) {
24
+ arr[first] = arr[last] as string;
25
+ arr[last] = temp as string;
26
+ }
27
+ return arr.join('').toString();
28
+ };
29
+
30
+ export const replacePunctuation = (holePlaceholder: string) => (text: string) =>
31
+ text.replace(punctuationRegexp(holePlaceholder), '');
32
+
33
+ export const calculatePlaceholder = (placeholder: SyncNodePlaceholder) => (text: string) => {
34
+ const index = text.indexOf(placeholder.hole);
35
+ const left = text.charAt(index - 1);
36
+ const right = text.charAt(index + 1);
37
+ const notAWord = /[^\w]|_/;
38
+
39
+ // cursor on the right
40
+ if (!right) {
41
+ return placeholder.punctuation;
42
+ }
43
+
44
+ // cursor on the left
45
+ if (!left) {
46
+ return placeholder.char;
47
+ }
48
+
49
+ if (notAWord.test(left) && notAWord.test(right)) {
50
+ return placeholder.punctuation;
51
+ }
52
+
53
+ return placeholder.char;
54
+ };
55
+
56
+ export const calcOffset = (node: Node, from: number, placeholder: string) => {
57
+ let offset = from;
58
+ let find = false;
59
+ node.descendants((n) => {
60
+ if (find) return false;
61
+ if (n.isText) {
62
+ const i = n.text?.indexOf(placeholder);
63
+ if (i != null && i >= 0) {
64
+ find = true;
65
+ offset += i;
66
+ return false;
67
+ }
68
+ }
69
+ offset += n.nodeSize;
70
+ return;
71
+ });
72
+ return offset;
73
+ };