blyxyas / mdbook-emojicodes

MDBook preprocessor for converting emojicodes (e.g. `: cat :`) into emojis 🐱
https://crates.io/crates/mdbook-emojicodes
MIT License
11 stars 2 forks source link

Do not render emojis in raw markdown blocks #7

Closed kdheepak closed 12 months ago

kdheepak commented 1 year ago

Thanks for making this plugin!

I would like to add the following section to a developer guide in a mdbook generated documentation:

### `mdbook-emojicodes`

The following raw markdown:

```markdown
I love cats :cat: and dogs :dog:, I have two, one's gray, like a raccoon :raccoon:, and the other one is black, like the night :night_with_stars:.

will render as the following:

I love cats :cat: and dogs :dog:, I have two, one's gray, like a raccoon :raccoon:, and the other one is black, like the night :night_with_stars:.



---

This is how it renders on GitHub:

> ### `mdbook-emojicodes`
> 
> The following raw markdown:
> 
> ```markdown
> I love cats :cat: and dogs :dog:, I have two, one's gray, like a raccoon :raccoon:, and the other one is black, like the night :night_with_stars:.
> ```
> 
> will render as the following:
> 
> I love cats :cat: and dogs :dog:, I have two, one's gray, like a raccoon :raccoon:, and the other one is black, like the night :night_with_stars:.

This is how it renders in `mdbook`:

> <img width="815" alt="image" src="https://github.com/blyxyas/mdbook-emojicodes/assets/1813121/f2ddcd0e-4efe-46f7-abc2-af8e9fd07eed">

---

Notice that in the markdown blocks in `mdbook` it still converts `:cat:` to :cat: but it doesn't do that on GitHub.

I think it would be nice if the plugin would leave any code blocks that are titled `markdown` alone. 

Initially I was thinking any code blocks should be left alone but then it wouldn't be possible to use emojicodes in admonish blocks using [`mdbook-admonish`](https://github.com/tommilligan/mdbook-admonish). 

Something else to consider is if someone has comments with `:tada:` in a `python` code block, should it be rendered as a emoji? Ideally it would be configurable per code block.
blyxyas commented 1 year ago

Good suggestion, I'm currently kinda busy, but I'll take a look ASAP, it will be free until then! :heart:

kdheepak commented 1 year ago

No worries! Take your time!

kdheepak commented 1 year ago

btw, feel free to ignore the comment if you are busy. I'm just curious.

I see you've added a "good first issue" label. I'm fairly new to mdbook so I don't know how you might go about handling this. But if you were to give me some direction, I'm happy to submit a PR.

I assumed you would have to pull in a dependency like pulldown_mark to figure out whether you are in a codeblock or not, and decide whether to parse based on that. Do you have a simpler approach in mind?

blyxyas commented 1 year ago

We can use the pulldown_cmark crate (preferably the same version that mdbook uses) but that would slow down generation. An additional regex could be added for detecting pairs of codefences (```), and checking if the current match character position is in between one of those pairs would be pretty easy. It would also check that the type is "markdown" or "md". If you're experienced with Rust, it should be kinda easy.

If you don't feel comfortable trying this out, always feel free to say so! Solving this issue isn't a compromise :sweat_smile: and I'm always willing to help here.

kdheepak commented 1 year ago

I’m fairly comfortable in Rust! So I wouldn’t mind giving it a shot. I did go through the code and figured I could easily do it by splitting the content on triple backticks that start at the beginning of a line, and only splitting the logic to every alternate chunk.

I think there are some corner cases though. If someone has a quadruple back tick that wouldn’t work.

I guess I can special case for that and call it good enough? What do you think?

blyxyas commented 1 year ago

Yeah it's probably good enough, we can use some regex syntax to account for four or more backticks. Even then, it's a very uncommon case so I think it's good enough :+1:

kdheepak commented 1 year ago

I made a PR for this. It handles code blocks correctly imo, but does not handle admonish codeblocks. I added a test showing what it does for admonish blocks (i.e. the same as normal codeblocks).