Parse and optionally replace @ mentions from a string of text.
Install with npm:
$ npm install --save parse-mentions
var parse = require('parse-mentions');
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 @ mentionsoptions.stringify
{Boolean}: Should the @ mentions be stringified in the output array. Whenfalse
an object with additional information about the mention is returned. Defaults totrue
.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}
//=> ]
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.
(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
Install dev dependencies:
$ npm install -d && npm test
Brian Woodward
Copyright © 2016, Brian Woodward. Released under the MIT license.
This file was generated by verb-generate-readme, v0.1.30, on September 24, 2016.