Skip to content

Commit 2e5589d

Browse files
committed
fixed issue with images not linking. #39
1 parent c6dd684 commit 2e5589d

File tree

9 files changed

+86
-12
lines changed

9 files changed

+86
-12
lines changed

example/react-native-markdown-renderer/lib/AstRenderer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import React, { Component, PropTypes } from "react";
22
import { Text, View } from "react-native";
33
import getUniqueID from "./util/getUniqueID";
44

5-
export function rootRenderRule(children) {
6-
return <View key={getUniqueID()}>{children}</View>;
5+
export function rootRenderRule(children, styles) {
6+
return <View key={getUniqueID()} styles={styles.root}>{children}</View>;
77
}
88

99
/**
@@ -66,6 +66,6 @@ export default class AstRenderer {
6666
*/
6767
render = nodes => {
6868
const children = nodes.map(value => this.renderNode(value, []));
69-
return rootRenderRule(children);
69+
return rootRenderRule(children, this._style);
7070
};
7171
}

example/react-native-markdown-renderer/lib/parser.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,35 @@ import groupTextTokens from './util/groupTextTokens';
1313
* @return {View}
1414
*/
1515
export function parser(source, renderer, markdownIt) {
16-
1716
let tokens = stringToTokens(source, markdownIt);
17+
18+
tokens = tokens.reduce((acc, curr) => {
19+
if (curr.children && curr.children.length > 0) {
20+
console.log(curr);
21+
// acc.push({
22+
// ...curr,
23+
// children: null,
24+
// nesting: 1,
25+
// });
26+
27+
while (curr.children.length) {
28+
acc.push(curr.children.shift());
29+
}
30+
31+
// acc.push({
32+
// ...curr,
33+
// children: null,
34+
// nesting: -1,
35+
// });
36+
} else {
37+
acc.push(curr);
38+
}
39+
40+
return acc;
41+
}, []);
42+
43+
console.log(tokens);
44+
1845
tokens = cleanupTokens(tokens);
1946
tokens = groupTextTokens(tokens);
2047

example/react-native-markdown-renderer/lib/renderRules.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ const renderRules = {
2929
},
3030

3131
text: (node, children, parent, styles) => {
32-
console.log('text', node.content);
33-
console.log('parent', parent);
3432
return <Text key={node.key}>{node.content}</Text>;
3533
},
3634
span: (node, children, parent, styles) => {

example/react-native-markdown-renderer/lib/styles.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import PlatformEnum from './data/PlatformEnum';
55
*
66
*/
77
export const styles = StyleSheet.create({
8+
root: {},
89
view: {},
910
codeBlock: {
1011
borderWidth: 1,

example/react-native-markdown-renderer/lib/util/cleanupTokens.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import getIsTextType from './getIsTextType';
44

55
export function cleanupTokens(tokens) {
66
tokens = removeInlineTokens(tokens);
7-
tokens.forEach(token => (token.type = getTokenTypeByToken(token)));
7+
tokens.forEach(token => {
8+
token.type = getTokenTypeByToken(token);
9+
10+
if (token.type === 'image') {
11+
token.block = true;
12+
}
13+
});
814

915
/**
1016
* changing a link token to a blocklink to fix issue where link tokens with
@@ -17,7 +23,9 @@ export function cleanupTokens(tokens) {
1723
} else if (stack.length > 0 && token.type === 'link' && token.nesting === -1) {
1824
if (stack.some(stackToken => stackToken.block)) {
1925
stack[0].type = 'blocklink';
26+
stack[0].block = true;
2027
token.type = 'blocklink';
28+
token.block = true;
2129
}
2230

2331
stack.push(token);

example/src/copy/linkedimg.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11

22
const copy = `
3+
4+
[![Minion](https://octodex.github.com/images/minion.png)](https://google.com)
5+
[![Stormtroopocat](https://octodex.github.com/images/stormtroopocat.png)](https://google.com)
6+
7+
## Links
8+
9+
`;
10+
const copy2 = `
311
**tes
412
t**
513
@@ -30,5 +38,4 @@ With a reference later in the document defining the URL location:
3038
3139
[id]: https://octodex.github.com/images/dojocat.jpg "The Dojocat"
3240
`;
33-
3441
export default copy;

src/lib/parser.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,35 @@ import groupTextTokens from './util/groupTextTokens';
1313
* @return {View}
1414
*/
1515
export function parser(source, renderer, markdownIt) {
16-
1716
let tokens = stringToTokens(source, markdownIt);
17+
18+
tokens = tokens.reduce((acc, curr) => {
19+
if (curr.children && curr.children.length > 0) {
20+
console.log(curr);
21+
// acc.push({
22+
// ...curr,
23+
// children: null,
24+
// nesting: 1,
25+
// });
26+
27+
while (curr.children.length) {
28+
acc.push(curr.children.shift());
29+
}
30+
31+
// acc.push({
32+
// ...curr,
33+
// children: null,
34+
// nesting: -1,
35+
// });
36+
} else {
37+
acc.push(curr);
38+
}
39+
40+
return acc;
41+
}, []);
42+
43+
console.log(tokens);
44+
1845
tokens = cleanupTokens(tokens);
1946
tokens = groupTextTokens(tokens);
2047

src/lib/renderRules.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ const renderRules = {
2929
},
3030

3131
text: (node, children, parent, styles) => {
32-
console.log('text', node.content);
33-
console.log('parent', parent);
3432
return <Text key={node.key}>{node.content}</Text>;
3533
},
3634
span: (node, children, parent, styles) => {

src/lib/util/cleanupTokens.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import getIsTextType from './getIsTextType';
44

55
export function cleanupTokens(tokens) {
66
tokens = removeInlineTokens(tokens);
7-
tokens.forEach(token => (token.type = getTokenTypeByToken(token)));
7+
tokens.forEach(token => {
8+
token.type = getTokenTypeByToken(token);
9+
10+
if (token.type === 'image') {
11+
token.block = true;
12+
}
13+
});
814

915
/**
1016
* changing a link token to a blocklink to fix issue where link tokens with
@@ -17,7 +23,9 @@ export function cleanupTokens(tokens) {
1723
} else if (stack.length > 0 && token.type === 'link' && token.nesting === -1) {
1824
if (stack.some(stackToken => stackToken.block)) {
1925
stack[0].type = 'blocklink';
26+
stack[0].block = true;
2027
token.type = 'blocklink';
28+
token.block = true;
2129
}
2230

2331
stack.push(token);

0 commit comments

Comments
 (0)