Skip to content

Commit

Permalink
feat: 🎸 make inline sync plugin support global nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Sep 5, 2022
1 parent a92384a commit 2e2f179
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 245 deletions.
245 changes: 0 additions & 245 deletions packages/preset-commonmark/src/plugin/inline-sync.ts

This file was deleted.

56 changes: 56 additions & 0 deletions packages/preset-commonmark/src/plugin/inline-sync/config.ts
@@ -0,0 +1,56 @@
/* Copyright 2021, Milkdown by Mirone. */
import { createSlice, Ctx } from '@milkdown/core';
import { Node, NodeType } from '@milkdown/prose/model';
import { Transaction } from '@milkdown/prose/state';

import { swap } from './utils';

export type ShouldSyncNode = (context: {
prevNode: Node;
nextNode: Node;
ctx: Ctx;
tr: Transaction;
text: string;
}) => boolean;

export type SyncNodePlaceholder = {
hole: string;
punctuation: string;
char: string;
};

export type InlineSyncConfig = {
placeholderConfig: SyncNodePlaceholder;
shouldSyncNode: ShouldSyncNode;
globalNodes: Array<NodeType | string>;
movePlaceholder: (placeholderToMove: string, text: string) => string;
};

export const defaultConfig: InlineSyncConfig = {
placeholderConfig: {
hole: '∅',
punctuation: '⁂',
char: '∴',
},
globalNodes: ['footnote_definition'],
shouldSyncNode: ({ prevNode, nextNode }) =>
prevNode.inlineContent &&
nextNode &&
// if node type changes, do not sync
prevNode.type === nextNode.type &&
// if two node fully equal, we don't modify them
!prevNode.eq(nextNode),
movePlaceholder: (placeholderToMove: string, text: string) => {
const symbolsNeedToMove = ['*', '_'];

let index = text.indexOf(placeholderToMove);
while (symbolsNeedToMove.includes(text[index - 1] || '') && symbolsNeedToMove.includes(text[index + 1] || '')) {
text = swap(text, index, index + 1);
index = index + 1;
}

return text;
},
};

export const inlineSyncConfigCtx = createSlice<InlineSyncConfig, 'inlineSyncConfig'>(defaultConfig, 'inlineSyncConfig');

1 comment on commit 2e2f179

@vercel
Copy link

@vercel vercel bot commented on 2e2f179 Sep 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.