Skip to content

Commit 51b07b7

Browse files
authored
Merge pull request #28 from iamacup/text-style-fixes
Text style fixes
2 parents ae9740c + 57d7eb9 commit 51b07b7

21 files changed

+287
-111
lines changed

.eslintrc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
root: true,
3+
extends: '@react-native-community',
4+
settings: {
5+
react: {
6+
version: require('./package.json').peerDependencies.react,
7+
},
8+
}
9+
};

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/node_modules
22
/.idea
33
package-lock.json
4-
/.DS_Store
4+
/.DS_Store
5+
yarn-error.lock
6+
yarn.lock
7+
.eslintcache

.prettierrc

Lines changed: 0 additions & 7 deletions
This file was deleted.

.prettierrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
bracketSpacing: false,
3+
jsxBracketSameLine: true,
4+
singleQuote: true,
5+
trailingComma: 'all',
6+
};

package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
{
22
"name": "react-native-markdown-display",
3-
"version": "4.0.2",
3+
"version": "4.0.3",
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",
7-
"scripts": {},
7+
"scripts": {
8+
"lint": "./node_modules/.bin/eslint --fix --cache ./src"
9+
},
10+
"pre-commit": [
11+
"lint"
12+
],
813
"repository": {
914
"type": "git",
1015
"url": "git+https://github.com/iamacup/react-native-markdown-display.git"
@@ -36,6 +41,11 @@
3641
},
3742
"devDependencies": {
3843
"chokidar": "^3.3.0",
39-
"fs-extra": "^8.1.0"
44+
"fs-extra": "^8.1.0",
45+
"@babel/core": "^7.6.4",
46+
"@babel/runtime": "^7.6.3",
47+
"@react-native-community/eslint-config": "^0.0.5",
48+
"eslint": "^6.6.0",
49+
"pre-commit": "1.2.2"
4050
}
4151
}

