From 6c9fe6b569494856c5d3775e7e9ede56ed7c0914 Mon Sep 17 00:00:00 2001 From: Victor Tortolero Date: Tue, 6 Apr 2021 20:11:11 -0500 Subject: [PATCH] Fix MD code snippet format inside Description #13589 --- .../src/blocks/Description.stories.tsx | 27 ++++++++++++++++- lib/components/src/blocks/Source.tsx | 2 +- .../src/typography/DocumentFormatting.tsx | 29 +++++++++++++++---- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/lib/components/src/blocks/Description.stories.tsx b/lib/components/src/blocks/Description.stories.tsx index 14bbfaacb87..9a13a2863f5 100644 --- a/lib/components/src/blocks/Description.stories.tsx +++ b/lib/components/src/blocks/Description.stories.tsx @@ -29,7 +29,27 @@ const markdownWithLinksCaption = ` He stared at the clinic, [Molly](https://storybook.js.org/) took him to the *[Tank War](https://storybook.js.org/)*, mouth touched with hot gold as a gliding cursor struck sparks from the wall of a **[skyscraper](https://storybook.js.org/)** canyon. `; -const Template = (args) => ; +const markdownWithCodeSnippets = ` +# My Example Markdown + +An \`inline\` codeblock + +\`\`\`tsx +// TypeScript React code block +export const MyStory = () => { + return ; +}; +\`\`\` + +\`\`\` +code block with with no language +const a = fn({ + b: 2, +}); +\`\`\` +`; + +const Template = (args: React.ComponentProps) => ; export const Text = Template.bind({}); Text.args = { @@ -45,3 +65,8 @@ export const MarkdownLinks = Template.bind({}); MarkdownLinks.args = { markdown: markdownWithLinksCaption, }; + +export const MarkdownCodeSnippets = Template.bind({}); +MarkdownCodeSnippets.args = { + markdown: markdownWithCodeSnippets, +}; diff --git a/lib/components/src/blocks/Source.tsx b/lib/components/src/blocks/Source.tsx index f1ffcc97270..546c2d9ab98 100644 --- a/lib/components/src/blocks/Source.tsx +++ b/lib/components/src/blocks/Source.tsx @@ -71,4 +71,4 @@ const Source: FunctionComponent = (props) => { Source.defaultProps = { format: false, }; -export { Source }; +export { Source, StyledSyntaxHighlighter }; diff --git a/lib/components/src/typography/DocumentFormatting.tsx b/lib/components/src/typography/DocumentFormatting.tsx index 59d5a0fa9da..545cddb0ba2 100644 --- a/lib/components/src/typography/DocumentFormatting.tsx +++ b/lib/components/src/typography/DocumentFormatting.tsx @@ -1,7 +1,7 @@ import React, { FunctionComponent } from 'react'; import { styled, CSSObject } from '@storybook/theming'; import { withReset, withMargin, headerCommon, codeCommon } from './shared'; -import { SyntaxHighlighter } from '../syntaxhighlighter/lazy-syntaxhighlighter'; +import { StyledSyntaxHighlighter } from '../blocks/Source'; export const H1 = styled.h1<{}>(withReset, headerCommon, ({ theme }) => ({ fontSize: `${theme.typography.size.l1}px`, @@ -330,14 +330,33 @@ const DefaultCodeBlock = styled.code<{}>( codeCommon ); -export const Code = ({ className, ...props }: React.ComponentProps) => { +export const Code = ({ + className, + children, + ...props +}: React.ComponentProps) => { const language = (className || '').match(/lang-(\S+)/); + const isInlineCode = !(children as string).match(/[\n\r]/g); - if (!language) { - return ; + if (isInlineCode) { + return ( + + {children} + + ); } - return ; + return ( + + {children} + + ); }; export const TT = styled.title<{}>(codeCommon);