Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 make inline sync plugin support global nodes
- Loading branch information
1 parent
a92384a
commit 2e2f179
Showing
7 changed files
with
341 additions
and
245 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
packages/preset-commonmark/src/plugin/inline-sync/config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
Oops, something went wrong.
2e2f179
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
milkdown – ./
milkdown-saul-mirone.vercel.app
milkdown.dev
milkdown.vercel.app
milkdown-git-main-saul-mirone.vercel.app
www.milkdown.dev