src/index.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Base Markdown component
33
* @author Mient-jan Stelling
44
*/
5-
import { useMemo } from 'react';
5+
import {useMemo} from 'react';
66
import PropTypes from 'prop-types';
77
import parser from './lib/parser';
88
import applyStyle from './lib/util/applyStyle';
@@ -15,8 +15,8 @@ import AstRenderer from './lib/AstRenderer';
1515
import MarkdownIt from 'markdown-it';
1616
import PluginContainer from './lib/plugin/PluginContainer';
1717
import blockPlugin from './lib/plugin/blockPlugin';
18-
import { styles } from './lib/styles';
19-
import { stringToTokens } from './lib/util/stringToTokens';
18+
import {styles} from './lib/styles';
19+
import {stringToTokens} from './lib/util/stringToTokens';
2020
/**
2121
*
2222
*/
@@ -48,13 +48,13 @@ const getCopyFromChildren = children => {
4848
const getRenderer = (renderer, rules, style, onLinkPress) => {
4949
if (renderer && rules) {
5050
console.warn(
51-
'react-native-markdown-display you are using renderer and rules at the same time. This is not possible, props.rules is ignored'
51+
'react-native-markdown-display you are using renderer and rules at the same time. This is not possible, props.rules is ignored',
5252
);
5353
}
5454

5555
if (renderer && style) {
5656
console.warn(
57-
'react-native-markdown-display you are using renderer and style at the same time. This is not possible, props.style is ignored'
57+
'react-native-markdown-display you are using renderer and style at the same time. This is not possible, props.style is ignored',
5858
);
5959
}
6060

@@ -63,7 +63,9 @@ const getRenderer = (renderer, rules, style, onLinkPress) => {
6363
if (!(typeof renderer === 'function') || renderer instanceof AstRenderer) {
6464
return renderer;
6565
} else {
66-
throw new Error('Provided renderer is not compatible with function or AstRenderer. please change');
66+
throw new Error(
67+
'Provided renderer is not compatible with function or AstRenderer. please change',
68+
);
6769
}
6870
} else {
6971
return new AstRenderer(
@@ -75,7 +77,7 @@ const getRenderer = (renderer, rules, style, onLinkPress) => {
7577
...styles,
7678
...style,
7779
},
78-
onLinkPress
80+
onLinkPress,
7981
);
8082
}
8183
};
@@ -105,8 +107,14 @@ const Markdown = ({
105107
}),
106108
onLinkPress = () => {},
107109
}) => {
108-
const momoizedRenderer = useMemo(() => getRenderer(renderer, rules, style, onLinkPress), [renderer, rules, style]);
109-
const markdownParser = useMemo(() => getMarkdownParser(markdownit, plugins), [markdownit, plugins]);
110+
const momoizedRenderer = useMemo(
111+
() => getRenderer(renderer, rules, style, onLinkPress),
112+
[onLinkPress, renderer, rules, style],
113+
);
114+
const markdownParser = useMemo(() => getMarkdownParser(markdownit, plugins), [
115+
markdownit,
116+
plugins,
117+
]);
110118

111119
const copy = (this.copy = getCopyFromChildren(children));
112120
return parser(copy, momoizedRenderer.render, markdownParser);
@@ -117,7 +125,10 @@ const Markdown = ({
117125
*/
118126
Markdown.propTypes = {
119127
children: PropTypes.node.isRequired,
120-
renderer: PropTypes.oneOfType([PropTypes.func, PropTypes.instanceOf(AstRenderer)]),
128+
renderer: PropTypes.oneOfType([
129+
PropTypes.func,
130+
PropTypes.instanceOf(AstRenderer),
131+
]),
121132
onLinkPress: PropTypes.func,
122133
rules: (props, propName, componentName) => {
123134
let invalidProps = [];
@@ -128,17 +139,19 @@ Markdown.propTypes = {
128139
}
129140

130141
if (typeof prop === 'object') {
131-
invalidProps = Object.keys(prop).filter(key => typeof prop[key] !== 'function');
142+
invalidProps = Object.keys(prop).filter(
143+
key => typeof prop[key] !== 'function',
144+
);
132145
}
133146

134147
if (typeof prop !== 'object') {
135148
return new Error(
136-
`Invalid prop \`${propName}\` supplied to \`${componentName}\`. Must be of shape {[index:string]:function} `
149+
`Invalid prop \`${propName}\` supplied to \`${componentName}\`. Must be of shape {[index:string]:function} `,
137150
);
138151
} else if (invalidProps.length > 0) {
139152
return new Error(
140153
`Invalid prop \`${propName}\` supplied to \`${componentName}\`. These ` +
141-
`props are not of type function \`${invalidProps.join(', ')}\` `
154+
`props are not of type function \`${invalidProps.join(', ')}\` `,
142155
);
143156
}
144157
},

src/lib/AstRenderer.js

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import React, { Component, PropTypes } from "react";
2-
import { Text, View } from "react-native";
3-
import getUniqueID from "./util/getUniqueID";
1+
import getUniqueID from './util/getUniqueID';
42

53
/**
64
*
@@ -15,6 +13,29 @@ export default class AstRenderer {
1513
this._renderRules = renderRules;
1614
this._style = style;
1715
this._onLinkPress = onLinkPress;
16+
17+
// this is all the style props that are unique to <Text> components as of 07/Nov/2019
18+
this._textStyleProps = [
19+
'textShadowOffset',
20+
'color',
21+
'fontSize',
22+
'fontStyle',
23+
'fontWeight',
24+
'lineHeight',
25+
'textAlign',
26+
'textDecorationLine',
27+
'textShadowColor',
28+
'fontFamily',
29+
'textShadowRadius',
30+
'includeFontPadding',
31+
'textAlignVertical',
32+
'fontVariant',
33+
'letterSpacing',
34+
'textDecorationColor',
35+
'textDecorationStyle',
36+
'textTransform',
37+
'writingDirection',
38+
];
1839
}
1940

2041
/**
@@ -27,7 +48,7 @@ export default class AstRenderer {
2748

2849
if (!renderFunction) {
2950
throw new Error(
30-
`${type} renderRule not defined example: <Markdown rules={renderRules}>`
51+
`${type} renderRule not defined example: <Markdown rules={renderRules}>`,
3152
);
3253
}
3354
return renderFunction;
@@ -45,16 +66,39 @@ export default class AstRenderer {
4566
const parents = [...parentNodes];
4667
parents.unshift(node);
4768

48-
if (node.type === "text") {
49-
return renderFunction(node, [], parentNodes, this._style);
69+
if (node.type === 'text') {
70+
// we build up a style object for text types, this effectively grabs the styles from parents and
71+
// applies them in order of priority parent (most) to child (least) priority
72+
// so that if we overwride the text style, it does not overwrite a header1 style, for instance.
73+
const styleObj = {};
74+
75+
for (let a = 0; a < parentNodes.length; a++) {
76+
if (this._style[parentNodes[a].type]) {
77+
const arr = Object.keys(this._style[parentNodes[a].type]);
78+
79+
for (let b = 0; b < arr.length; b++) {
80+
if (this._textStyleProps.includes(arr[b])) {
81+
styleObj[arr[b]] = this._style[parentNodes[a].type][arr[b]];
82+
}
83+
}
84+
}
85+
}
86+
87+
return renderFunction(node, [], parentNodes, this._style, styleObj);
5088
}
5189

5290
const children = node.children.map(value => {
5391
return this.renderNode(value, parents);
5492
});
5593

56-
if (node.type === "link" || node.type === "blocklink") {
57-
return renderFunction(node, children, parentNodes, this._style, this._onLinkPress);
94+
if (node.type === 'link' || node.type === 'blocklink') {
95+
return renderFunction(
96+
node,
97+
children,
98+
parentNodes,
99+
this._style,
100+
this._onLinkPress,
101+
);
58102
}
59103

60104
return renderFunction(node, children, parentNodes, this._style);
@@ -67,7 +111,7 @@ export default class AstRenderer {
67111
*/
68112
render = nodes => {
69113
const children = nodes.map(value => this.renderNode(value, []));
70-
const root = { type: "root", key: getUniqueID() };
114+
const root = {type: 'root', key: getUniqueID()};
71115
return this.getRenderFunction(root.type)(root, children, null, this._style);
72116
};
73117
}

src/lib/data/PlatformEnum.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export default {
2-
IOS: "ios",
3-
ANDROID: "android"
2+
IOS: 'ios',
3+
ANDROID: 'android',
44
};

src/lib/parser.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import React from 'react';
2-
import { View } from 'react-native';
31
import tokensToAST from './util/tokensToAST';
4-
import { stringToTokens } from './util/stringToTokens';
5-
import { cleanupTokens } from './util/cleanupTokens';
2+
import {stringToTokens} from './util/stringToTokens';
3+
import {cleanupTokens} from './util/cleanupTokens';
64
import groupTextTokens from './util/groupTextTokens';
75

86
/**

0 commit comments

Comments
 (0)