Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add markdown component with simple rules for native #67

Merged
merged 15 commits into from
Jul 20, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Forward props in Markdown component to Text component
  • Loading branch information
Ashoat committed Jul 20, 2020
commit 772088e7baac93a1bd2aba3088e6f0d10f24590e
32 changes: 18 additions & 14 deletions native/markdown/markdown.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,30 @@ import { onlyEmojiRegex } from 'lib/shared/emojis';
import { getMarkdownStyles } from './styles';

type Props = {|
...React.ElementConfig<typeof Text>,
style: TextStyle,
useDarkStyle: boolean,
children: string,
rules: MarkdownRules,
|};
function Markdown(props: Props) {
const { useDarkStyle, rules } = props;
const style = React.useMemo(() => {
const { style, useDarkStyle, children, rules, ...rest } = props;

const markdownStyles = React.useMemo(() => {
return getMarkdownStyles(useDarkStyle ? 'dark' : 'light');
}, [useDarkStyle]);
const { simpleMarkdownRules, emojiOnlyFactor } = React.useMemo(
() => rules(style),
[rules, style],
() => rules(markdownStyles),
[rules, markdownStyles],
);

const parser = React.useMemo(
() => SimpleMarkdown.parserFor(simpleMarkdownRules),
[simpleMarkdownRules],
);
const ast = React.useMemo(
() => parser(props.children, { disableAutoBlockNewlines: true }),
[parser, props.children],
() => parser(children, { disableAutoBlockNewlines: true }),
[parser, children],
);

const output = React.useMemo(
Expand All @@ -48,19 +50,17 @@ function Markdown(props: Props) {
if (emojiOnlyFactor === null || emojiOnlyFactor === undefined) {
return false;
}
return onlyEmojiRegex.test(props.children);
}, [emojiOnlyFactor, props.children]);
return onlyEmojiRegex.test(children);
}, [emojiOnlyFactor, children]);
const textStyle = React.useMemo(() => {
if (
!emojiOnly ||
emojiOnlyFactor === null ||
emojiOnlyFactor === undefined
) {
return props.style;
return style;
}
const flattened: FlattenedTextStyle = (StyleSheet.flatten(
props.style,
): any);
const flattened: FlattenedTextStyle = (StyleSheet.flatten(style): any);
invariant(
flattened && typeof flattened === 'object',
`Markdown component should have style`,
Expand All @@ -71,9 +71,13 @@ function Markdown(props: Props) {
`style prop should have fontSize if using emojiOnlyFactor`,
);
return { ...flattened, fontSize: fontSize * emojiOnlyFactor };
}, [emojiOnly, props.style, emojiOnlyFactor]);
}, [emojiOnly, style, emojiOnlyFactor]);

return <Text style={textStyle}>{renderedOutput}</Text>;
return (
<Text style={textStyle} {...rest}>
{renderedOutput}
</Text>
);
}

export default Markdown;