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

Adding markdown to block-text #106

Merged
merged 3 commits into from
May 31, 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
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>;
}
Loading