Skip to content

Commit

Permalink
Adding markdown to block-text (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
cohitre authored May 31, 2024
1 parent c65dbd2 commit e00fd58
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
17 changes: 13 additions & 4 deletions packages/block-text/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion packages/block-text/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@usewaypoint/block-text",
"version": "0.0.3",
"version": "0.0.4",
"description": "@usewaypoint/document compatible Text component",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand All @@ -26,5 +26,8 @@
},
"devDependencies": {
"@testing-library/react": "^14.2.1"
},
"dependencies": {
"markdown-parser-react": "^1.1.2"
}
}
20 changes: 20 additions & 0 deletions packages/block-text/src/__snapshots__/index.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,23 @@ exports[`block-text renders with default values 1`] = `
<div />
</DocumentFragment>
`;

exports[`block-text renders with safe markdown 1`] = `
<DocumentFragment>
<div>
<div>
<h2>
This is &lt;span&gt;markdown&lt;/span&gt;
</h2>
</div>
</div>
</DocumentFragment>
`;

exports[`block-text renders without markdown 1`] = `
<DocumentFragment>
<div>
## This is not &lt;span&gt;markdown&lt;/span&gt;
</div>
</DocumentFragment>
`;
25 changes: 25 additions & 0 deletions packages/block-text/src/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,29 @@ describe('block-text', () => {
it('renders with default values', () => {
expect(render(<Text />).asFragment()).toMatchSnapshot();
});

it('renders with safe markdown', () => {
expect(
render(
<Text
props={{
text: `## This is <span>markdown</span>`,
markdown: true,
}}
/>
).asFragment()
).toMatchSnapshot();
});

it('renders without markdown', () => {
expect(
render(
<Text
props={{
text: `## This is not <span>markdown</span>`,
}}
/>
).asFragment()
).toMatchSnapshot();
});
});
9 changes: 9 additions & 0 deletions packages/block-text/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Markdown from 'markdown-parser-react';
import React, { CSSProperties } from 'react';
import { z } from 'zod';

Expand Down Expand Up @@ -74,6 +75,7 @@ export const TextPropsSchema = z.object({
.nullable(),
props: z
.object({
markdown: z.boolean().optional().nullable(),
text: z.string().optional().nullable(),
})
.optional()
Expand All @@ -98,5 +100,12 @@ export function Text({ style, props }: TextProps) {
};

const text = props?.text ?? TextPropsDefaults.text;
if (props?.markdown) {
return (
<div style={wStyle}>
<Markdown content={text} />
</div>
);
}
return <div style={wStyle}>{text}</div>;
}

0 comments on commit e00fd58

Please sign in to comment.