Skip to content

Commit bd353cc

Browse files
committed
Fix code blocks and fences extra line formatting
1 parent 06248eb commit bd353cc

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-markdown-display",
3-
"version": "4.0.8",
3+
"version": "4.0.9",
44
"description": "Markdown renderer for react-native, with CommonMark spec support + adds syntax extensions & sugar (URL autolinking, typographer), originally created by Mient-jan Stelling as react-native-markdown-renderer",
55
"main": "src/index.js",
66
"types": "src/index.d.ts",

src/lib/renderRules.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,36 @@ const renderRules = {
137137
);
138138
},
139139
code_block: (node, children, parent, styles) => {
140+
// we trim new lines off the end of code blocks because the parser sends an extra one.
141+
let {content} = node;
142+
143+
if (
144+
typeof node.content === 'string' &&
145+
node.content.charAt(node.content.length - 1) === '\n'
146+
) {
147+
content = node.content.substring(0, node.content.length - 1);
148+
}
149+
140150
return (
141151
<Text key={node.key} style={styles.codeBlock}>
142-
{node.content}
152+
{content}
143153
</Text>
144154
);
145155
},
146156
fence: (node, children, parent, styles) => {
157+
// we trim new lines off the end of code blocks because the parser sends an extra one.
158+
let {content} = node;
159+
160+
if (
161+
typeof node.content === 'string' &&
162+
node.content.charAt(node.content.length - 1) === '\n'
163+
) {
164+
content = node.content.substring(0, node.content.length - 1);
165+
}
166+
147167
return (
148168
<Text key={node.key} style={styles.codeBlock}>
149-
{node.content}
169+
{content}
150170
</Text>
151171
);
152172
},

0 commit comments

Comments
 (0)