GithubHelp home page GithubHelp logo

hashtag-js's Introduction

Hashtag.js

Hastag.js is a js library for finding and replacing hashtags in strings. Useful when working with your own backends that use social media feeds.

Installation

Hashtag.js does not require jQuery or any other libraries, it's written in plain Vanilla JS. To install, just include the script in your document:

<script type="text/javascript" src="hashtag.min.js"></script>

Basic usage

Hashtag.replaceTags('#myDiv');

As simple as that! Let's say that #myDiv looks like this:

<div id="myDiv">
  #hashtags are #cool
</div>

After running the replaceTags() function, the tags will be replaced:

<div id="myDiv">
  <span class="tag">#hashtags</span> are <span class="tag">#cool</span>
</div>

Setting options

You can set options for the library by using the setOptions() function and passing an object to it.

Templates

Hashtags.js supports a simple templating system for replacing tags. To specify a template that should be used for replacing tags, pass it in the setOptions() function:

Hashtag.setOptions({
    'template': '<a href="https://twitter.com/hashtag/{#n}">{#}</a>'
})

Upon calling the replaceTags() function, all the tags will be replaced with the provided template.

You have two expressions you can use in the template:

  • {#} - the hashtag
  • {#n} - the name of the hashtag (whitout '#', handy for using in links)

Multiple templates

You can have multiple templates set up that yo can alternately use. To do so, you must specify a list of templates in the setOptions() function:

Hashtag.setOptions({
	templates: {
		'twitter': '<a href="https://twitter.com/hashtag/{#n}">{#}</a>',
		'fb': '<a href="https://www.facebook.com/hashtag/{#n}">{#}</a>'
	}
});

Now you can use your templates with the replaceTags() function:

Hashtag.replaceTags('#myDiv', 'fb');

hashtag-js's People

Contributors

jakubbilko avatar

Stargazers

 avatar Ulisses Caon avatar Hyewon Cho avatar Reinaldinho avatar Kenichi Saito avatar Asuka Suzuki avatar

Watchers

James Cloos avatar  avatar jpgaelzer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.