-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Mardown Shortcut plugin Create a markdown shortcuts plugin #80
- Loading branch information
Showing
5 changed files
with
6,318 additions
and
5,207 deletions.
There are no files selected for viewing
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
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
86 changes: 86 additions & 0 deletions
86
packages/svelte-lexical/src/lib/core/plugins/MardownShortcut/MarkdownShortcutPlugin.svelte
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,86 @@ | ||
<script lang="ts"> | ||
import {onMount} from 'svelte'; | ||
import {ImageNode, getEditor} from 'svelte-lexical'; | ||
import { | ||
registerMarkdownShortcuts, | ||
type ElementTransformer, | ||
} from '@lexical/markdown'; | ||
import { | ||
CHECK_LIST, | ||
ELEMENT_TRANSFORMERS, | ||
TEXT_FORMAT_TRANSFORMERS, | ||
TEXT_MATCH_TRANSFORMERS, | ||
type TextMatchTransformer, | ||
type Transformer, | ||
} from '@lexical/markdown'; | ||
import { | ||
$createImageNode as createImageNode, | ||
$isImageNode as isImageNode, | ||
} from '../Image/ImageNode.js'; | ||
import { | ||
$isHorizontalRuleNode as isHorizontalRuleNode, | ||
HorizontalRuleNode, | ||
$createHorizontalRuleNode as createHorizontalRuleNode, | ||
} from '../HorizontalRuleNode.js'; | ||
import type {LexicalNode} from 'lexical'; | ||
const editor = getEditor(); | ||
const HR: ElementTransformer = { | ||
dependencies: [HorizontalRuleNode], | ||
export: (node: LexicalNode) => { | ||
return isHorizontalRuleNode(node) ? '***' : null; | ||
}, | ||
regExp: /^(---|\*\*\*|___)\s?$/, | ||
replace: (parentNode, _1, _2, isImport) => { | ||
const line = createHorizontalRuleNode(); | ||
// TODO: Get rid of isImport flag | ||
if (isImport || parentNode.getNextSibling() != null) { | ||
parentNode.replace(line); | ||
} else { | ||
parentNode.insertBefore(line); | ||
} | ||
line.selectNext(); | ||
}, | ||
type: 'element', | ||
}; | ||
export const IMAGE: TextMatchTransformer = { | ||
dependencies: [ImageNode], | ||
export: (node) => { | ||
if (!isImageNode(node)) { | ||
return null; | ||
} | ||
return `![${node.getAltText()}](${node.getSrc()})`; | ||
}, | ||
importRegExp: /!(?:\[([^[]*)\])(?:\(([^(]+)\))/, | ||
regExp: /!(?:\[([^[]*)\])(?:\(([^(]+)\))$/, | ||
replace: (textNode, match) => { | ||
const [, altText, src] = match; | ||
const imageNode = createImageNode({ | ||
altText, | ||
maxWidth: 800, | ||
src, | ||
}); | ||
textNode.replace(imageNode); | ||
}, | ||
trigger: ')', | ||
type: 'text-match', | ||
}; | ||
const TRANSFORMERS: Array<Transformer> = [ | ||
HR, | ||
IMAGE, | ||
CHECK_LIST, | ||
...ELEMENT_TRANSFORMERS, | ||
...TEXT_FORMAT_TRANSFORMERS, | ||
...TEXT_MATCH_TRANSFORMERS, | ||
]; | ||
onMount(() => { | ||
registerMarkdownShortcuts(editor, TRANSFORMERS); | ||
}); | ||
</script> |
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
Oops, something went wrong.