Skip to content

Commit c14da38

Browse files
committed
fixed issue where links where blocking rending of the rest of the markdown copy
1 parent e4bee67 commit c14da38

File tree

9 files changed

+330
-19
lines changed

9 files changed

+330
-19
lines changed

example/App.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import React, { Component } from 'react';
7-
import { Platform, Picker, ScrollView, StyleSheet, Text, View, Alert } from 'react-native';
7+
import { Platform, Picker, ScrollView, StyleSheet, Text, View, Dimensions } from 'react-native';
88

99
import Markdown, {
1010
AstRenderer,
@@ -15,10 +15,14 @@ import Markdown, {
1515
} from './react-native-markdown-renderer';
1616
//
1717
import markdownItCheckbox from 'markdown-it-checkbox';
18+
import { TabViewAnimated, SceneMap, TabBar } from 'react-native-tab-view';
19+
1820
import copyAll from './src/copyAll';
1921
import customMarkdownStyle from './src/customMarkdownStyle';
2022
import copyAllCheckboxPlugin from './src/copyAllCheckboxPlugin';
2123
import pluginRules from './src/pluginRules';
24+
import all from './src/copy/all';
25+
import linkedimg from './src/copy/linkedimg';
2226

2327
const instructions = Platform.select({
2428
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
@@ -55,20 +59,25 @@ const renderer = new AstRenderer(
5559
styles
5660
);
5761

62+
const routes = {
63+
all: () => <ScrollView><Markdown children={all} /></ScrollView>,
64+
linkedimg: () => <ScrollView><Markdown children={linkedimg} /></ScrollView>,
65+
};
66+
67+
const initialLayout = {
68+
height: 0,
69+
width: Dimensions.get('window').width,
70+
};
71+
5872
export default class App extends Component {
5973
state = {
60-
view: 0,
74+
index: 0,
75+
routes: [
76+
{ key: 'all', title: 'All Markdown' },
77+
{ key: 'linkedimg', title: 'Linked Images' },
78+
],
6179
};
6280

63-
list = [
64-
{ description: 'default' },
65-
{ description: 'custom renderer' },
66-
{ description: 'custom style sheet' },
67-
{ description: 'custom rules' },
68-
{ description: 'custom rules & styles' },
69-
{ description: 'plugins (checkbox)' },
70-
];
71-
7281
getView(value) {
7382
switch (value) {
7483
case 0: {
@@ -107,7 +116,23 @@ export default class App extends Component {
107116
this.setState({ view: itemIndex });
108117
};
109118

119+
handleIndexChange = index => this.setState({ index });
120+
renderHeader = props => <TabBar {...props} />;
121+
renderScene = SceneMap(routes);
122+
110123
render() {
124+
return (
125+
<TabViewAnimated
126+
navigationState={this.state}
127+
renderScene={this.renderScene}
128+
renderHeader={this.renderHeader}
129+
onIndexChange={this.handleIndexChange}
130+
initialLayout={initialLayout}
131+
/>
132+
);
133+
}
134+
135+
render3() {
111136
let currentView = this.state.view;
112137

113138
return (

example/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"prop-types": "^15.6.1",
1414
"react": "16.3.1",
1515
"react-native": "0.55.2",
16-
"react-native-fit-image": "^1.5.4"
16+
"react-native-fit-image": "^1.5.4",
17+
"react-native-markdown-renderer": "^3.2.0",
18+
"react-native-tab-view": "0.0.77"
1719
},
1820
"devDependencies": {
1921
"babel-jest": "22.4.3",
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// tslint:disable:max-classes-per-file
2+
import { MarkdownIt, Token } from 'markdown-it';
3+
import { ComponentType, ReactNode } from 'react';
4+
import { StyleSheet, View } from 'react-native';
5+
6+
export function getUniqueID(): string;
7+
export function openUrl(url: string): void;
8+
9+
export function hasParents(parents: any[], type: string): boolean;
10+
11+
export type RenderFunction = (
12+
node: any,
13+
children: ReactNode[],
14+
parent: ReactNode,
15+
styles: any,
16+
) => ReactNode;
17+
18+
export interface RenderRules {
19+
[name: string]: RenderFunction;
20+
}
21+
22+
export const renderRules: RenderRules;
23+
24+
export interface MarkdownParser {
25+
parse: (value: string, options: any) => Token[];
26+
}
27+
28+
export interface ASTNode {
29+
type: string;
30+
sourceType: string; // original source token name
31+
key: string;
32+
content: string;
33+
tokenIndex: number;
34+
index: number;
35+
attributes: Record<string, any>;
36+
children: ASTNode[];
37+
}
38+
39+
export class AstRenderer {
40+
constructor(renderRules: RenderRules, style?: any);
41+
getRenderFunction(type: string): RenderFunction;
42+
renderNode(node: any, parentNodes: ReadonlyArray<any>): ReactNode;
43+
render(nodes: ReadonlyArray<any>): View;
44+
}
45+
46+
export function parser(
47+
source: string,
48+
renderer: (node: ASTNode) => View,
49+
parser: MarkdownParser,
50+
): any;
51+
52+
export function stringToTokens(
53+
source: string,
54+
markdownIt: MarkdownParser,
55+
): Token[];
56+
57+
export function tokensToAST(tokens: ReadonlyArray<Token>): ASTNode[];
58+
59+
interface PluginContainerResult<A> extends Array<any> {
60+
0: A;
61+
}
62+
63+
export class PluginContainer<A> {
64+
constructor(plugin: A, ...options: any[]);
65+
toArray(): PluginContainerResult<A>;
66+
}
67+
68+
export function blockPlugin(md: any, name: string, options: object): any;
69+
70+
export const styles: any;
71+
72+
export interface MarkdownProps {
73+
rules?: RenderRules;
74+
style?: StyleSheet.NamedStyles<any>;
75+
renderer?: AstRenderer;
76+
markdownit?: MarkdownIt;
77+
plugins?: Array<PluginContainer<any>>;
78+
}
79+
80+
type MarkdownStatic = React.ComponentType<MarkdownProps>;
81+
export const Markdown: MarkdownStatic;
82+
export type Markdown = MarkdownStatic;
83+
84+
export default Markdown;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export function parser(source, renderer, markdownIt) {
2121
tokens = groupTextTokens(tokens);
2222

2323
const astTree = tokensToAST(tokens);
24-
console.log(astTree);
2524

2625
return renderer(astTree);
2726
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function cleanupTokens(tokens) {
1515
if (token.type === 'link' && token.nesting === 1) {
1616
stack.push(token);
1717
} else if (stack.length > 0 && token.type === 'link' && token.nesting === -1) {
18-
if (stack.findIndex(stackToken => !getIsTextType(stackToken)) > -1) {
18+
if (stack.some(stackToken => !getIsTextType(stackToken.type))) {
1919
stack[0].type = 'blocklink';
2020
token.type = 'blocklink';
2121
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import getTokenTypeByToken from './getTokenTypeByToken';
33

44
/**
55
*
6-
* @param {{type: string, tag:string, content: string, children: *, attrs: Array}} token
6+
* @param {{type: string, tag:string, content: string, children: *, attrs: Array, meta, info}} token
77
* @param {number} tokenIndex
88
* @return {{type: string, content, tokenIndex: *, index: number, attributes: {}, children: *}}
99
*/
@@ -18,13 +18,13 @@ function createNode(token, tokenIndex) {
1818
const [name, value] = curr;
1919
return { ...prev, [name]: value };
2020
}, {});
21-
2221
}
2322

24-
2523
return {
2624
type,
2725
sourceType: token.type,
26+
sourceInfo: token.info,
27+
sourceMeta: token.meta,
2828
key: getUniqueID(),
2929
content,
3030
tokenIndex,
@@ -34,7 +34,6 @@ function createNode(token, tokenIndex) {
3434
};
3535
}
3636

37-
3837
/**
3938
*
4039
* @param {Array<{type: string, tag:string, content: string, children: *, attrs: Array}>}tokens

0 commit comments

Comments
 (0)