-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Allow optional suppressing of the default JSX handling - Expose `onError` prop - Display error message in diff-source mode
- Loading branch information
Showing
9 changed files
with
231 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
title: Error handling | ||
slug: error-handling | ||
position: 100 | ||
--- | ||
|
||
# Handling Errors | ||
|
||
The markdown format can be complex due to its loose nature and you may integrate the editor in on top of existing content that you have no full control over. In this article, we will go over the error types that the editor can produce, the actual reasons behind them, and how to handle them. | ||
|
||
## Errors caused by an invalid markdown format | ||
|
||
The editor component uses the [MDAST library](https://github.com/syntax-tree/mdast-util-from-markdown) to parse the markdown content. Although it's quite forgiving, certain content can cause the parsing to fail, in which case the editor will remain empty. To obtain more information about the error, you can pass a callback to the `onError` prop - the callback will receive a payload that includes the error message and the source markdown that triggered it. | ||
|
||
### Parse errors caused by HTML-like formatting (e.g. HTML comments, or links surrounded by angle brackets) | ||
|
||
To handle common basic HTML formatting (e.g. `u` tags), the default parsing includes the [mdast-util-mdx-jsx extension](https://github.com/syntax-tree/mdast-util-mdx-jsx). In some cases, this can cause the parsing to fail. You can disable this extension by setting the `suppressHtmlProcessing` prop to `true`, but you will lose the ability to use HTML-like formatting in your markdown. | ||
|
||
## Errors due to missing plugins | ||
|
||
Another problem that can occur during markdown parsing is the lack of plugins to handle certain markdown features. For example, the markdown may include table syntax, but the editor may not have the table plugin enabled. Internally, this exception is going to happen at the phase where mdast nodes are converted into lexical nodes (the UI rendered in the rich text editing surface). Just like in the previous case, you can use the `onError` prop to handle these errors. You can also add a custom "catch-all" plugin that register a mdast visitor with low priority that will handle all unknown nodes. See `./extending-the-editor` for more information. | ||
|
||
## Enable source mode to allow the user to recover from errors | ||
|
||
The diff-source plugin can be used as an "escape hatch" for potentially invalid markdown. Out of the box, the plugin will attach listeners to the markdown conversion, and, if it fails, will display an error message suggesting the user to switch to source mode and fix the problem there. If the user fixes the problem, then switching to rich text mode will work and the content will be displayed correctly. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
## Job Applications winter 2021 | ||
|
||
* Macquarie commodities trader <https://www.careers.macquarie.com/en/job/961296/graduate-analyst-commodity-markets-and-finance> | ||
* BBC AI postgrad <https://careerssearch.bbc.co.uk/jobs/job/Artificial-Intelligence-Postgraduate-Apprentice-Scheme-Level-7/57158> | ||
* Canva | ||
* Orsted | ||
* Aldi | ||
* Amazon | ||
* EY | ||
* crossing minds | ||
* | ||
* ibm | ||
* <https://www.workatastartup.com/companies/humanloop/website> |
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,58 @@ | ||
import React from 'react' | ||
import { | ||
BoldItalicUnderlineToggles, | ||
ChangeCodeMirrorLanguage, | ||
ConditionalContents, | ||
DiffSourceToggleWrapper, | ||
MDXEditor, | ||
ShowSandpackInfo, | ||
UndoRedo, | ||
diffSourcePlugin, | ||
toolbarPlugin | ||
} from '../' | ||
import markdown from './assets/buggy-markdown.md?raw' | ||
import { ALL_PLUGINS } from './_boilerplate' | ||
|
||
export function BuggyMarkdown() { | ||
return ( | ||
<MDXEditor | ||
onError={(msg) => console.warn(msg)} | ||
markdown={markdown} | ||
onChange={(md) => console.log('change', { md })} | ||
plugins={ALL_PLUGINS} | ||
/> | ||
) | ||
} | ||
|
||
export function MissingPlugins() { | ||
return ( | ||
<MDXEditor | ||
onError={(msg) => console.warn(msg)} | ||
markdown={`# Hello`} | ||
onChange={(md) => console.log('change', { md })} | ||
plugins={[ | ||
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' }) | ||
]} | ||
/> | ||
) | ||
} |
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
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.