From aa684b70c1cff9026279a89944a5ce3ac6579863 Mon Sep 17 00:00:00 2001 From: Petyo Ivanov Date: Wed, 13 Dec 2023 13:35:29 +0200 Subject: [PATCH] docs: catch all plugin example --- src/examples/error-handling.tsx | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/examples/error-handling.tsx b/src/examples/error-handling.tsx index c71c5b68..17fe738a 100644 --- a/src/examples/error-handling.tsx +++ b/src/examples/error-handling.tsx @@ -5,13 +5,18 @@ import { ConditionalContents, DiffSourceToggleWrapper, MDXEditor, + MdastImportVisitor, ShowSandpackInfo, UndoRedo, diffSourcePlugin, + realmPlugin, + system, toolbarPlugin } from '../' import markdown from './assets/buggy-markdown.md?raw' import { ALL_PLUGINS } from './_boilerplate' +import * as Mdast from 'mdast' +import { $createParagraphNode } from 'lexical' export function BuggyMarkdown() { return ( @@ -56,3 +61,54 @@ export function MissingPlugins() { /> ) } + +const CatchAllVisitor: MdastImportVisitor = { + testNode: () => true, + visitNode: ({ mdastNode, actions }) => { + console.warn('catch all', { mdastNode }) + actions.addAndStepInto($createParagraphNode()) + }, + priority: -500 +} + +const [catchAllPlugin] = realmPlugin({ + id: 'catch-all', + systemSpec: system(() => ({})), + init: (realm) => { + realm.pubKey('addImportVisitor', CatchAllVisitor) + } +}) + +export function CatchAllPlugin() { + return ( + console.warn(msg)} + markdown={`# Hello`} + onChange={(md) => console.log('change', { md })} + plugins={[ + catchAllPlugin(), + toolbarPlugin({ + toolbarContents: () => ( + + editor?.editorType === 'codeblock', contents: () => }, + { when: (editor) => editor?.editorType === 'sandpack', contents: () => }, + { + fallback: () => ( + <> + + + + ) + } + ]} + /> + + ) + }), + diffSourcePlugin({ viewMode: 'rich-text', diffMarkdown: 'boo' }) + ]} + /> + ) +}