File tree Expand file tree Collapse file tree 5 files changed +78
-7
lines changed
Expand file tree Collapse file tree 5 files changed +78
-7
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import Markdown, {
88} from 'react-native-markdown-renderer' ;
99import markdownItCheckbox from 'markdown-it-checkbox' ;
1010import copyAll from './src/copyAll' ;
11+ import code from './src/code' ;
1112import customMarkdownStyle from './src/customMarkdownStyle' ;
1213import copyAllCheckboxPlugin from "./src/copyAllCheckboxPlugin" ;
1314import 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 } /> ;
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change @@ -6,8 +6,18 @@ import { StyleSheet } from 'react-native';
66export 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' ,
Original file line number Diff line number Diff 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,
You can’t perform that action at this time.
0 commit comments