verdnatura-chat/app/containers/markdown/Preview.tsx

45 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-02-28 18:00:47 +00:00
import React from 'react';
2022-03-29 18:53:10 +00:00
import { Text, TextStyle } from 'react-native';
2022-02-28 18:00:47 +00:00
import removeMarkdown from 'remove-markdown';
import shortnameToUnicode from '../../utils/shortnameToUnicode';
2022-04-28 18:45:00 +00:00
import { themes } from '../../lib/constants';
2022-02-28 18:00:47 +00:00
import { formatText } from './formatText';
import { useTheme } from '../../theme';
import styles from './styles';
import { formatHyperlink } from './formatHyperlink';
interface IMarkdownPreview {
msg?: string;
numberOfLines?: number;
testID?: string;
2022-03-29 18:53:10 +00:00
style?: TextStyle[];
2022-02-28 18:00:47 +00:00
}
2022-03-29 18:53:10 +00:00
const MarkdownPreview = ({ msg, numberOfLines = 1, testID, style = [] }: IMarkdownPreview) => {
2022-04-28 18:45:00 +00:00
const { theme } = useTheme();
2022-02-28 18:00:47 +00:00
if (!msg) {
return null;
}
let m = formatText(msg);
m = formatHyperlink(m);
m = shortnameToUnicode(m);
// Removes sequential empty spaces
m = m.replace(/\s+/g, ' ');
m = removeMarkdown(m);
m = m.replace(/\n+/g, ' ');
return (
<Text
accessibilityLabel={m}
style={[styles.text, { color: themes[theme].bodyText }, ...style]}
numberOfLines={numberOfLines}
testID={testID}>
{m}
</Text>
);
};
export default MarkdownPreview;