Skip to content

Commit

Permalink
docs: catch all plugin example
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed Dec 13, 2023
1 parent edfc236 commit aa684b7
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/examples/error-handling.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -56,3 +61,54 @@ export function MissingPlugins() {
/>
)
}

const CatchAllVisitor: MdastImportVisitor<Mdast.Content> = {
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 (
<MDXEditor
onError={(msg) => console.warn(msg)}
markdown={`# Hello`}
onChange={(md) => console.log('change', { md })}
plugins={[
catchAllPlugin(),
toolbarPlugin({
toolbarContents: () => (
<DiffSourceToggleWrapper>
<ConditionalContents
options={[
{ when: (editor) => editor?.editorType === 'codeblock', contents: () => <ChangeCodeMirrorLanguage /> },
{ when: (editor) => editor?.editorType === 'sandpack', contents: () => <ShowSandpackInfo /> },
{
fallback: () => (
<>
<UndoRedo />
<BoldItalicUnderlineToggles />
</>
)
}
]}
/>
</DiffSourceToggleWrapper>
)
}),
diffSourcePlugin({ viewMode: 'rich-text', diffMarkdown: 'boo' })
]}
/>
)
}

0 comments on commit aa684b7

Please sign in to comment.