markdown-it / markdown-it-emoji

Emoji syntax plugin for markdown-it markdown parser
https://markdown-it.github.io/
MIT License
729 stars 166 forks source link

Need better isolation for shortcuts #5

Closed puzrin closed 9 years ago

puzrin commented 9 years ago

Need netter algorythm to avoid conflicts between shortcuts and text

Ideas? Discuss.

nogweii commented 9 years ago

I think the simplest solution is to ensure that the emoji's are surrounded by whitespace. (Or if an emoji follows an emoji.)

puzrin commented 9 years ago

Resolved in master

wizawu commented 9 years ago

@puzrin I think the solution in dd6a247f58ec96e508edfe83bc9d620587cce3f5 is still buggy.

e.g. :o still conflicts with :octocat:.

:+1: for @evaryont 's solution.

puzrin commented 9 years ago

Thanks for info. I've created new ticket, sources of problems are different. \w does not support unicode, and will be movement to wrong direction.

If anyone has time, he an try to write regex generator, as linkify-it does.

rlidwka commented 9 years ago

e.g. :o still conflicts with :octocat:.

@wizawu , I don't think it is. Here is the test I've come up with:

var md = require('markdown-it')()
var emoji = require('markdown-it-emoji')

md.use(emoji, {
  defs: {
    'octocat': 'B',
    'ouch': 'A',
  },

  shortcuts: {
    ouch: [ ':o' ]
  }
})

console.log(md.render(':octocat:')

It'll produce B, and never Actocat:.

The reason is that when we build regexp, all emoticons get sorted, thus the resulting regexp would be /:octocat:|:o/ and not the other way around.

As far as I know, regexp matching engine returns leftmost match, so it would always work:

> /:octocat:|:o/.exec(':octocat:')
[ ':octocat:',
  index: 0,
  input: ':octocat:' ]
> /:o|:octocat:/.exec(':octocat:')
[ ':o',
  index: 0,
  input: ':octocat:' ]

Thus, no bug there.

puzrin commented 9 years ago

@wizawu did you tested your example or not prior to report?

wizawu commented 9 years ago

My case is rather simple.

var md = require('markdown-it')()
var emoji = require('markdown-it-emoji')

md.use(emoji)

console.log(md.render(':octocat:'))

The output is

<p>😮ctocat:</p>

The problem is not with the custom :o shortcut. It is with the default :o shortcut.

wizawu commented 9 years ago

I know where the problem is... There is no "octocat" in full.json, so it would be just skipped.

I am using the plugin with emojify. I guess I need add the defs.