DanLeininger / parse-mentions

Parse and optionally replace @ mentions from a string of text.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

parse-mentions NPM version NPM downloads Build Status

Parse and optionally replace @ mentions from a string of text.

Install

Install with npm:

$ npm install --save parse-mentions

Usage

var parse = require('parse-mentions');

API

Parse a string to retrieve or replace @ mention strings.

Params

  • str {String}: Input string to parse looking for @ mentions.
  • options {Object}: Optional options object to control the output of the @ mentions
  • options.stringify {Boolean}: Should the @ mentions be stringified in the output array. When false an object with additional information about the mention is returned. Defaults to true.
  • fn {Function}: Optional replace function. When passed in, the @ mentions are replaced in the string instead of returned as an array. The new string is returned.
  • returns {String|Array}: Array of mentions when a replace function is not passed in.

Example

// get an array of @ mention strings
var mentions = parse('- @doowb\n- @jonschlinkert');
console.log(mentions);
//=> ['doowb', 'jonschlinkert']

// replace @ mention strings with a url
var str = parse('- @doowb\n- @jonschlinkert', function(name) {
return `[@${name}](https://github.com/${name})`;
});
console.log(str);
//=> - [@doowb](https://github.com/doowb)
//=> - [@jonschlinkert](https://github.com/jonschlinkert)

// get an array of @ mention objects
var mentions = parse('- @doowb\n- @jonschlinkert', {stringify: false});
console.log(mentions);
//=> [
//=>   {name: 'doowb', mention: '@doowb', index: 2},
//=>   {name: 'jonschlinkert', mention: '@jonschlinkert', index: 11}
//=> ]

About

Related projects

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Please read the contributing guide for avice on opening issues, pull requests, and coding standards.

Building docs

(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)

To generate the readme and API documentation with verb:

$ npm install -g verb verb-generate-readme && verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Brian Woodward

License

Copyright © 2016, Brian Woodward. Released under the MIT license.


This file was generated by verb-generate-readme, v0.1.30, on September 24, 2016.

About

Parse and optionally replace @ mentions from a string of text.

License:MIT License


Languages

Language:JavaScript 100.0%