Skip to content

Commit be45d23

Browse files
committed
docs: update docs following refactoring
1 parent 6cd64d1 commit be45d23

File tree

1 file changed

+106
-62
lines changed

1 file changed

+106
-62
lines changed

README.md

Lines changed: 106 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,54 @@
22

33
[![Version][version-image]][package-url] [![Downloads][downloads-image]][package-url] [![Build Status][build-image]][build-url] [![License][license-image]][license-url]
44

5-
Auto-Linking component for React Native. Parses text and wraps URLs, phone numbers, emails, social handles, hashtags, and more with Text nodes and onPress handlers. And it's all fully customizable :)
5+
Auto-Linking component for React Native. Parses text and wraps URLs, phone numbers, emails, social handles, hashtags, and more with Text nodes and onPress handlers.
6+
7+
**New in v4**: Autolink any pattern using [custom regex matchers](#custom-matchers) and click handlers! Thanks @lafiosca!
68

79
## Installation
810

911
```shell
10-
npm install react-native-autolink --save
12+
npm i react-native-autolink
1113
```
1214

1315
## Usage
1416

15-
Simply import the library and pass desired props:
17+
Simply import the library and enable the link types you want to auto-link:
1618

1719
```javascript
1820
import Autolink from 'react-native-autolink';
1921

20-
class MyComponent extends Component {
21-
render() {
22-
return (
23-
<Autolink
24-
text="This is the string to parse for urls (https://github.com/joshswan/react-native-autolink), phone numbers (415-555-5555), emails (josh@example.com), mentions/handles (@twitter), and hashtags (#exciting)"
25-
hashtag="instagram"
26-
mention="twitter"
27-
/>
28-
);
29-
}
30-
}
22+
const MyComponent = () => (
23+
<Autolink
24+
// Required: the text to parse for links
25+
text="This is the string to parse for urls (https://github.com/joshswan/react-native-autolink), phone numbers (415-555-5555), emails (josh@example.com), mentions/handles (@twitter), and hashtags (#exciting)"
26+
// Optional: enable email linking
27+
email
28+
// Optional: enable hashtag linking to instagram
29+
hashtag="instagram"
30+
// Optional: enable @username linking to twitter
31+
mention="twitter"
32+
// Optional: enable phone linking
33+
phone="sms"
34+
// Optional: enable URL linking
35+
url
36+
// Optional: custom linking matchers
37+
matchers={[MyCustomTextMatcher]}
38+
/>
39+
);
3140
```
3241

42+
_Note_: No link types are enabled by default as of v4. Be sure to enable one or more (e.g. `email`, `hashtag`, `phone`, etc.) or you won't see anything linked!
43+
3344
## Props
3445

3546
- [`component?`](#component)
36-
- [`customLinks?`](#customLinks)
3747
- [`email?`](#email)
3848
- [`hashtag?`](#hashtag)
3949
- [`latlng?`](#latlng)
4050
- [`linkProps?`](#linkprops)
4151
- [`linkStyle?`](#linkstyle)
52+
- [`matchers?`](#matchers)
4253
- [`mention?`](#mention)
4354
- [`onPress?`](#onpress)
4455
- [`onLongPress?`](#onlongpress)
@@ -68,53 +79,6 @@ class MyComponent extends Component {
6879
<Autolink text={text} component={View} />
6980
```
7081

71-
### `customLinks`
72-
73-
| Type | Required | Default | Description |
74-
| ----------------------- | -------- | ------- | ----------------------------------------------------------------------- |
75-
| `UserCustomMatchSpec[]` | No | | Specifications for custom link patterns and their handling (see below). |
76-
77-
This property allows the user to establish custom link patterns and handling. It is particularly useful for mixing internal app navigation links with standard external links within the same block of content.
78-
79-
```ts
80-
interface UserCustomMatchSpec {
81-
/** Regular expression pattern to match user-specified custom links */
82-
pattern: RegExp;
83-
/** Custom function for extracting link text from regex replacer args */
84-
extractText?: (replacerArgs: ReplacerArgs) => string;
85-
/** Custom function for extracting link URL from regex replacer args */
86-
extractUrl?: (replacerArgs: ReplacerArgs) => string;
87-
/** Custom override for styling links of this type */
88-
style?: StyleProp<TextStyle>;
89-
/** Custom override for handling presses on links of this type */
90-
onPress?: (replacerArgs: ReplacerArgs) => void;
91-
/** Custom override for handling long-presses on links of this type */
92-
onLongPress?: (replacerArgs: ReplacerArgs) => void;
93-
}
94-
```
95-
96-
The `ReplacerArgs` type is an array containing the variadic arguments passed to a replacer function as provided to `String.replace`. Essentially, element 0 is the entire matched link, and elements 1 through N are any captured subexpressions. More details can be found [in this documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_function_as_a_parameter).
97-
98-
When using the built-in link handling, the `extractUrl` function can be provided to determine the URL to which the link should navigate. Alternatively the `onPress` function will bypass that entirely, allowing the user to provide custom handling specific to this link type, useful for navigating within the application.
99-
100-
The following hypothetical example handles custom @-mention links of the format `@[Name](userId)`, navigating to a user profile screen:
101-
102-
```tsx
103-
<Autolink
104-
text={text}
105-
customLinks={[
106-
{
107-
pattern: /@\[([^[]*)]\(([^(^)]*)\)/g,
108-
style: { color: '#ff00ff' },
109-
extractText: (args) => `@${args[1]}`,
110-
onPress: (args) => {
111-
navigate('userProfile', { userId: args[2] });
112-
},
113-
},
114-
]}
115-
/>
116-
```
117-
11882
### `email`
11983

12084
| Type | Required | Default | Description |
@@ -167,6 +131,18 @@ _Warning:_ Still experimental.
167131
<Autolink text={text} linkStyle={{ color: 'blue' }} />
168132
```
169133

134+
### `matchers`
135+
136+
| Type | Required | Default | Description |
137+
| ----------------- | -------- | ------- | ----------------------------------------------------------------------------------------------- |
138+
| `CustomMatcher[]` | No | | Array of custom matcher objects with optional press handlers, styling, and text/url extraction. |
139+
140+
See the [Custom Matchers](#custom-matchers) section below for more information.
141+
142+
```js
143+
<Autolink text={text} matchers={[MyCustomMatcher]} />
144+
```
145+
170146
### `mention`
171147

172148
| Type | Required | Default | Description |
@@ -369,6 +345,74 @@ type UrlConfig = {
369345

370346
_Note:_ Requires `LSApplicationQueriesSchemes` on iOS so it is disabled by default on iOS. See [Linking docs](https://reactnative.dev/docs/linking.html) for more info.
371347

348+
## Custom Matchers
349+
350+
Custom matchers allow for complete customization of Autolink. You can match text based on a custom regular expression, supply custom `onPress` and `onLongPress` handlers, custom link styles, and even custom functions for creating the text and/or URL for the match.
351+
352+
Custom matchers are particularly useful for matching other types of data that aren't supported out-of-the-box (e.g. international phone numbers) and for mixing internal app navigation links with standard external links within the same block of content.
353+
354+
Check out the section below for a few [ready-to-go custom matchers](#available-matchers).
355+
356+
```ts
357+
interface CustomMatcher {
358+
/* Regular expression pattern to match/link user-specified patterns */
359+
pattern: RegExp;
360+
/* Custom press handler for links of this type */
361+
onPress?: (match: CustomMatch) => void;
362+
/* Custom long-press handler for links of this type */
363+
onLongPress?: (match: CustomMatch) => void;
364+
/* Custom styling for links of this type */
365+
style?: StyleProp<TextStyle>;
366+
/* Custom type/identifier for use with match.getType() */
367+
type?: string;
368+
/* Custom function for extracting link text using regex replacer args */
369+
getLinkText?: (replacerArgs: ReplacerArgs) => string;
370+
/* Custom function for extracting link URL using regex replacer args */
371+
getLinkUrl?: (replacerArgs: ReplacerArgs) => string;
372+
}
373+
```
374+
375+
The `ReplacerArgs` type supplied to `getLinkText` and `getLinkUrl` is an array containing the variadic arguments passed to a replacer function as provided to `String.replace`. Essentially, element 0 is the entire matched link, and elements 1 through N are any captured subexpressions. More details can be found [in the documentation on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_function_as_a_parameter).
376+
377+
The `CustomMatch` class supplied to `onPress` and `onLongPress` (or their non-custom-matcher companions above) includes a few methods that can be useful as well, e.g. `getMatcher()` to return the `CustomMatcher` object and `getReplacerArgs` to return the same array discussed above.
378+
379+
### Custom Matcher Usage
380+
381+
When using the built-in link handling, the `getLinkUrl` function can be provided to determine the URL to which the link should navigate. Alternatively the `onPress` function will bypass that entirely, allowing the user to provide custom handling specific to this link type, e.g. for navigating within the application.
382+
383+
The following hypothetical example handles custom @-mention links of the format `@[Name](userId)`, navigating to a user profile screen:
384+
385+
```js
386+
<Autolink
387+
text={text}
388+
matchers={[
389+
{
390+
pattern: /@\[([^[]*)]\(([^(^)]*)\)/g,
391+
style: { color: '#ff00ff' },
392+
getLinkText: (replacerArgs) => `@${replacerArgs[1]}`,
393+
onPress: (match) => {
394+
navigate('userProfile', { userId: match.getReplacerArgs()[2] });
395+
},
396+
},
397+
]}
398+
/>
399+
```
400+
401+
### Available Matchers
402+
403+
A few custom matchers (e.g. `LatLngMatcher`, `IntlPhoneMatcher`, `PhoneMatchersByCountry`) are already included and available for use. They're just objects so they're easily customizable too! PRs are welcome for additional custom matchers that could be useful to the community.
404+
405+
```js
406+
import { Autolink, LatLngMatcher } from 'react-native-autolink';
407+
408+
<Autolink text="Some text with locations 32.123, -117.123" matchers={[LatLngMatcher]} />;
409+
410+
// Or customize the matcher
411+
const MyLatLngMatcher = { ...LatLngMatcher, onPress: () => alert('LatLng pressed!') };
412+
413+
<Autolink text={text} matchers={[MyLatLngMatcher]} />;
414+
```
415+
372416
## Supported By
373417

374418
<a href="https://www.disruptivelabs.io">

0 commit comments

Comments
 (0)