Skip to content

Commit

Permalink
Merge pull request #146 from ChtiJS/fix/line_break
Browse files Browse the repository at this point in the history
fix(line_break): fixing markdown to handle line break
  • Loading branch information
nfroidure authored Dec 5, 2023
2 parents 68107a9 + 60376cd commit 334d81a
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/utils/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { publicRuntimeConfig } from './config';
import { toASCIIString } from './ascii';
import { CSS_BREAKPOINT_START_L, CSS_BREAKPOINT_START_M } from './constants';
import { parseYouTubeURL } from './youtube';
import { Fragment } from 'react';
import type { ReactNode } from 'react';

export type MarkdownRootNode = {
Expand Down Expand Up @@ -76,6 +77,9 @@ export type MarkdownBlockquoteNode = {
export type MarkdownHRNode = {
type: 'thematicBreak';
};
export type MarkdownBreakNode = {
type: 'break';
};
export type MarkdownImageNode = {
type: 'image';
url: string;
Expand All @@ -98,6 +102,7 @@ export type MarkdownNode =
| MarkdownHeadingNode
| MarkdownTextNode
| MarkdownBoldNode
| MarkdownBreakNode
| MarkdownEmphasisNode
| MarkdownCodeNode
| MarkdownParagraphNode
Expand Down Expand Up @@ -182,7 +187,16 @@ const headingMap: NodeToElementMapper<MarkdownHeadingNode> = (
);
};
const textMap: NodeToElementMapper<MarkdownTextNode> = (context, node) => (
node.value.includes('\n') ?
fixText(node.value)
.split(/\r?\n/gm)
.map((text, i) => (
<Fragment key={1}>
{i > 0 ? <br /> : null}
{text}
</Fragment>
))
: fixText(node.value)
);
const boldMap: NodeToElementMapper<MarkdownEmphasisNode> = (context, node) => (
<Strong key={context.index}>
Expand Down Expand Up @@ -355,6 +369,10 @@ const hyperlinkMap: NodeToElementMapper<MarkdownLinkNode> = (context, node) => {
);
};

const breakMap: NodeToElementMapper<MarkdownBreakNode> = (context) => (
<br key={context.index} />
);

const elementsMapping = {
root: rootMap,
paragraph: paragraphMap,
Expand All @@ -371,6 +389,7 @@ const elementsMapping = {
bold: boldMap,
strong: boldMap,
html: htmlMap,
break: breakMap,
} as const;

export function parseMarkdown(input: string): MarkdownNode {
Expand Down

0 comments on commit 334d81a

Please sign in to comment.