Commit fba0b475 authored by Rob Colbert's avatar Rob Colbert

project started

parent 947afd4d
Copyright 2018 Rob Colbert <rob.colbert@openplatform.us>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
# gab-parse
Copyright (C) 2019 Gab AI, Inc.<br/>
License: MIT
The `gab-parse` module implements a function that accepts text input and renders all @mentions and #hashtags to HTML links. By default, the links go to [Gab.com](https://gab.com/).
## Getting Started
To install gab-parse using NPM:
npm install gab-parse
To install gab-parse using yarn:
yarn add gab-parse
## Default Use
var gabParse = require('gab-parse');
var parser = gabParse();
var text = 'This gab mentions @robcolbert and the #LearnToCode hashtag.';
console.log(parser(text));
## Advanced Use
var gabParse = require('gab-parse');
var standard = gabParse();
console.log(standard('I am mentioning @support.'));
console.log(standard('This is a #hashtag'));
var hashtagsOnly = gabParse({ mentions: false });
var mentionsOnly = gabParse({ hashtags: false });
var twitter = gabParse({
replaceMention: (mention) => {
return `<a href="https://twitter.com/${mention.slice(1)}">${mention}</a>`;
},
replaceHashtag: (tag) => {
return `<a href="https://twitter.com/hashtag/${tag.slice(1)}">${tag}</a>`;
}
});
var text = 'This gab mentions @robcolbert and the #LearnToCode hashtag.';
console.log(standard(text));
console.log(hashtagsOnly(text));
console.log(mentionsOnly(text));
console.log(twitter(text));
## Options
{
mentions: Boolean,
hashtags: Boolean,
replaceMention: Function,
replaceHashtag: Function
}
The gab-parse module exposes a function that accepts an options object and returns a configured function that parses text with the options provided. The parser can enable/disable the processing of mentions and/or hashtags, and the replacer functions can be overridden to provide whatever level of formatting is required including linking to other services.
### mentions: Boolean
When true, mentions will be replaced. When false, mentions will not be replaced.
### hashtags: Boolean
When true, hashtags will be replaced. When false, hashtags will not be replaced.
### replaceMention: Function
A function that accepts a mention with the at symbol (@) and returns a well-formed HTML &lt;a&gt; element with the `href` attribute set to link to a service's profile page for the mentioned user.
### replaceHashtag: Function
A function that accepts a mention with the hash/pound sign (#) and returns a well-formed HTML &lt;a&gt; element with the `href` attribute set to link to a service's page about the tag.
// gab-parse.js
// Copyright (C) 2019 Gab AI, Inc.
// License: MIT
'use strict';
module.exports = (options) => {
const defaults = {
mentions: true,
hashtags: true,
replaceMention: (mention) => {
return `<a href="https://gab.com/${mention.slice(1)}">${mention}</a>`;
},
replaceHashtag: (tag) => {
return `<a href="https://gab.com/hash/${tag.slice(1)}">${tag}</a>`;
}
};
if (!options) {
options = defaults;
} else if (Array.isArray(options)) {
throw new Error('gab-parse: options must not be an array');
} else if (typeof options !== 'object') {
throw new Error('gab-parse: options must be an object');
} else {
options = Object.assign(defaults, options);
}
return (text) => {
var parseRegEx = /(^|\s)(#|@)\w{3,30}\b/g;
var slugs = { };
var tags = text.match(parseRegEx);
if (!tags || !tags.length) {
return text;
}
tags = tags
.map((tag) => {
return tag.trim();
})
.filter((tag) => {
return !slugs[tag] && (slugs[tag] = true);
});
tags.forEach((tag) => {
switch (tag[0]) {
case '@':
if (options.mentions) {
text = text.replace(new RegExp(tag, 'g'), options.replaceMention(tag));
}
break;
case '#':
if (options.hashtags) {
text = text.replace(new RegExp(tag, 'g'), options.replaceHashtag(tag));
}
break;
}
});
return text;
};
};
\ No newline at end of file
{
"name": "gab-parse",
"version": "0.1.0",
"description": "Library to convert @mentions and #hashtags into HTML links to Gab.com",
"main": "gab-parse.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/robcolbert/gab-parse.git"
},
"keywords": [
"gab",
"gab.com",
"mention",
"hashtag",
"parse",
"link",
"href"
],
"author": "Gab AI, Inc.",
"license": "MIT",
"bugs": {
"url": "https://github.com/robcolbert/gab-parse/issues"
},
"homepage": "https://github.com/robcolbert/gab-parse#readme"
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment