Skip to content

Commit 41554ad

Browse files
authoredSep 12, 2024
fix: nl2br warning (#1301)
1 parent 6217e20 commit 41554ad

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed
 

‎package.json

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"react-dom": "^18.3.1",
4747
"react-error-boundary": "^4.0.13",
4848
"react-i18next": "^15.0.1",
49-
"react-nl2br": "^1.0.4",
5049
"react-router-dom": "^6.26.0",
5150
"sharp": "^0.33.4",
5251
"siwe": "^2.3.2",

‎src/components/Blog/CustomRichBlocks.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { MediaAttributes } from '@/types/strapi';
1010
import { BlocksRenderer } from '@strapi/blocks-react-renderer';
1111
import type { RootNode } from 'node_modules/@strapi/blocks-react-renderer/dist/BlocksRenderer';
1212
import { isValidElement, type JSX, type ReactElement } from 'react';
13-
import nl2br from 'react-nl2br';
13+
import newlineToBr from 'src/utils/newlineToBr';
1414
import { BlogParagraphContainer } from './BlogArticle/BlogArticle.style';
1515
import type { BlogWidgetProps } from './BlogWidget';
1616
import { BlogWidget } from './BlogWidget';
@@ -238,12 +238,12 @@ export const CustomRichBlocks = ({
238238
</BlogLink>
239239
);
240240
} else {
241-
const nl2brText: Array<ReactElement | string> = nl2br(
241+
const nl2brText: Array<ReactElement | string> = newlineToBr(
242242
el.props.text,
243243
);
244244
return nl2brText.map((line, lineIndex: number) => {
245245
if (isValidElement(line) && line.type === 'br') {
246-
// adds <br> from nl2br
246+
// adds <br>
247247
return line;
248248
}
249249
return (
@@ -260,7 +260,7 @@ export const CustomRichBlocks = ({
260260
});
261261
}
262262
} else {
263-
return <></>;
263+
return null;
264264
}
265265
})}
266266
</BlogParagraphContainer>

‎src/utils/newlineToBr.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { ReactElement } from 'react';
2+
import { createElement, Fragment } from 'react';
3+
4+
const newlineToBr = (str: unknown): (string | ReactElement)[] => {
5+
if (typeof str !== 'string') {
6+
return [String(str)];
7+
}
8+
9+
return str.split(/(\r\n|\r|\n)/g).map((line, index) => {
10+
if (line.match(/(\r\n|\r|\n)/g)) {
11+
return createElement('br', { key: index });
12+
}
13+
return createElement(Fragment, { key: index }, line);
14+
});
15+
};
16+
17+
export default newlineToBr;

‎yarn.lock

-10
Original file line numberDiff line numberDiff line change
@@ -9520,7 +9520,6 @@ __metadata:
95209520
react-dom: "npm:^18.3.1"
95219521
react-error-boundary: "npm:^4.0.13"
95229522
react-i18next: "npm:^15.0.1"
9523-
react-nl2br: "npm:^1.0.4"
95249523
react-router-dom: "npm:^6.26.0"
95259524
rollup-plugin-polyfill-node: "npm:^0.13.0"
95269525
sharp: "npm:^0.33.4"
@@ -11306,15 +11305,6 @@ __metadata:
1130611305
languageName: node
1130711306
linkType: hard
1130811307

11309-
"react-nl2br@npm:^1.0.4":
11310-
version: 1.0.4
11311-
resolution: "react-nl2br@npm:1.0.4"
11312-
peerDependencies:
11313-
react: ^15.7.0 || ^16.14.0 || ^17.0.0 || ^18.0.0
11314-
checksum: 10/face38b19bec9ae9bf43e7efbf158a577f0810fd8d93650c464bdd80a37d61be6f59170cca4fe4993b44c8d5652c541d4f4786d30297ca3252fd99143b5b3816
11315-
languageName: node
11316-
linkType: hard
11317-
1131811308
"react-router-dom@npm:^6.26.0":
1131911309
version: 6.26.1
1132011310
resolution: "react-router-dom@npm:6.26.1"

0 commit comments

Comments
 (0)
Please sign in to comment.