Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Inder Dhir committed Aug 28, 2021
1 parent e143043 commit 7945541
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 37 deletions.
50 changes: 20 additions & 30 deletions src/lib/renderRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ import hasParents from './util/hasParents';

import textStyleProps from './data/textStyleProps';

function renderText(node, inheritedStyles, styles) {
// we trim new lines off the end of code blocks because the parser sends an extra one.
let {content} = node;

if (
typeof node.content === 'string' &&
node.content.charAt(node.content.length - 1) === '\n'
) {
content = node.content.substring(0, node.content.length - 1);
}

return (
<Text key={node.key} style={[inheritedStyles, styles.code_block]}>
{content}
</Text>
);
}

const renderRules = {
// when unknown elements are introduced, so it wont break
unknown: (node, children, parent, styles) => null,
Expand Down Expand Up @@ -177,38 +195,10 @@ const renderRules = {
</Text>
),
code_block: (node, children, parent, styles, inheritedStyles = {}) => {
// we trim new lines off the end of code blocks because the parser sends an extra one.
let {content} = node;

if (
typeof node.content === 'string' &&
node.content.charAt(node.content.length - 1) === '\n'
) {
content = node.content.substring(0, node.content.length - 1);
}

return (
<Text key={node.key} style={[inheritedStyles, styles.code_block]}>
{content}
</Text>
);
return renderText(node, inheritedStyles, styles);
},
fence: (node, children, parent, styles, inheritedStyles = {}) => {
// we trim new lines off the end of code blocks because the parser sends an extra one.
let {content} = node;

if (
typeof node.content === 'string' &&
node.content.charAt(node.content.length - 1) === '\n'
) {
content = node.content.substring(0, node.content.length - 1);
}

return (
<Text key={node.key} style={[inheritedStyles, styles.fence]}>
{content}
</Text>
);
return renderText(node, inheritedStyles, styles);
},

// Tables
Expand Down
2 changes: 1 addition & 1 deletion src/lib/util/cleanupTokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function cleanupTokens(tokens) {
* nested non text tokens breaks component
*/
const stack = [];
tokens = tokens.reduce((acc, token, index) => {
tokens = tokens.reduce((acc, token) => {
if (token.type === 'link' && token.nesting === 1) {
stack.push(token);
} else if (
Expand Down
4 changes: 1 addition & 3 deletions src/lib/util/convertAdditionalStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,5 @@ export default function convertAdditionalStyles(style) {
return x != null;
});

const conv = cssToReactNative(tuples);

return conv;
return cssToReactNative(tuples);
}
2 changes: 1 addition & 1 deletion src/lib/util/groupTextTokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function groupTextTokens(tokens) {

let hasGroup = false;

tokens.forEach((token, index) => {
tokens.forEach((token) => {
if (!token.block && !hasGroup) {
hasGroup = true;
result.push(new Token('textgroup', 1));
Expand Down
4 changes: 2 additions & 2 deletions src/lib/util/renderInlineAsText.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default function renderInlineAsText(tokens) {
var result = '';
let result = '';

for (var i = 0, len = tokens.length; i < len; i++) {
for (let i = 0, len = tokens.length; i < len; i++) {
if (tokens[i].type === 'text') {
result += tokens[i].content;
} else if (tokens[i].type === 'image') {
Expand Down

0 comments on commit 7945541

Please sign in to comment.