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

How to add additional shortcuts? #55

Open
markg85 opened this issue Aug 25, 2024 · 1 comment
Open

How to add additional shortcuts? #55

markg85 opened this issue Aug 25, 2024 · 1 comment

Comments

@markg85
Copy link

markg85 commented Aug 25, 2024

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?

@markg85
Copy link
Author

markg85 commented Aug 25, 2024

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? ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant