Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: normalize emoji characters for consistency #37

Merged
merged 5 commits into from
Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

27 changes: 12 additions & 15 deletions lib/block.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const emoji = require('node-emoji')
const emojiRegex = require('emoji-regex')
const hljs = require('highlight.js')
const util = require('./util')

Expand Down Expand Up @@ -37,20 +39,6 @@ md.renderer.rules.hashtag_open = function (tokens, idx) {
return '<a href="' + url + '">'
}

const oldEmojiRender = md.renderer.rules.emoji || function (tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options)
}

// emoji
md.renderer.rules.emoji = function (tokens, idx) {
if (config.emoji) {
const emoji = tokens[idx].markup
return config.emoji(emoji)
} else {
return oldEmojiRender(tokens, idx)
}
}

// links
var oldLinkOpenRender = md.renderer.rules.link_open || function (tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options)
Expand Down Expand Up @@ -167,6 +155,8 @@ md.renderer.rules.image = (tokens, idx) => {
}

module.exports = (text, opts) => {
const normalized = emoji.unemojify('' + text || '')

config = Object.assign({}, defaults, opts)

Object.values(config.protocols.concat(additionalProtocols)).forEach(protocol => {
Expand All @@ -176,5 +166,12 @@ module.exports = (text, opts) => {
}
})

return md.render('' + (text || ''))
let result = md.render(normalized)

if (config.emoji) {
const regex = emojiRegex()
result = result.replace(regex, x => config.emoji(emoji.which(x) || x))
}

return util.removeBadBytes(result)
}
27 changes: 13 additions & 14 deletions lib/inline.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const emoji = require('node-emoji')
const emojiRegex = require('emoji-regex')
const util = require('./util')

const defaults = {
Expand All @@ -11,19 +13,6 @@ let config = {}
const md = require('markdown-it')()
.use(require('markdown-it-emoji'), { shortcuts: {} })

// emoji
const oldEmojiRender = md.renderer.rules.emoji || function (tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options)
}
md.renderer.rules.emoji = function (tokens, idx) {
if (config.emoji) {
const emoji = tokens[idx].markup
return config.emoji(emoji)
} else {
return oldEmojiRender(tokens, idx)
}
}

// links
md.renderer.rules.link_open = () => ''
md.renderer.rules.link_close = () => ''
Expand All @@ -42,6 +31,16 @@ md.renderer.rules.image = (tokens, idx) => {
return tokens[idx].content
}
module.exports = (text, opts) => {
let normalized = emoji.unemojify('' + text || '')

config = Object.assign({}, defaults, opts)
return util.replaceNewlines(md.renderInline('' + (text || '')))

let result = util.replaceNewlines(md.renderInline(normalized))

if (config.emoji) {
const regex = emojiRegex()
result = result.replace(regex, x => config.emoji(emoji.which(x) || x))
}

return util.removeBadBytes(result)
}
8 changes: 7 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@ module.exports = {
replaceNewlines: text => text.replace(/\n+(?!$)/g, ' '),
formatSigilText: sigilText => {
return sigilText.replace(/^%/, '%25').slice(0, 8) + '...'
}
},
// https://github.com/joypixels/emojione/issues/644
removeBadBytes: text =>
text
.split('')
.filter(f => f.codePointAt(0) !== 0xfe0f)
.join('')
}
Loading