@openpeepshq/rn-components 0.5.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openpeepshq/rn-components",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "React Native community screens and shared UI (extracted from the Black Ambition app) for the OpenPeeps stack.",
|
|
5
5
|
"source": "./src/index.ts",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -140,8 +140,8 @@
|
|
|
140
140
|
"timezones-list": "^3.1.0",
|
|
141
141
|
"validator": "^13.15.23",
|
|
142
142
|
"zustand": "^5.0.3",
|
|
143
|
-
"@openpeepshq/common": "^0.
|
|
144
|
-
"@openpeepshq/react": "^0.
|
|
143
|
+
"@openpeepshq/common": "^0.3.0",
|
|
144
|
+
"@openpeepshq/react": "^0.6.0"
|
|
145
145
|
},
|
|
146
146
|
"devDependencies": {
|
|
147
147
|
"@babel/core": "^7.26.7",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
type
|
|
2
|
+
preprocessMarkdownSource,
|
|
3
|
+
type MarkdownRenderProps,
|
|
4
4
|
} from '@openpeepshq/common/lib';
|
|
5
5
|
import { useNavigation } from '@react-navigation/native';
|
|
6
6
|
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
|
|
@@ -19,19 +19,14 @@ import { hashtagPlugin, hashtagRenderer } from './extensions/hashtag';
|
|
|
19
19
|
import { mentionPlugin, mentionRenderer } from './extensions/mention';
|
|
20
20
|
import { codeBlockRenderer, fenceRenderer } from './extensions/codeBlock';
|
|
21
21
|
import { codeInlineRenderer } from './extensions/codeInline';
|
|
22
|
+
import { htmlBlockRenderer, htmlInlineRenderer } from './extensions/html';
|
|
22
23
|
import { markdownTheme } from './theme';
|
|
23
24
|
import { extractLinks, isEmail, isInternalLink, toGotoUrl } from './utils';
|
|
24
25
|
import { PreviewLink } from '../preview-link';
|
|
25
26
|
import { useOpenPeepsTheme } from '../../theme/OpenPeepsThemeProvider';
|
|
26
27
|
import { recordOutboundClick } from '@openpeepshq/react';
|
|
27
28
|
|
|
28
|
-
export interface OpenpeepsMarkdownProps {
|
|
29
|
-
source: string;
|
|
30
|
-
mentions?: MentionProfileRef[];
|
|
31
|
-
linkPreviewMode?: 'prepend' | 'append' | 'inline' | 'none';
|
|
32
|
-
/** When true, same-origin links open in the system browser instead of in-app navigation. */
|
|
33
|
-
newTab?: boolean | ((link: string) => boolean);
|
|
34
|
-
}
|
|
29
|
+
export interface OpenpeepsMarkdownProps extends MarkdownRenderProps {}
|
|
35
30
|
|
|
36
31
|
export const OpenpeepsMarkdown = ({
|
|
37
32
|
source,
|
|
@@ -46,7 +41,9 @@ export const OpenpeepsMarkdown = ({
|
|
|
46
41
|
useNavigation<NativeStackNavigationProp<MainStackParamList>>();
|
|
47
42
|
|
|
48
43
|
const markdownit = useMemo(() => {
|
|
49
|
-
|
|
44
|
+
// html: true so inline HTML (iframes, etc.) is tokenized and passed
|
|
45
|
+
// to custom renderers that apply the same allowlist as the web side.
|
|
46
|
+
const md = MarkdownIt({ linkify: true, typographer: true, html: true });
|
|
50
47
|
md.use(hashtagPlugin);
|
|
51
48
|
md.use(mentionPlugin(mentions));
|
|
52
49
|
return md;
|
|
@@ -62,6 +59,12 @@ export const OpenpeepsMarkdown = ({
|
|
|
62
59
|
return typeof newTab === 'function' ? newTab(url) : newTab;
|
|
63
60
|
};
|
|
64
61
|
|
|
62
|
+
const handleIframePress = (url: string) => {
|
|
63
|
+
const origin = BASE_URL as string;
|
|
64
|
+
recordOutboundClick(url, origin);
|
|
65
|
+
Linking.openURL(url);
|
|
66
|
+
};
|
|
67
|
+
|
|
65
68
|
const handleLinkPress = (url: string) => {
|
|
66
69
|
const origin = BASE_URL as string;
|
|
67
70
|
if (shouldOpenExternally(url)) {
|
|
@@ -73,7 +76,11 @@ export const OpenpeepsMarkdown = ({
|
|
|
73
76
|
};
|
|
74
77
|
|
|
75
78
|
const tokens = useMemo(
|
|
76
|
-
() =>
|
|
79
|
+
() =>
|
|
80
|
+
stringToTokens(
|
|
81
|
+
preprocessMarkdownSource(source ?? '', mentions),
|
|
82
|
+
markdownit
|
|
83
|
+
),
|
|
77
84
|
[source, mentions, markdownit]
|
|
78
85
|
);
|
|
79
86
|
const links = useMemo(
|
|
@@ -104,6 +111,8 @@ export const OpenpeepsMarkdown = ({
|
|
|
104
111
|
code_block: codeBlockRenderer(colors),
|
|
105
112
|
fence: fenceRenderer(colors),
|
|
106
113
|
code_inline: codeInlineRenderer(colors),
|
|
114
|
+
html_block: htmlBlockRenderer(handleIframePress),
|
|
115
|
+
html_inline: htmlInlineRenderer(handleIframePress),
|
|
107
116
|
}}
|
|
108
117
|
>
|
|
109
118
|
{ast as unknown as ReactNode[]}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Linking, StyleSheet, Text, View } from 'react-native';
|
|
3
|
+
import type { RenderFunction } from 'react-native-markdown-display';
|
|
4
|
+
import {
|
|
5
|
+
extractIframeSrcs,
|
|
6
|
+
MARKDOWN_HTML_ALLOWED_TAGS,
|
|
7
|
+
} from '@openpeepshq/common/lib';
|
|
8
|
+
|
|
9
|
+
const TAG_RE = /<\/?([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>/g;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Renderer for markdown-it `html_inline` tokens.
|
|
13
|
+
* Extracts iframes (rendered as tap-to-open links) and strips other HTML.
|
|
14
|
+
*/
|
|
15
|
+
export const htmlInlineRenderer =
|
|
16
|
+
(onIframePress: (url: string) => void): RenderFunction =>
|
|
17
|
+
(node) => {
|
|
18
|
+
const raw =
|
|
19
|
+
typeof node.content === 'string' ? node.content : String(node.content);
|
|
20
|
+
return renderHtmlContent(raw, onIframePress);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Renderer for markdown-it `html_block` tokens.
|
|
25
|
+
*/
|
|
26
|
+
export const htmlBlockRenderer =
|
|
27
|
+
(onIframePress: (url: string) => void): RenderFunction =>
|
|
28
|
+
(node) => {
|
|
29
|
+
const raw =
|
|
30
|
+
typeof node.content === 'string' ? node.content : String(node.content);
|
|
31
|
+
return renderHtmlContent(raw, onIframePress);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const renderHtmlContent = (
|
|
35
|
+
raw: string,
|
|
36
|
+
onIframePress: (url: string) => void
|
|
37
|
+
): React.ReactElement | null => {
|
|
38
|
+
// Iframes: extract src URLs and render as tap-to-open buttons.
|
|
39
|
+
const iframes = extractIframeSrcs(raw);
|
|
40
|
+
if (iframes.length > 0) {
|
|
41
|
+
return (
|
|
42
|
+
<View style={styles.iframeContainer} key={raw.slice(0, 40)}>
|
|
43
|
+
{iframes.map((src, i) => (
|
|
44
|
+
<View key={`${i}-${src.slice(0, 20)}`} style={styles.iframeRow}>
|
|
45
|
+
<Text style={styles.iframeLink} onPress={() => onIframePress(src)}>
|
|
46
|
+
Open embedded content
|
|
47
|
+
</Text>
|
|
48
|
+
<Text
|
|
49
|
+
style={styles.iframeUrl}
|
|
50
|
+
onPress={() => void Linking.openURL(src)}
|
|
51
|
+
>
|
|
52
|
+
{src}
|
|
53
|
+
</Text>
|
|
54
|
+
</View>
|
|
55
|
+
))}
|
|
56
|
+
</View>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Strip all HTML tags — only allowlisted tags are permitted on the web
|
|
61
|
+
// (via DOMPurify). On RN we render safe tags as text and drop the rest.
|
|
62
|
+
const allowed = new Set<string>(MARKDOWN_HTML_ALLOWED_TAGS);
|
|
63
|
+
const tags: string[] = [];
|
|
64
|
+
let match: RegExpExecArray | null;
|
|
65
|
+
TAG_RE.lastIndex = 0;
|
|
66
|
+
while ((match = TAG_RE.exec(raw)) !== null) {
|
|
67
|
+
tags.push(match[1]?.toLowerCase());
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// If all tags are allowlisted, strip them and render as text.
|
|
71
|
+
const text = raw.replace(TAG_RE, '');
|
|
72
|
+
if (tags.every((tag) => !tag || allowed.has(tag.toLowerCase()))) {
|
|
73
|
+
return <Text style={styles.text}>{text}</Text>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Disallowed tags present — strip everything to plain text.
|
|
77
|
+
return <Text style={styles.text}>{text}</Text>;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const styles = StyleSheet.create({
|
|
81
|
+
iframeContainer: { marginVertical: 8 },
|
|
82
|
+
iframeRow: { flexDirection: 'column', alignItems: 'flex-start' },
|
|
83
|
+
iframeLink: {
|
|
84
|
+
color: '#007AFF',
|
|
85
|
+
textDecorationLine: 'underline',
|
|
86
|
+
fontSize: 14,
|
|
87
|
+
},
|
|
88
|
+
iframeUrl: {
|
|
89
|
+
color: '#666',
|
|
90
|
+
fontSize: 12,
|
|
91
|
+
marginTop: 4,
|
|
92
|
+
},
|
|
93
|
+
text: { fontSize: 16, lineHeight: 24 },
|
|
94
|
+
});
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { stringToTokens } from 'react-native-markdown-display';
|
|
2
|
+
import { toGotoUrl } from '@openpeepshq/common/lib';
|
|
2
3
|
|
|
3
4
|
type Token = ReturnType<typeof stringToTokens>[number];
|
|
4
5
|
|
|
6
|
+
export { toGotoUrl };
|
|
7
|
+
|
|
5
8
|
export const isInternalLink = (href: string, origin: string): boolean => {
|
|
6
9
|
if (!href || href.includes('goto:/')) {
|
|
7
10
|
return !!href;
|
|
@@ -16,24 +19,6 @@ export const isInternalLink = (href: string, origin: string): boolean => {
|
|
|
16
19
|
}
|
|
17
20
|
};
|
|
18
21
|
|
|
19
|
-
export const toGotoUrl = (url: string, origin: string): string => {
|
|
20
|
-
if (url.includes('goto:/')) {
|
|
21
|
-
return url;
|
|
22
|
-
}
|
|
23
|
-
if (url.startsWith('/') && !url.startsWith('//')) {
|
|
24
|
-
return `goto:${url}`;
|
|
25
|
-
}
|
|
26
|
-
try {
|
|
27
|
-
const urlObj = new URL(url);
|
|
28
|
-
if (urlObj.origin === origin) {
|
|
29
|
-
return `goto:${urlObj.pathname}${urlObj.search}${urlObj.hash}`;
|
|
30
|
-
}
|
|
31
|
-
} catch {
|
|
32
|
-
// fall through
|
|
33
|
-
}
|
|
34
|
-
return url;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
22
|
export const extractLinks = (token: Token): string[] => {
|
|
38
23
|
if (token.type === 'link') {
|
|
39
24
|
return [token.href];
|
|
@@ -102,6 +102,22 @@ export const ConversationShow = ({ route, navigation }: ConversationProps) => {
|
|
|
102
102
|
void markPostsSeen({ postIds: unseenIds });
|
|
103
103
|
}, [id, currentProfile, messages, isLoading, markPostsSeen]);
|
|
104
104
|
|
|
105
|
+
useSetPageHeader(
|
|
106
|
+
isLoading ? undefined : (
|
|
107
|
+
<ConversationProfileHeader
|
|
108
|
+
participants={headerParticipants}
|
|
109
|
+
onPress={isGroupChat ? () => setParticipantsOpen(true) : undefined}
|
|
110
|
+
/>
|
|
111
|
+
),
|
|
112
|
+
isLoading ? undefined : (
|
|
113
|
+
<TouchableOpacity
|
|
114
|
+
onPress={() => navigation.navigate('ConversationInfo', { id })}
|
|
115
|
+
>
|
|
116
|
+
<InfoIcon size={18} className="text-foreground" />
|
|
117
|
+
</TouchableOpacity>
|
|
118
|
+
)
|
|
119
|
+
);
|
|
120
|
+
|
|
105
121
|
if (isLoading) {
|
|
106
122
|
return (
|
|
107
123
|
<ThemedView className="flex-1 items-center justify-center">
|
|
@@ -110,18 +126,6 @@ export const ConversationShow = ({ route, navigation }: ConversationProps) => {
|
|
|
110
126
|
);
|
|
111
127
|
}
|
|
112
128
|
|
|
113
|
-
useSetPageHeader(
|
|
114
|
-
<ConversationProfileHeader
|
|
115
|
-
participants={headerParticipants}
|
|
116
|
-
onPress={isGroupChat ? () => setParticipantsOpen(true) : undefined}
|
|
117
|
-
/>,
|
|
118
|
-
<TouchableOpacity
|
|
119
|
-
onPress={() => navigation.navigate('ConversationInfo', { id })}
|
|
120
|
-
>
|
|
121
|
-
<InfoIcon size={18} className="text-foreground" />
|
|
122
|
-
</TouchableOpacity>
|
|
123
|
-
);
|
|
124
|
-
|
|
125
129
|
return (
|
|
126
130
|
<ThemedView className="flex-1">
|
|
127
131
|
<View className="flex-1">
|