Skip to content

Commit

Permalink
Merge pull request #53 from gmsgowtham/simplify/style-typings
Browse files Browse the repository at this point in the history
fix(minor): remove custom style types and simplify style typings
  • Loading branch information
gmsgowtham authored Oct 14, 2022
2 parents 07d6f90 + c5c2469 commit 713d4f1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
12 changes: 8 additions & 4 deletions src/lib/Parser.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { ReactNode } from 'react';
import type { TextStyle, ViewStyle, ImageStyle } from 'react-native';
import type { marked } from 'marked';
import { decode } from 'entities';
import Renderer from './Renderer';
import type { MarkedStyles } from '../theme/types';
import type { CustomStyleProp, ParserOptions, TextStyleProp } from './types';
import type { ParserOptions } from './types';

class Parser {
private renderer;
private styles: MarkedStyles;
private headingStylesMap: Record<number, TextStyleProp>;
private headingStylesMap: Record<number, TextStyle | undefined>;
constructor(options: ParserOptions) {
this.styles = { ...options.styles };
this.renderer = new Renderer();
Expand Down Expand Up @@ -96,7 +97,10 @@ class Parser {
return elements.filter((element) => element !== null);
}

parseInline(tokens: marked.Token[], styles?: CustomStyleProp) {
parseInline(
tokens: marked.Token[],
styles?: ViewStyle | TextStyle | ImageStyle
) {
const elements: ReactNode[] = tokens.map((token) => {
if (!token) return null;

Expand Down Expand Up @@ -187,7 +191,7 @@ class Parser {

private getNormalizedSiblingNodesForBlockAndInlineTokens = (
tokens: marked.Token[],
textStyle: TextStyleProp
textStyle?: TextStyle
): ReactNode[] => {
let tokenRenderQueue: marked.Token[] = [];
const siblingNodes: ReactNode[] = [];
Expand Down
26 changes: 16 additions & 10 deletions src/lib/Renderer.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import React, { ReactNode } from 'react';
import { ScrollView, View, Text, TouchableHighlight } from 'react-native';
import {
ScrollView,
View,
Text,
TouchableHighlight,
TextStyle,
ViewStyle,
} from 'react-native';
import MarkedList from '@jsamr/react-native-li';
import Disc from '@jsamr/counter-style/presets/disc';
import Decimal from '@jsamr/counter-style/presets/decimal';
import MDImage from './../components/MDImage';
import { generateRandomString } from '../utils/string';
import type { TextStyleProp, ViewStyleProp } from './types';
import { onLinkPress } from '../utils/handlers';

class Renderer {
getTextNode = (children: string | ReactNode[], styles: TextStyleProp) => {
getTextNode = (children: string | ReactNode[], styles?: TextStyle) => {
return (
<Text key={generateRandomString()} style={styles}>
{children}
Expand All @@ -20,7 +26,7 @@ class Renderer {
getTextLinkNode = (
children: string | ReactNode[],
href: string,
styles: TextStyleProp
styles?: TextStyle
) => {
return (
<Text
Expand Down Expand Up @@ -49,7 +55,7 @@ class Renderer {
);
};

getViewNode(children: ReactNode[] | null, styles: ViewStyleProp) {
getViewNode(children: ReactNode[] | null, styles?: ViewStyle) {
return (
<View key={generateRandomString()} style={styles}>
{children}
Expand All @@ -59,8 +65,8 @@ class Renderer {

getCodeBlockNode(
text: string,
containerStyle: ViewStyleProp,
textStyle: TextStyleProp
containerStyle?: ViewStyle,
textStyle?: TextStyle
) {
return (
<ScrollView
Expand All @@ -73,7 +79,7 @@ class Renderer {
);
}

getBlockquoteNode(children: ReactNode[], styles: ViewStyleProp) {
getBlockquoteNode(children: ReactNode[], styles?: ViewStyle) {
return (
<View key={generateRandomString()} style={styles}>
{children}
Expand All @@ -88,8 +94,8 @@ class Renderer {
getListNode(
ordered: boolean,
li: ReactNode[],
listStyle: ViewStyleProp,
textStyle: TextStyleProp
listStyle?: ViewStyle,
textStyle?: TextStyle
) {
return (
<MarkedList
Expand Down
11 changes: 1 addition & 10 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import type { ReactNode } from 'react';
import type {
ImageStyle,
TextStyle,
ViewStyle,
FlatListProps,
} from 'react-native';
import type { FlatListProps } from 'react-native';
import type { MarkedStyles } from './../theme/types';

export type TextStyleProp = TextStyle | undefined;
export type ViewStyleProp = ViewStyle | undefined;
export type CustomStyleProp = ViewStyle | TextStyle | ImageStyle | undefined;

export interface ParserOptions {
styles?: MarkedStyles;
}
Expand Down

0 comments on commit 713d4f1

Please sign in to comment.