Git Product home page Git Product logo

markdown-it-hashtag's Introduction

markdown-it-hashtag

Build Status Coverage Status npm version

hashtag (#tag) plugin for markdown-it markdown parser.

#hashtag => <a href="/tags/hashtag" class="tag">#hashtag</a>

Install

node.js, bower:

npm install markdown-it-hashtag --save
bower install markdown-it-hashtag --save

Use

Basic

var md = require('markdown-it')()
            .use(require('markdown-it-hashtag'));

md.render('#hashtag'); // => '<p><a href="/tags/hashtag" class="tag">#hashtag</a></p>'

Differences in browser. If you load the script directly into the page, without package system, module will add itself globally as window.markdownitHashtag.

Advanced

You can specify the RegExp for hashtags and specify the allowed preceding content. You can also modify the output of the renderer. Here is an example with default values:

var md = require('markdown-it')()
            .use(require('markdown-it-hashtag'),{
              // pattern for hashtags with normal string escape rules
              hashtagRegExp: '\\w+',
              // pattern for allowed preceding content
              preceding:     '^|\\s'
            });

md.renderer.rules.hashtag_open  = function(tokens, idx) {
  var tagName = tokens[idx].content.toLowerCase(); 
  return '<a href="/tags/' + tagName + '" class="tag">';
}

md.renderer.rules.hashtag_text  = function(tokens, idx) {
  return '#' + tokens[idx].content;
}

md.renderer.rules.hashtag_close = function { return '</a>'; }

md.render('#hashtag'); // => '<p><a href="/tags/hashtag" class="tag">#hashtag</a></p>'

License

MIT

markdown-it-hashtag's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

markdown-it-hashtag's Issues

Check if there is a clean solution to prevent hashtag links inside of links

The link text may have some markup so most inline rules also apply for those texts. We don't want hashtag markup inside of link texts because then we would have a link inside of a link.

To prevent that the current implementation goes through the token stream and replaces all hashtag tokens between link_open and link_close with text tokens. The code can be found here: https://github.com/svbergerem/markdown-it-hashtag/blob/develop/index.js#L115

One should check if there is a cleaner solution for that issue. It would be great if we could make sure that the hashtag rule isn't applied inside of links at all. (before even looking for hashtags)

Support for non-English characters

In JavaScript regular expressions, \w only matches [A-Za-z0-9_].
So it doesn't work well if we put any non-English characters in the tag, like #测试 or #テスト.

Perhaps \w+ should be replaced by something like (?:\w|[^\u0000-\u007F])+ or [^\u0000-\u0029\u0040\u005b-\u0060\u007b-\u007f], as suggested in a StackOverflow Answer?

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.