Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/richie-south/javascript-rich-string-parser
Find tokens in your strings
https://github.com/richie-south/javascript-rich-string-parser
parser string-parser string-parsing
Last synced: about 5 hours ago
JSON representation
Find tokens in your strings
- Host: GitHub
- URL: https://github.com/richie-south/javascript-rich-string-parser
- Owner: richie-south
- License: mit
- Created: 2021-08-31T12:29:30.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-20T11:57:07.000Z (10 months ago)
- Last Synced: 2024-10-29T01:28:50.077Z (19 days ago)
- Topics: parser, string-parser, string-parsing
- Language: TypeScript
- Homepage:
- Size: 388 KB
- Stars: 1
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rich-string-parser
Finds rich text in strings, ex: links, mentions, emails, your own parser, in an non overlapping way to prevent dubble matching.
**Example**
```typescript
import {richStringParser} from 'rich-string-parser'
import {emailParser} from 'rich-string-parser/lib/parsers/email-parser'
import {linkParser} from 'rich-string-parser/lib/parsers/link-parser'const result = richStringParser(
'https://www.typescriptlang.org/ text [email protected] more text',
[emailParser(), linkParser()],
)// log result
/* [
{
type: 'LinkParser',
match: 'https://www.typescriptlang.org/',
index: 0,
subIndex: 0
},
' text ',
{
type: 'EmailParser',
match: '[email protected]',
index: 37,
subIndex: 37
},
' more text'
] */
```## Parser result
required parameters
```jsonc
{
"type": "", // uniq for each parser
"match": "", // result from parser
"index": 0, // where match is found based on whole string
"subIndex": 0 // where match is found based on parsed substring
}
```## Built in parsers
**MentionParser**
Matches on `@(number|string)`, @(9712|John)
Result:
Without mention type:
```typescript
{
type: 'MentionParser',
match: '@(9712|John)',
id: 9712,
name: 'John',
index: 0,
subIndex: 0,
target: undefined
}
```With mention type provided:
```typescript
{
type: 'MentionParser',
match: '@(0|Testgroupchatroom|group)',
id: 0,
name: 'Testgroupchatroom',
index: 0,
subIndex: 0,
target: 'group'
}
```**EmailParser**
Matches on `[email protected]`,
Result:
```typescript
{
type: 'EmailParser',
match: '[email protected]',
index: 0,
subIndex: 0,
}
```**LinkParser**
Matches on `https://example.com`,
Result:
```typescript
{
type: 'LinkParser',
match: 'https://example.com',
index: 0,
subIndex: 0,
}
```## Create your own parser
Create a function that retrurns a `Parser` interface with a custom string in generic type, example `Parser<'HashtagParser'>`.
Implemente the required return object function `parse`.**Example**
```typescript
function hashtagParser(): Parser<'HashtagParser'> {
const regex: RegExp = /(#[a-z\d-]+)/gireturn {
parse: (text, index) => {
const result = regex.exec(text)
if (result === null) return nullreturn {
type: 'HashtagParser',
match: result[0],
index: index + result.index, // don't forget to add the global index
subIndex: result.index,
}
},
}
}
```