Skip to content

Commit f55762f

Browse files
authored
Merge pull request #28 from benjaminrobinet/feature/headings-plaintext
feat: add plainText to headings
2 parents 2e9c3c4 + 07c9518 commit f55762f

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ You can provide your own React components to the renderer, both for blocks and m
4040

4141
- **Blocks** are full-width elements, usually at the root of the content. The available options are:
4242
- paragraph
43-
- heading (receives `level`)
43+
- heading (receives `level` and `plainText`)
4444
- list (receives `format`)
4545
- quote
4646
- code (receives `plainText`)

Diff for: src/Block.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const voidTypes = ['image'];
1717
const augmentProps = (content: Node) => {
1818
const { children: childrenNodes, type, ...props } = content;
1919

20-
if (type === 'code') {
20+
if (type === 'code' || type === 'heading') {
2121
// Builds a plain text string from an array of nodes, regardless of links or modifiers
2222
const getPlainText = (children: typeof childrenNodes): string => {
2323
return children.reduce((currentPlainText, node) => {

Diff for: src/BlocksRenderer.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type Node = RootNode | NonTextInlineNode;
8888
type GetPropsFromNode<T> = Omit<T, 'type' | 'children'> & {
8989
children?: React.ReactNode;
9090
// For code blocks, add a plainText property that is created by this renderer
91-
plainText?: T extends { type: 'code' } ? string : never;
91+
plainText?: T extends { type: 'code' | 'heading' } ? string : never;
9292
};
9393

9494
// Map of all block types to their matching React component

Diff for: tests/BlocksRenderer.test.tsx

+22
Original file line numberDiff line numberDiff line change
@@ -455,5 +455,27 @@ describe('BlocksRenderer', () => {
455455

456456
expect(screen.getByText('const a = 1;const b = 2;')).toBeInTheDocument();
457457
});
458+
459+
it('parses headings to plain text', () => {
460+
render(
461+
<BlocksRenderer
462+
content={content}
463+
blocks={{
464+
heading: (props) => {
465+
const HeadingLevel = `h${props.level}` as keyof React.JSX.IntrinsicElements;
466+
return (
467+
<HeadingLevel data-testid="heading-with-id" data-plain-text={props.plainText}>
468+
{props.children}
469+
</HeadingLevel>
470+
);
471+
},
472+
}}
473+
/>
474+
);
475+
476+
const element = screen.getByTestId('heading-with-id');
477+
478+
expect(element.dataset.plainText).toBe('A cool website');
479+
});
458480
});
459481
});

0 commit comments

Comments
 (0)