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

feat: add plainText to headings #28

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ You can provide your own React components to the renderer, both for blocks and m

- **Blocks** are full-width elements, usually at the root of the content. The available options are:
- paragraph
- heading (receives `level`)
- heading (receives `level` and `plainText`)
- list (receives `format`)
- quote
- code (receives `plainText`)
Expand Down
2 changes: 1 addition & 1 deletion src/Block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const voidTypes = ['image'];
const augmentProps = (content: Node) => {
const { children: childrenNodes, type, ...props } = content;

if (type === 'code') {
if (type === 'code' || type === 'heading') {
// Builds a plain text string from an array of nodes, regardless of links or modifiers
const getPlainText = (children: typeof childrenNodes): string => {
return children.reduce((currentPlainText, node) => {
Expand Down
2 changes: 1 addition & 1 deletion src/BlocksRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type Node = RootNode | NonTextInlineNode;
type GetPropsFromNode<T> = Omit<T, 'type' | 'children'> & {
children?: React.ReactNode;
// For code blocks, add a plainText property that is created by this renderer
plainText?: T extends { type: 'code' } ? string : never;
plainText?: T extends { type: 'code' | 'heading' } ? string : never;
};

// Map of all block types to their matching React component
Expand Down
22 changes: 22 additions & 0 deletions tests/BlocksRenderer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -445,5 +445,27 @@ describe('BlocksRenderer', () => {

expect(screen.getByText('const a = 1;const b = 2;')).toBeInTheDocument();
});

it('parses headings to plain text', () => {
render(
<BlocksRenderer
content={content}
blocks={{
heading: (props) => {
const HeadingLevel = `h${props.level}` as keyof React.JSX.IntrinsicElements;
return (
<HeadingLevel data-testid="heading-with-id" data-plain-text={props.plainText}>
{props.children}
</HeadingLevel>
);
},
}}
/>
);

const element = screen.getByTestId('heading-with-id');

expect(element.dataset.plainText).toBe('A cool website');
});
});
});