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

Support customEmojis #31

Merged
merged 1 commit into from
Apr 8, 2024
Merged

Support customEmojis #31

merged 1 commit into from
Apr 8, 2024

Conversation

guoyunhe
Copy link
Contributor

@guoyunhe guoyunhe commented Jan 9, 2024

Fix #30

@rhysd
Copy link
Owner

rhysd commented Apr 8, 2024

Thank you for sending this patch. I apologize for the delay since I was busy on other projects.

@rhysd rhysd merged commit 2b7e01f into rhysd:master Apr 8, 2024
rhysd added a commit that referenced this pull request Apr 8, 2024
This reverts commit 2b7e01f, reversing
changes made to e4b9918.
@rhysd
Copy link
Owner

rhysd commented Apr 8, 2024

I reverted this patch because it is broken and it is not easily fixed in my side.

When replacing emoji markup with HTML element string, remark sanitizes the input.

So, for example,

import { remark } from 'remark';
import toHtml from 'remark-html';
import emoji from 'remark-emoji';

const processor = remark()
    .use(emoji, {
        customEmojis: {
            parrot: '<img src="https://cultofthepartyparrot.com/parrots/parrot.gif" style="width:auto;height:1em;">',
        },
    })
    .use(toHtml);

const file = await processor.process('Hello :parrot:!');
console.log(String(file));

The output is:

<p>Hello &#x3C;img src="https://cultofthepartyparrot.com/parrots/parrot.gif" style="width:auto;height:1em;">!</p>

< is replaced with &#x3C;.

Please submit a new PR after fixing this issue. Before creating it,

  • Please confirm your patch works in your local
  • Please add tests to verify your modifications

@rhysd
Copy link
Owner

rhysd commented Apr 8, 2024

The misunderstanding in this patch is that replaceEmoji returns a raw HTML string. It actually returns a text string, which will be sanitized as HTML text node.

Please read this API document for the details of return type: https://github.com/syntax-tree/mdast-util-find-and-replace?tab=readme-ov-file#returns-1

If we receive HTML fragments as configuration and replace emoji markups with them,

  1. Parse the fragment strings into MDAST nodes in advance
  2. replaceEmoji transforms the input with replacing the emoji markups with MDAST nodes.
    For example,
    ':parrot:'
    should be transformed to
    {'hName': 'img', ...}

chad3814 added a commit to chad3814/remark-emoji that referenced this pull request Jun 5, 2024
I followed the code in rhysd#31 and @rhysd's comments on what code it
should return. Looking into MDAST, there's a `html` node, so I
used that and replaced the `aria()` with that as well.
@chad3814 chad3814 mentioned this pull request Jun 5, 2024
chad3814 added a commit to chad3814/remark-emoji that referenced this pull request Jun 5, 2024
I read through the code in rhysd#31 and @rhysd's comments about what should
be returned. I then read through the MDAST nodes and there's an Html
node, so I used that for the custom emoji as well as changed the aria
accessibility wrapping to use that, making it simpler.

Added test cases and tweaked the accessibility ones.
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

Successfully merging this pull request may close these issues.

[Feature Request] Custom emoji support
2 participants