markdown-it-emoji
markdown-it-emoji copied to clipboard
How to add additional shortcuts?
Hi,
I'm trying to add additional shortcuts like so:
import markdownIt from 'markdown-it'
import { full as emoji } from 'markdown-it-emoji'
const md = markdownIt()
.use(emoji, {
shortcuts: {
"fa-eye": [":fa-eye:"]
}
})
I'm trying to support font-awesome (now fork-awesome) support. Typing :fa-eye: should render the fork-awesome emoji for that. I know how to do that part (emoji rule overwrite to handle the fa-* emoji's) but i'm stuck at even defining additional shortcuts.
When debugging that shortcut that i'm adding is present in this code (full.mjs in this project):
export default function emoji_plugin (md, options) {
const defaults = {
defs: emojies_defs,
shortcuts: emojies_shortcuts,
enabled: []
}
const opts = md.utils.assign({}, defaults, options || {})
bare_emoji_plugin(md, opts)
};
However, after the line:
const opts = md.utils.assign({}, defaults, options || {})
It's already reduced to just my shortcut. The default ones emojies_shortcuts get lost here.
Next, it's passed into the bare_emoji_plugin via bare_emoji_plugin(md, opts).
In the bare handling this line:
const opts = normalize_opts(md.utils.assign({}, defaults, options || {}))
Removes anything that was there, the resulting opts.shortcuts is now empty {}...
Am i doing something wrong here?
Hmm, i have it working but it's perhaps not the nicest way.. Here's the diff:
diff --git a/lib/full.mjs b/lib/full.mjs
index 1ca6757..67bf812 100644
--- a/lib/full.mjs
+++ b/lib/full.mjs
@@ -9,6 +9,8 @@ export default function emoji_plugin (md, options) {
enabled: []
}
+ options.shortcuts = { ...emojies_shortcuts, ...options?.shortcuts}
+
const opts = md.utils.assign({}, defaults, options || {})
bare_emoji_plugin(md, opts)
diff --git a/lib/normalize_opts.mjs b/lib/normalize_opts.mjs
index 69899ff..b5a2014 100644
--- a/lib/normalize_opts.mjs
+++ b/lib/normalize_opts.mjs
@@ -19,7 +19,7 @@ export default function normalize_opts (options) {
// Flatten shortcuts to simple object: { alias: emoji_name }
const shortcuts = Object.keys(options.shortcuts).reduce((acc, key) => {
// Skip aliases for filtered emojies, to reduce regexp
- if (!emojies[key]) return acc
+ // if (!emojies[key]) return acc
if (Array.isArray(options.shortcuts[key])) {
options.shortcuts[key].forEach(alias => { acc[alias] = key })
So i'm explicitly merging options.shortcuts which solved the issue of my values being gone.
And i'm intentionally disabling a check in normalize_opts.mjs for my entries to stay in there!
Next, i do this rule override:
md.renderer.rules.emoji = function (tokens, idx, options, env, self) {
// if content starts with fa-
const tokenMarkup = tokens[idx].markup;
if (tokenMarkup.startsWith('fa-')) {
return `<i class="fa ${tokenMarkup}"></i>`
} else {
return tokens[idx].content;
}
};
And it works! Neat :) But is it proper? ..