Skip to content

Commit

Permalink
Add parsing inline markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed Jul 16, 2022
1 parent 9a30dc6 commit 08dbf23
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ const Markdown = (props) => {
};

// convert input markdown into tokens
const lexer = new Lexer(lexerOptions);
const tokens = lexer.lex(props.value ?? props.children ?? '');
const markdownString = props.value ?? props.children ?? '';
const tokens = props.isInline
? Lexer.lexInline(markdownString, lexerOptions)
: Lexer.lex(markdownString, lexerOptions);

// parser options
const parserOptions = {
Expand All @@ -37,12 +39,16 @@ const Markdown = (props) => {
}),
};

const children = new ReactParser(parserOptions).parse(tokens);
const parser = new ReactParser(parserOptions);
const children = props.isInline
? parser.parseInline(tokens)
: parser.parse(tokens);

return createElement(Fragment, null, children);
};

Markdown.defaultProps = {
isInline: false,
breaks: false,
gfm: true,
baseURL: null,
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ declare module 'marked-react' {
import { ReactElement, FC, ReactNode } from 'react';

export interface MarkdownOptions {
isInline?: boolean;
breaks?: boolean;
gfm?: boolean;
baseURL?: string;
Expand Down
8 changes: 8 additions & 0 deletions stories/0-MarkedReact.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,11 @@ AllProps.args = {
};

AllProps.parameters = Default.parameters;

export const InlineMarkdown = Template.bind({});
InlineMarkdown.args = {
value: 'Hello world!',
isInline: true,
};

InlineMarkdown.parameters = Default.parameters;
6 changes: 6 additions & 0 deletions tests/component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ describe('Markdown Component', () => {
const html = renderToStaticMarkup(marked);
expect(html).toBe('<h1>This is a heading: Hello World!</h1>');
});

it('should use parse inline markdown', () => {
const marked = createElement(Markdown, { value: 'Hello World!', isInline: true });
const html = renderToStaticMarkup(marked);
expect(html).toBe('Hello World!');
});
});

0 comments on commit 08dbf23

Please sign in to comment.