-
Notifications
You must be signed in to change notification settings - Fork 862
[EuiMarkdown] Allow mailto: links by default & allow customization of link validation #5790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cee-chen
merged 13 commits into
elastic:main
from
chandlerprall:bug/5770-markdown-mailto
May 7, 2022
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
272c3d6
Transform mailto: links in markdown to just the target address, as we…
chandlerprall 9daeb61
documentation
chandlerprall c8c2ae8
changelog
chandlerprall 04af037
corrected changelog entry filename
chandlerprall 15f4f71
Lint issues
chandlerprall 20bae8a
Refactored markdownLinkValidator to be configurable
chandlerprall fdac6a1
documentation, documentation, documentation
chandlerprall b5a9644
PR feedback
chandlerprall d42cfe3
enable mailto: links by default in markdown
chandlerprall fd76cae
updated comment
chandlerprall 5b3e2f2
test to verify mailto protocol is supported
chandlerprall 4e31ab0
pr feedback
chandlerprall 0ce1ee9
[PR feedback] Changelog
constancecchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
27 changes: 27 additions & 0 deletions
27
src-docs/src/views/markdown_editor/markdown_format_links.js
This file contains hidden or 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,27 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { EuiMarkdownFormat } from '../../../../src'; | ||
|
|
||
| // eslint-disable-next-line no-restricted-globals | ||
| const locationPathname = location.pathname; | ||
|
|
||
| export const markdownContent = `**Links starting with http:, https:, and / are valid:** | ||
|
|
||
| * https://elastic.com | ||
| * http://elastic.com | ||
| * https link to [elastic.co](https://elastic.co) | ||
| * http link to [elastic.co](http://elastic.co) | ||
| * relative link to [eui doc's homepage](${locationPathname}) | ||
|
|
||
| **Other link protocols are kept as their markdown source:** | ||
| * ftp://elastic.co | ||
| * An [ftp link](ftp://elastic.co) | ||
|
|
||
| **mailto: renders with only the link copy** | ||
| * Send a note to [someone](mailto:[email protected]) | ||
cee-chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * Also applies to inline email addresses like [email protected] | ||
| `; | ||
|
|
||
| export default () => { | ||
| return <EuiMarkdownFormat>{markdownContent}</EuiMarkdownFormat>; | ||
| }; | ||
34 changes: 34 additions & 0 deletions
34
src-docs/src/views/markdown_editor/markdown_link_validation.js
This file contains hidden or 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,34 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { | ||
| getDefaultEuiMarkdownParsingPlugins, | ||
| euiMarkdownLinkValidator, | ||
| EuiMarkdownFormat, | ||
| } from '../../../../src/components'; | ||
|
|
||
| // find the validation plugin and configure it to only allow https: and mailto: links | ||
| const parsingPlugins = getDefaultEuiMarkdownParsingPlugins(); | ||
| parsingPlugins.find(([plugin, config]) => { | ||
| const isValidationPlugin = plugin === euiMarkdownLinkValidator; | ||
| if (isValidationPlugin) { | ||
| config.allowProtocols = ['https:', 'mailto:']; | ||
| } | ||
| return isValidationPlugin; | ||
| }); | ||
|
|
||
| const markdown = `**Standalone links** | ||
| https://example.com | ||
| http://example.com | ||
| [email protected] | ||
|
|
||
| **As markdown syntax** | ||
| [example.com, https](https://example.com) | ||
| [example.com, http](http://example.com) | ||
| [email [email protected]](mailto:[email protected]) | ||
| `; | ||
|
|
||
| export default () => ( | ||
| <EuiMarkdownFormat parsingPluginList={parsingPlugins}> | ||
| {markdown} | ||
| </EuiMarkdownFormat> | ||
| ); |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -19,6 +19,9 @@ import { Link } from 'react-router-dom'; | |
| import MarkdownEditorWithPlugins from './markdown_editor_with_plugins'; | ||
| const markdownEditorWithPluginsSource = require('!!raw-loader!./markdown_editor_with_plugins'); | ||
|
|
||
| const linkValidationSource = require('!!raw-loader!./markdown_link_validation'); | ||
| import LinkValidation from './markdown_link_validation'; | ||
|
|
||
| const pluginSnippet = `<EuiMarkdownEditor | ||
| uiPlugin={myPluginUI} | ||
| parsingPluginList={myPluginParsingList} | ||
|
|
@@ -170,6 +173,186 @@ export const MarkdownPluginExample = { | |
| ), | ||
|
|
||
| sections: [ | ||
| { | ||
| title: 'Default plugins', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new documentation is fantastic!! Thank you for adding it! |
||
| text: ( | ||
| <> | ||
| <p> | ||
| EUI provides additional plugins by default, but these can be omitted | ||
| or otherwise customized by providing the{' '} | ||
| <EuiCode>parsingPluginList</EuiCode>,{' '} | ||
| <EuiCode>processingPluginList</EuiCode>, and{' '} | ||
| <EuiCode>uiPlugins</EuiCode> props to the editor and formatter | ||
| components. | ||
| </p> | ||
| <p>The parsing plugins, responsible for parsing markdown are:</p> | ||
| <ol> | ||
| <li> | ||
| <EuiLink | ||
| href="https://www.npmjs.com/package/remark-parse" | ||
| external | ||
| > | ||
| remark-parse | ||
| </EuiLink> | ||
| </li> | ||
| <li> | ||
| <EuiLink | ||
| href="https://github.com/elastic/eui/blob/main/src/components/markdown_editor/plugins/remark/remark_prismjs.ts" | ||
| external | ||
| > | ||
| additional pre-processing for code blocks | ||
| </EuiLink> | ||
| </li> | ||
| <li> | ||
| <EuiLink | ||
| href="https://www.npmjs.com/package/remark-emoji" | ||
| external | ||
| > | ||
| remark-emoji | ||
| </EuiLink> | ||
| </li> | ||
| <li> | ||
| <EuiLink | ||
| href="https://www.npmjs.com/package/remark-breaks" | ||
| external | ||
| > | ||
| remark-breaks | ||
| </EuiLink> | ||
| </li> | ||
| <li> | ||
| <EuiLink | ||
| href="https://github.com/elastic/eui/blob/main/src/components/markdown_editor/plugins/markdown_link_validator.tsx" | ||
| external | ||
| > | ||
| link validation for security | ||
| </EuiLink> | ||
| </li> | ||
| <li> | ||
| <EuiLink | ||
| href="https://github.com/elastic/eui/blob/main/src/components/markdown_editor/plugins/markdown_checkbox/parser.ts" | ||
| external | ||
| > | ||
| injection of EuiCheckbox for markdown check boxes | ||
| </EuiLink> | ||
| </li> | ||
| <li> | ||
| <EuiLink | ||
| href="https://github.com/elastic/eui/blob/main/src/components/markdown_editor/plugins/markdown_tooltip/parser.ts" | ||
| external | ||
| > | ||
| tooltip plugin parser | ||
| </EuiLink> | ||
| </li> | ||
| </ol> | ||
| <p> | ||
| The above set provides an abstract syntax tree used by the editor to | ||
| provide feedback, and the renderer passes that output to the set of | ||
| processing plugins to allow it to be rendered: | ||
| </p> | ||
| <ol> | ||
| <li> | ||
| <EuiLink | ||
| href="https://www.npmjs.com/package/remark-rehype" | ||
| external | ||
| > | ||
| remark-rehype | ||
| </EuiLink> | ||
| </li> | ||
| <li> | ||
| <EuiLink | ||
| href="https://www.npmjs.com/package/rehype-react" | ||
| external | ||
| > | ||
| rehype-react | ||
| </EuiLink> | ||
| </li> | ||
| <li> | ||
| <EuiLink | ||
| href="https://github.com/elastic/eui/blob/main/src/components/markdown_editor/plugins/markdown_tooltip/renderer.tsx" | ||
| external | ||
| > | ||
| tooltip plugin renderer | ||
| </EuiLink> | ||
| </li> | ||
| </ol> | ||
| <p> | ||
| The last set of plugin configuration - <EuiCode>uiPlugins</EuiCode>{' '} | ||
| - allows toolbar buttons to be defined and how they alter or inject | ||
| markdown and returns with only one plugin: | ||
| </p> | ||
| <ol> | ||
| <li> | ||
| <EuiLink | ||
| href="https://github.com/elastic/eui/blob/main/src/components/markdown_editor/plugins/markdown_tooltip/plugin.tsx" | ||
| external | ||
| > | ||
| tooltip plugin ui | ||
| </EuiLink> | ||
| </li> | ||
| </ol> | ||
| <p> | ||
| These plugin definitions can be obtained by calling{' '} | ||
| <EuiCode>getDefaultEuiMarkdownParsingPlugins</EuiCode>,{' '} | ||
| <EuiCode>getDefaultEuiMarkdownProcessingPlugins</EuiCode>, and{' '} | ||
| <EuiCode>getDefaultEuiMarkdownUiPlugins</EuiCode> respectively. Each | ||
| of these three functions take an optional configuration object with | ||
| an <EuiCode>exclude</EuiCode> key, an array of EUI-defaulted plugins | ||
| to disable. Currently the only option this configuration can take is{' '} | ||
| <EuiCode>'tooltip'</EuiCode>. | ||
| </p> | ||
| </> | ||
| ), | ||
| }, | ||
| { | ||
| source: [ | ||
| { | ||
| type: GuideSectionTypes.JS, | ||
| code: linkValidationSource, | ||
| }, | ||
| ], | ||
| title: 'Link validation & security', | ||
| text: ( | ||
| <Fragment> | ||
| <p> | ||
| To enhance user and application security, the default behavior | ||
| removes links to URLs that aren't relative (beginning with{' '} | ||
| <EuiCode>/</EuiCode>) and don't use the{' '} | ||
| <EuiCode>https:</EuiCode>, <EuiCode>http:</EuiCode>, or{' '} | ||
| <EuiCode>mailto:</EuiCode> protocols. This validation can be further | ||
| configured or removed altogether. | ||
| </p> | ||
| <p> | ||
| In this example only <EuiCode>https:</EuiCode> and{' '} | ||
| <EuiCode>mailto:</EuiCode> links are allowed. | ||
| </p> | ||
| </Fragment> | ||
| ), | ||
| snippet: [ | ||
| `// change what link protocols are allowed | ||
| const parsingPlugins = getDefaultEuiMarkdownParsingPlugins(); | ||
| parsingPlugins.find(([plugin, config]) => { | ||
| const isValidationPlugin = plugin === euiMarkdownLinkValidator; | ||
| if (isValidationPlugin) { | ||
| config.allowProtocols = ['https:', 'mailto:']; | ||
| } | ||
| return isValidationPlugin; | ||
| });`, | ||
| `// filter out the link validation plugin | ||
| const parsingPlugins = getDefaultEuiMarkdownParsingPlugins().filter(([plugin]) => { | ||
| return plugin !== euiMarkdownLinkValidator; | ||
| });`, | ||
| `// disable relative urls | ||
| const parsingPlugins = getDefaultEuiMarkdownParsingPlugins(); | ||
| parsingPlugins.find(([plugin, config]) => { | ||
| const isValidationPlugin = plugin === euiMarkdownLinkValidator; | ||
| if (isValidationPlugin) { | ||
| config.allowRelative = false; | ||
| } | ||
| return isValidationPlugin; | ||
| });`, | ||
| ], | ||
| demo: <LinkValidation />, | ||
| }, | ||
| { | ||
| wrapText: false, | ||
| text: ( | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.