0.0.3-alpha • Published 2 years ago

sarma v0.0.3-alpha

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

How To Use?

Just pass in a text from a user input, and it will parse a token array for you :)

import { SarmaParser, SarmaParserOptions } from "sarma";

const text = "Hey there @CoconutOrange, me and @Diaval will be **checking** a movie #movie-night! Will you be joining? :eyes:";

const options: SarmaParserOptions = {
  text,
  parseEmphasis: true,
};

const parser = new SarmaParser(options);

const tokens = parser.parse();

Which returns an array like:

tokens = [
  TextToken({
    "rawText": "Hey there ",
    "location": [0, 9]
  }),
  MentionToken({
    "rawText": "@CoconutOrange",
    "location": [10, 23]
  }),
  TextToken({
    "rawText": ", me and ",
    "location": [24, 32]
  }),
  MentionToken({
    "rawText": "@Diaval",
    "location": [33, 39]
  }),
  TextToken({
    "rawText":" will be ",
    "location":[40, 48]
  }),
  BoldTextToken({
    "rawText":"**checking**",
    "location":[49, 60]
  }),
  TextToken({
    "rawText":" a movie ",
    "location":[61, 69]
  }),
  TagToken({
    "rawText":"#movie-night",
    "location":[70, 81]
  }),
  TextToken({
    "rawText":"! Will you be joining? ",
    "location":[82, 104]
  }),
  EmoteToken({
    "rawText":":eyes:",
    "location":[105, 110]
  })
]

Available Options

Sarma comes with a few handy options to alter parsing result:

Option NameTypeDefault ValueDescription
textstring - requiredText to be parsed into tokens
parseInto"words" or "chunks" - optional"chunks"Should it parse into words, or merge into bigger chunks where possible
parseWhitespacesboolean - optionalfalseShould it parse whitespaces as tokens or not
parseEmphasisboolean - optionalfalseShould it parse emphasis tokens such as bold (**) or italic (*)