Skip to content
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

Add enableSoftLineBreaks to shared-ux-markdown #182635

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions packages/shared-ux/markdown/impl/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
EuiMarkdownEditor,
EuiMarkdownEditorProps,
EuiMarkdownFormat,
euiMarkdownLinkValidator,
getDefaultEuiMarkdownPlugins,
} from '@elastic/eui';
import React, { useState } from 'react';
Expand All @@ -30,6 +29,10 @@ export type MarkdownProps = Partial<
* allow opt in to default EUI link validation behavior, see {@link https://eui.elastic.co/#/editors-syntax/markdown-plugins#link-validation-security}
*/
validateLinks?: boolean;
/**
* enables regular line breaks to be rendered in HTML without `<br />` tags, see {@link https://github.github.com/gfm/#soft-line-breaks}
*/
enableSoftLineBreaks?: boolean;
/**
* provides a way to signal to a parent component that the component rendered successfully
*/
Expand Down Expand Up @@ -57,6 +60,7 @@ export const Markdown = ({
readOnly = false,
enableTooltipSupport = false,
validateLinks = false,
enableSoftLineBreaks = false,
...restProps
}: MarkdownProps) => {
const [value, setValue] = useState(defaultValue);
Expand All @@ -66,20 +70,18 @@ export const Markdown = ({
onRender?.();
}, [onRender]);

const excludingPlugins = Array<'lineBreaks' | 'linkValidator' | 'tooltip'>();
if (!enableTooltipSupport) excludingPlugins.push('tooltip');
if (!validateLinks) excludingPlugins.push('linkValidator');
if (enableSoftLineBreaks) excludingPlugins.push('lineBreaks');

const { parsingPlugins, processingPlugins, uiPlugins } = getDefaultEuiMarkdownPlugins({
exclude: enableTooltipSupport ? undefined : ['tooltip'],
exclude: excludingPlugins,
});

// openLinksInNewTab functionality from https://codesandbox.io/s/relaxed-yalow-hy69r4?file=/demo.js:482-645
processingPlugins[1][1].components.a = (props) => <EuiLink {...props} target="_blank" />;

const _parsingPlugins = validateLinks
? parsingPlugins
: // @ts-expect-error
parsingPlugins.filter(([plugin]) => {
return plugin !== euiMarkdownLinkValidator;
});

// Render EuiMarkdownFormat when readOnly set to true
if (readOnly) {
if (!children && !markdownContent) {
Expand All @@ -91,7 +93,7 @@ export const Markdown = ({
color={'inherit'}
className={className}
aria-label={ariaLabelContent ?? 'markdown component'}
parsingPluginList={_parsingPlugins}
parsingPluginList={parsingPlugins}
processingPluginList={openLinksInNewTab ? processingPlugins : undefined}
data-test-subj={restProps['data-test-subj']}
// There was a trick to pass style as a part of props in the legacy React <Markdown> component
Expand All @@ -112,7 +114,7 @@ export const Markdown = ({
onChange={setValue}
height={height}
uiPlugins={uiPlugins}
parsingPluginList={_parsingPlugins}
parsingPluginList={parsingPlugins}
processingPluginList={openLinksInNewTab ? processingPlugins : undefined}
data-test-subj={restProps['data-test-subj']}
/>
Expand Down
8 changes: 5 additions & 3 deletions packages/shared-ux/markdown/impl/markdown_format.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ export const MarkdownStoryComponent = (params: MarkdownStorybookParams) => {
markdownContent={'My content in **markdown** format set as the *markdownContent prop*'}
/>
<Markdown {...params} readOnly={true}>
{
'My content in **markdown** format passed as *children* [test link to open in new tab or not](https://www.elastic.co)'
}
{`My content in **markdown** format passed as *children*
\`openLinksInNewTab\` [test link to open in new tab or not](https://www.elastic.co)
\`enableTooltipSupport\` !{tooltip[anchor text](Tooltip content)}
\`validateLinks\` [link with non-standard scheme](testing-testing-this-is-a-non-standatd-scheme://)
`}
</Markdown>
</EuiFlexItem>
</EuiFlexGroup>
Expand Down
12 changes: 12 additions & 0 deletions packages/shared-ux/markdown/mocks/storybook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ export class MarkdownStorybookMock extends AbstractStorybookMock<
options: [0, 20, 50, 'full'],
},
},
enableTooltipSupport: {
control: 'boolean',
defaultValue: false,
},
validateLinks: {
control: 'boolean',
defaultValue: false,
},
enableSoftLineBreaks: {
control: 'boolean',
defaultValue: false,
},
};

serviceArguments = {};
Expand Down