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

Update readme to get mentioned data from string value on 2.x.x version #109

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,75 @@ const value = 'Hello @[David Tabaka](5)! How are you?';

console.log(replaceMentionValues(value, ({id}) => `@${id}`)); // Hello @5! How are you?
console.log(replaceMentionValues(value, ({name}) => `@${name}`)); // Hello @David Tabaka! How are you?
```


### Get mentioned data from a text value

For get mentioned data from a value like this, for example:
```
const value = "@[John Doe](someJohnId) how are you?"
```
You need to do a setup like this (please modify by your own)
```
const mentionPartType: MentionPartType = {
trigger: '@',
};

const hashtagPartType: MentionPartType = {
trigger: '#',
};

const urlPatternPartType: PatternPartType = {
pattern: /(https?:\/\/|www\.)[-a-zA-Z0-9@:%._\+~#=]{1,256}\.(xn--)?[a-z0-9-]{2,20}\b([-a-zA-Z0-9@:%_\+\[\],.~#?&\/=]*[-a-zA-Z0-9@:%_\+\]~#?&\/=])*/gi,
};

const partTypes: PartType[] = [
mentionPartType,
hashtagPartType,
urlPatternPartType,
];
```

consume it like this:
```
const { parts, plainText } = parseValue(value, partTypes)
```

the value of parts will be like this (please transform it if you need it in different obj)
```
[
{
"text": "@John Doe",
"position": {
"start": 0,
"end": 9
},
"partType": {
"trigger": "@"
},
"data": {
"original": "@[John Doe](someJohnId)",
"trigger": "@",
"name": "John Doe",
"id": "someJohnId"
}
},
{
"text": " how are you?",
"position": {
"start": 9,
"end": 22
}
}
]
```






Rendering `MentionInput`'s value
-
If you want to parse and render your value somewhere else you can use `parseValue` tool which gives you array of parts and then use your own part renderer to resolve this issue.
Expand Down