-
Notifications
You must be signed in to change notification settings - Fork 76
/
index.tsx
95 lines (84 loc) 路 2.34 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import React from 'react';
import BaseInterweave, {
InterweaveProps as BaseInterweaveProps,
MatcherInterface,
FilterInterface,
} from 'interweave';
import { EmailMatcher, UrlMatcher } from 'interweave-autolink';
import { EmojiMatcher, useEmojiData } from 'interweave-emoji';
import Core from '../..';
import EmailFactory from './factories/Email';
import UrlFactory from './factories/Url';
import transformer from './factories/transformer';
export const globalMatchers: MatcherInterface<{}>[] = [];
export const globalFilters: FilterInterface[] = [];
const emojiOptions = {
convertEmoticon: false,
convertShortcode: true,
convertUnicode: true,
enlargeThreshold: 3,
};
export const urlMatcher = new UrlMatcher(
'url',
{
customTLDs: ['tools'],
},
UrlFactory,
);
export const emailMatcher = new EmailMatcher('email', {}, EmailFactory);
export const emojiMatcher = new EmojiMatcher('emoji', emojiOptions);
export const emojiMatcherWithEmoticons = new EmojiMatcher('emoji', {
...emojiOptions,
convertEmoticon: true,
});
export type Props = BaseInterweaveProps & {
/** Render any found links using large prop. */
large?: boolean;
/** Only run these matchers (by name). */
onlyMatchers?: string[];
/** Render any found links using small prop. */
small?: boolean;
/** Convert emoticons to emojis. */
withEmoticons?: boolean;
};
/**
* Safely render HTML, filter attributes, autowrap text with matchers, render emoji characters,
* and much more.
*/
export default function Interweave({
content,
filters = [],
matchers = [],
onlyMatchers = [],
withEmoticons,
...props
}: Props) {
const [, emojiSource] = useEmojiData({
avoidFetch: process.env.NODE_ENV === 'test',
throwErrors: false,
});
const finalFilters = [...globalFilters, ...filters];
let finalMatchers = [
...globalMatchers,
emailMatcher,
urlMatcher,
withEmoticons ? emojiMatcherWithEmoticons : emojiMatcher,
...matchers,
];
if (onlyMatchers.length > 0) {
finalMatchers = finalMatchers.filter(matcher => onlyMatchers.includes(matcher.propName));
}
return (
<BaseInterweave
newWindow
content={content}
emojiSize="1.25em"
filters={finalFilters}
matchers={finalMatchers}
transform={transformer}
{...props}
emojiPath={Core.settings.emojiCDN}
emojiSource={emojiSource}
/>
);
}