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

fix(tickets): properly format the text in the tickets and make the links usable #533

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
"hour": "Hour",
"hours": "Hour",
"icon": "Icon",
"image": "Image",
"inYourAgenda": "in your agenda",
"it": "Italian",
"language": "Language",
Expand Down
1 change: 1 addition & 0 deletions assets/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
"hour": "Orario",
"hours": "Ore",
"icon": "Icona",
"image": "Immagine",
"inYourAgenda": "nella tua agenda",
"it": "Italiano",
"language": "Lingua",
Expand Down
4 changes: 2 additions & 2 deletions src/core/components/TextWithLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PropsWithChildren } from 'react';
import { TextProps } from 'react-native';
import { MixedStyleDeclaration } from 'react-native-render-html';

import { linkUrls } from '../../utils/html';
import { linkUrls, replaceImgWithAnchorTags } from '../../utils/html';
import { HtmlView } from './HtmlView';

type Props = {
Expand All @@ -11,7 +11,7 @@ type Props = {

export const TextWithLinks = ({ baseStyle, children, style }: Props) => {
if (!children || typeof children !== 'string') return null;
const html = linkUrls(children);
const html = linkUrls(replaceImgWithAnchorTags(children));
return (
<HtmlView
source={{ html }}
Expand Down
4 changes: 2 additions & 2 deletions src/features/tickets/components/TextMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useStylesheet } from '@lib/ui/hooks/useStylesheet';
import { Theme } from '@lib/ui/types/Theme';

import { TextWithLinks } from '../../../core/components/TextWithLinks';
import { getHtmlTextContent } from '../../../utils/html';
import { sanitizeHtml } from '../../../utils/html';

export interface TextMessageProps {
message: string;
Expand All @@ -15,7 +15,7 @@ export const TextMessage = ({ message }: TextMessageProps) => {
const styles = useStylesheet(createStyles);

const textMessage = useMemo(() => {
return getHtmlTextContent(message);
return sanitizeHtml(message ?? '');
}, [message]);

return <TextWithLinks style={styles.text}>{textMessage}</TextWithLinks>;
Expand Down
10 changes: 9 additions & 1 deletion src/utils/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Document } from 'react-native-render-html';

import { innerText } from 'domutils';
import { parseDocument } from 'htmlparser2';
import i18next from 'i18next';

export const sanitizeHtml = (html: string) =>
html.replace(/\r/g, '').replace(/\\r/g, '').replace(/\\"/g, '"').trim();
Expand All @@ -11,9 +12,16 @@ export const getHtmlTextContent = (text: string) => {
return innerText(dom.children as any[]);
};

export const replaceImgWithAnchorTags = (html: string) => {
const imgRegex = /<img[^>]*src="([^">]*)"[^>]*>/gi;
return html.replace(imgRegex, (_, src) => {
return `<a href="${src}">${i18next.t('common.image')}</a>`;
});
};

export const linkUrls = (html: string) => {
const regex =
/(?!<a[^>]*>[^<])(?:https?:\/\/|www\.)(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$])(?![^<]*<\/a>)/gi;
/(?!<a[^>]*>[^<])(?:https?:\/\/(?:www\.)?|www\.)?(?:[A-Z0-9-]+(?:[-.][A-Z0-9]+)*\.[A-Z]{2,5})(?::[0-9]{1,5})?(?:\/[A-Z0-9\-._~:/?#[\]@!$&'()*+,;=]*)?(?:\?[A-Z0-9\-._~:/?#[\]@!$&'()*+,;=%]*)?(?:#[A-Z0-9\-._~:/?#[\]@!$&'()*+,;=%]*)?(?![^<]*<\/a>)(?<!\.)/gi;
return html.replace(regex, match => {
if (!match.startsWith('http')) match = `https://${match}`;
return `<a href="${match}">${match}</a>`;
Expand Down