Skip to content

Commit 016f14a

Browse files
committed
fixing issue #11 Syntax highlighting & Blockquotes
1 parent 867f81a commit 016f14a

File tree

5 files changed

+78
-7
lines changed

5 files changed

+78
-7
lines changed

example/App.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Markdown, {
88
} from 'react-native-markdown-renderer';
99
import markdownItCheckbox from 'markdown-it-checkbox';
1010
import copyAll from './src/copyAll';
11+
import code from './src/code';
1112
import customMarkdownStyle from './src/customMarkdownStyle';
1213
import copyAllCheckboxPlugin from "./src/copyAllCheckboxPlugin";
1314
import pluginRules from "./src/pluginRules";
@@ -50,7 +51,7 @@ export default class App extends Component {
5051
getView(value) {
5152
switch (value) {
5253
case 0: {
53-
return <Markdown children={copyAll} />;
54+
return <Markdown children={code} />;
5455
}
5556
case 1: {
5657
return <Markdown renderer={renderer.render} children={copyAll} />;

example/src/code.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const markdownText = `
2+
Inline \`code\`
3+
4+
Indented code
5+
6+
// Some comments
7+
line 1 of code
8+
line 2 of code
9+
line 3 of code
10+
11+
12+
Block code "fences"
13+
14+
\`\`\`
15+
Sample text here...
16+
\`\`\`
17+
18+
Syntax highlighting
19+
20+
\`\`\` js
21+
var foo = function (bar) {
22+
return bar++;
23+
};
24+
25+
console.log(foo(5));
26+
\`\`\`
27+
`;
28+
29+
export default markdownText;

lib/renderRules.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,40 @@ const renderRules = {
108108
<View key={node.key} style={styles.blockquote}>
109109
{children}
110110
</View>,
111-
code: (node, children, parent, styles) =>
112-
<View key={node.key} style={styles.code}>
113-
{children}
114-
</View>,
111+
code: (node, children, parent, styles) => {
112+
switch (node.sourceType) {
113+
case 'code_inline': {
114+
return (
115+
<Text key={node.key} style={styles.codeInline}>
116+
{node.content}
117+
</Text>
118+
);
119+
break;
120+
}
121+
case 'code_block': {
122+
return (
123+
<Text key={node.key} style={styles.codeBlock}>
124+
{node.content}
125+
</Text>
126+
);
127+
break;
128+
}
129+
case 'fence': {
130+
return (
131+
<Text key={node.key} style={styles.codeBlock}>
132+
{node.content}
133+
</Text>
134+
);
135+
break;
136+
}
137+
}
138+
139+
return (
140+
<View key={node.key} style={styles.codeInline}>
141+
{children}
142+
</View>
143+
);
144+
},
115145
pre: (node, children, parent, styles) =>
116146
<View key={node.key} style={styles.pre}>
117147
{children}

lib/styles.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@ import { StyleSheet } from 'react-native';
66
export const styles = StyleSheet.create({
77
view: {},
88
codeBlock: {
9-
fontFamily: 'Courier',
10-
fontWeight: '500',
9+
borderWidth: 1,
10+
borderColor: '#CCCCCC',
11+
backgroundColor: '#f5f5f5',
12+
padding: 10,
13+
borderRadius: 4,
14+
},
15+
codeInline: {
16+
borderWidth: 1,
17+
borderColor: '#CCCCCC',
18+
backgroundColor: '#f5f5f5',
19+
padding: 10,
20+
borderRadius: 4,
1121
},
1222
del: {
1323
backgroundColor: '#000000',

lib/util/tokensToAST.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function createNode(token, tokenIndex) {
2929

3030
return {
3131
type,
32+
sourceType: token.type,
3233
key: getUniqueID(),
3334
content,
3435
tokenIndex,

0 commit comments

Comments
 (0)