Skip to content

Commit 39c8ba5

Browse files
author
Luke Brandon Farrell
committed
Fixed asterix bold and italics and added pre-commit hooks
1 parent 7341d4c commit 39c8ba5

5 files changed

Lines changed: 1560 additions & 38 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ The `<SmartText />` component contains a number of abilities:
2020

2121
1. Emoji 😄🥶🤯 it uses [node-emoji](https://www.npmjs.com/package/node-emoji) under the hood
2222
2. Markdown support for *italics* and **bold**
23-
3. Nested text support e.g. `<SmartText size={12} color='black'>My nested <SmartText bold>test!</SmartText>` where parent props will be inherited by children.
23+
3. Nested text support e.g. `<SmartText size={12} color='black'>My nested <SmartText bold>test!</SmartText>` where parent props will be inherited by children.
2424

2525
## Light Text
2626

2727
The `<LightText />` componnet takes all the same props as `<SmartText />` but has no abilities! This component can be used
2828
to enjoy all the benefits of easy styling and positioning without any of the extra code or processing which comes with `<SmartText />`.
2929

30+
- You can also nest `LightText` in a `SmartText`: `<SmartText size={12} color='black'>My nested <LightText bold>test!</LightText>`
31+
3032
## Props
3133

3234
| Prop | Type | Optional |

lib/SmartText.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ var __rest = (this && this.__rest) || function (s, e) {
2525
return t;
2626
};
2727
/* NPM - Node Package Manage */
28-
import React from 'react';
29-
import { Text as RNText } from 'react-native';
30-
import PropTypes from 'prop-types';
31-
import _map from 'lodash.map';
3228
import _get from 'lodash.get';
3329
import _isString from 'lodash.isstring';
30+
import _map from 'lodash.map';
31+
import _flatten from 'lodash.flatten';
3432
import Emoji from 'node-emoji';
33+
import PropTypes from 'prop-types';
34+
import React from 'react';
35+
import { Text as RNText } from 'react-native';
36+
/** Components */
37+
import LightText from './LightText';
3538
var SmartText = function (_a) {
3639
var items = _a.children, size = _a.size, color = _a.color, bold = _a.bold, italic = _a.italic, underline = _a.underline, strikethrough = _a.strikethrough, align = _a.align, lineHeight = _a.lineHeight, family = _a.family, opacity = _a.opacity, style = _a.style, _b = _a.m, m = _b === void 0 ? 0 : _b, _c = _a.mh, mh = _c === void 0 ? 0 : _c, _d = _a.mv, mv = _d === void 0 ? 0 : _d, _e = _a.mt, mt = _e === void 0 ? 0 : _e, _f = _a.mb, mb = _f === void 0 ? 0 : _f, _g = _a.ml, ml = _g === void 0 ? 0 : _g, _h = _a.mr, mr = _h === void 0 ? 0 : _h, _j = _a.p, p = _j === void 0 ? 0 : _j, _k = _a.ph, ph = _k === void 0 ? 0 : _k, _l = _a.pv, pv = _l === void 0 ? 0 : _l, _m = _a.pt, pt = _m === void 0 ? 0 : _m, _o = _a.pb, pb = _o === void 0 ? 0 : _o, _p = _a.pl, pl = _p === void 0 ? 0 : _p, _q = _a.pr, pr = _q === void 0 ? 0 : _q, props = __rest(_a, ["children", "size", "color", "bold", "italic", "underline", "strikethrough", "align", "lineHeight", "family", "opacity", "style", "m", "mh", "mv", "mt", "mb", "ml", "mr", "p", "ph", "pv", "pt", "pb", "pl", "pr"]);
3740
var smartChildren = items;
@@ -40,11 +43,7 @@ var SmartText = function (_a) {
4043
*
4144
* *italics* and **bold** supported
4245
*/
43-
// TODO : This currently breaks nesting text components
44-
// smartChildren = mapChildrenToTransformer(
45-
// smartChildren,
46-
// parseAsteriskInMarkdownString
47-
// );
46+
smartChildren = mapChildrenToTransformer(smartChildren, parseAsteriskInMarkdownString);
4847
/**
4948
* Maps children to <SmartText /> or <span /> components
5049
*/
@@ -91,12 +90,12 @@ var SmartText = function (_a) {
9190
if (_isString(children)) {
9291
return transform(children);
9392
}
94-
return _map(children, function (child, index) {
93+
return _flatten(_map(children, function (child, index) {
9594
if (_isString(child)) {
9695
return transform(child, index);
9796
}
9897
return child;
99-
});
98+
}));
10099
}
101100
/**
102101
* Maps children and copies props from parent SmartText to any children
@@ -119,7 +118,7 @@ var SmartText = function (_a) {
119118
}
120119
return _map(children, function (child, index) {
121120
var type = _get(child, 'type', null);
122-
if (type === SmartText || type === 'span') {
121+
if (type === SmartText || type === LightText) {
123122
var ownProps = _get(child, 'props', null);
124123
var children_1 = _get(ownProps, 'children', null);
125124
return (React.createElement(SmartText, __assign({ key: index, size: size, color: color, bold: bold, italic: italic, underline: underline, strikethrough: strikethrough, align: align, lineHeight: lineHeight, family: family }, props, ownProps), children_1));
@@ -140,15 +139,15 @@ var SmartText = function (_a) {
140139
// We use (S^t) as a matching pattern as it is a unlikely occurance in any text
141140
markdown = markdown.replace(/\*{2}(.*?)\*{2}/g, function (match, p1) { return "=S^tS^t" + p1 + "S^tS^t="; });
142141
markdown = markdown.replace(/\*{1}(.*?)\*{1}/g, function (match, p1) { return "=S^t" + p1 + "S^t="; });
143-
return markdown.split(/=/g).map(function (item) {
142+
return markdown.split(/=/g).map(function (item, index) {
144143
var text = item.replace(/(S\^t)/g, '');
145144
// **Double** asterisk is bold
146145
if (/(S\^t){2}(.*?)(S\^t){2}/.test(item)) {
147-
return React.createElement("span", { bold: true }, text);
146+
return (React.createElement(LightText, { key: index, bold: true }, text));
148147
// **single** asterisk is italic
149148
}
150149
else if (/(S\^t){1}(.*?)(S\^t){1}/.test(item)) {
151-
return React.createElement("span", { italic: true }, text);
150+
return (React.createElement(LightText, { key: index, italic: true }, text));
152151
}
153152
return text;
154153
});

0 commit comments

Comments
 (0)