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

[Markdown] $ is escaped as \$ if encountered once #6213

Closed
Pavek opened this issue Jun 11, 2019 · 29 comments · Fixed by #7938
Closed

[Markdown] $ is escaped as \$ if encountered once #6213

Pavek opened this issue Jun 11, 2019 · 29 comments · Fixed by #7938
Labels
lang:markdown Issues affecting Markdown locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:bug Issues identifying ugly output, or a defect in the program

Comments

@Pavek
Copy link

Pavek commented Jun 11, 2019

Prettier 1.18.2
Playground link

--parser markdown
--print-width 100
--prose-wrap always

Input:

Pagagraph with $14 million.

Pagagraph with $14 million. But if more $dollars on the same line...

Output:

Pagagraph with \$14 million.

Pagagraph with $14 million. But if more $dollars on the same line...

Expected behavior:

No escaping of dollar sign is expected:

Pagagraph with $14 million.

Pagagraph with $14 million. But if more $dollars on the same line...
@lydell
Copy link
Member

lydell commented Jun 11, 2019

#5425 (comment)

Since we now support math syntax ($ inline_math $), we need to escape $ to avoid unexpected parsing.

But it does seem a bit excessive to me. Maybe we can do this better.

@barlock
Copy link

barlock commented Jul 8, 2019

Uncertain if this is related or a new issue. remark escapes with \\ and prettier seems to only recognize one \. Running remark and prettier back to back causes \ to grow indefinitely.

@alexander-akait alexander-akait added lang:markdown Issues affecting Markdown type:bug Issues identifying ugly output, or a defect in the program labels Aug 7, 2019
@millette
Copy link

millette commented Aug 19, 2019

If we know we're not using math syntax, can this behavior be disabled?

UPDATE I tried prettier-ignore for markdown but it actually formatted the comment (indented it) and re-added the \ too.

UPDATE 2 prettier-ignore works at the top of the mdx file but applies to the whole file. It's not a great workaround.

@felixfbecker
Copy link

This is bad when the markdown target (e.g. static site generator) doesn't support math syntax so will render literal \$ into the HTML.

@jescalan
Copy link

jescalan commented Apr 2, 2020

I am also getting a blackslash rendered into the html output, which as far as I see it is a major bug in prettier's markdown formatting. I would be happy to put in a PR to help fix/remove this given some sort of direction or signoff from a maintainer. @ikatyang - any thoughts here?

@ikatyang
Copy link
Member

ikatyang commented Apr 3, 2020

@jescalan

I am also getting a blackslash rendered into the html output, which as far as I see it is a major bug in prettier's markdown formatting.

The behavior you mentioned sounds like a bug, but the backslash in escaped dollar sign should not be rendered into the html output according to the CommonMark spec. Since this issue (#6213) is about code style while yours sounds like a formatting bug, could you open a new issue with steps to reproduce?

@Pavek
Copy link
Author

Pavek commented Apr 4, 2020

@ikatyang I'm not sure what do you mean by "this issue is about code style "?

This issue is exactly about formatting: if author writes $14 million in Markdown, then end user sees \$14 million in HTML.

@ikatyang
Copy link
Member

ikatyang commented Apr 4, 2020

@Pavek

This issue is exactly about formatting: if author writes $14 million in Markdown, then end user sees \$14 million in HTML.

It looks like the Markdown renderer you used is not spec-compliant, I'm not sure what should we do in this case.

@lydell
Copy link
Member

lydell commented Apr 4, 2020

Maybe we can avoid printing a \ before $ if the $ occurs in a situation where it is impossible or low risk it will be interpreted as inline math?

But we don’t want to end up in a situtation where this edit:

-A gift of $14 million
+A gift of $14 million from John and $20 million from Jane

... suddenly results in accidental inline math that could have been avoided if Prettier had printed \$14 in the first place. I don’t know the rules for inline math, though.

@Pavek
Copy link
Author

Pavek commented Apr 4, 2020

@ikatyang

It looks like the Markdown renderer you used is not spec-compliant, I'm not sure what should we do in this case.

True, To be precise this is bug in Marked prior to version 0.4.0. My plan for this issue is to upgrade renderer.

But I argue that his behavior violates Prettier correctness rule:

The first requirement of Prettier is to output valid code that has the exact same behavior as before formatting. Please report any code where Prettier fails to follow these correctness rules — that's a bug which needs to be fixed!

In this case Prettier indeed outputs a valid code (both $1 and \$1 are valid according to spec), but behavior (end user experience) is not the same. Due to 3rd-party bug of course, but anyway.

@ikatyang
Copy link
Member

ikatyang commented Apr 5, 2020

I don’t know the rules for inline math, though.

I don't know either. That's the trickiest part, there is no formal definition for the math syntax in markdown. As far as I can tell, its definition is simply "content between dollar signs", the rest detail is up to implementers, which we followed the rules from remark-math at the moment.

But it seems remark-math has switched to use rules from pandoc last year:

Anything between two $ characters will be treated as TeX math. The opening $ must have a non-space character immediately to its right, while the closing $ must have a non-space character immediately to its left, and must not be followed immediately by a digit. Thus, $20,000 and $30,000 won’t parse as math. If for some reason you need to enclose text in literal $ characters, backslash-escape them and they won’t be treated as math delimiters.

Given that this new rule won't be that easy to be triggered, we should be able to avoid those unnecessary backslashes if we apply the same rule here.

@kristojorg
Copy link

kristojorg commented Apr 7, 2020

I am also seeing prettier escape "*" to "\*". Is this related?

@fisker
Copy link
Member

fisker commented Apr 7, 2020

I think this is getting worse when updating remark-math $10 - $20 also escaped(not escaped before)
6c4f3b0#diff-944fd19d93664b443ab3688b40edfbb3R54 , let's try remove the escape, and handle broken math syntax later.

@thorn0 thorn0 mentioned this issue Apr 8, 2020
4 tasks
@leerob
Copy link

leerob commented Apr 12, 2020

I'm seeing this as well. Are there any workarounds for the time being? I don't want to disable the entire file (MDX in my case).

@leon-do
Copy link

leon-do commented Apr 30, 2020

any workarounds?

@samjulien
Copy link

I'm having this same issue using MDX in Gatsby.

wallslide added a commit to appsocially/upil-site that referenced this issue Jun 18, 2020
@minimaldesign
Copy link

I am also seeing prettier escape "*" to "\*". Is this related?

I'm also seeing this in a markdown file where I'm trying to bold some stuff… I can't imagine I'm the only one? 🤔 Not sure if I should open another issue or…? I there's an open issue or solution out there, can you point me in the right direction? I haven't found it.

@lydell
Copy link
Member

lydell commented Jun 21, 2020

@minimaldesign Does \* cause any issues for you? If so, what markdown tooling are you using?

@minimaldesign
Copy link

minimaldesign commented Jun 21, 2020

@minimaldesign Does \* cause any issues for you? If so, what markdown tooling are you using?

Well, the first issue is that it's not standard markdown format, so it creates potential compatibility issues down the line.
Second issue: I'm putting together a blog using 11ty… If I try to make something **bold** it's rendered just like that (with the **...*** instead of bold).
Lastly, I don't want to get a bunch of unnecessary unrelated diffs when I update some old blog posts…

Thanks!

@lydell
Copy link
Member

lydell commented Jun 22, 2020

@minimaldesign What do you mean it’s not standard markdown? It’s supported in the original markdown implementation (https://daringfireball.net/projects/markdown/dingus) and in the CommonMark Spec: https://spec.commonmark.org/0.29/#backslash-escapes

@minimaldesign
Copy link

I mean it in the dictionary sense: normal, usual, typical, common, ordinary, customary, conventional, etc.

John Gruber:

The overriding design goal for Markdown’s formatting syntax is to make it as readable as possible. The idea is that a Markdown-formatted document should be publishable as-is, as plain text, without looking like it’s been marked up with tags or formatting instructions.

I don't think it's right to add unnecessary \ all over the place.

Maybe it's only important to me? But as I also mentioned initially, there are also tech ramifications like broken output and misleading diffs.

Thanks!

@lydell
Copy link
Member

lydell commented Jun 23, 2020

I personally like \* – makes it ultra clear that it was intended to be a star, and isn’t broken emphasis.

there are also tech ramifications like broken output

Where?

@minimaldesign
Copy link

I personally like \* – makes it ultra clear that it was intended to be a star, and isn’t broken emphasis.

Hmm, maybe I wasn't clear enough. I write:

Some **bold** text

And Prettier changes it to:

Some **bold\*\* text

I had to turn off Prettier on that page…

@thorn0
Copy link
Member

thorn0 commented Jun 23, 2020

@minimaldesign If Prettier indeed breaks your code, please open a new issue with a proper bug report. To do that, reproduce the problem on Prettier's Playground and click the Report issue button there.

@josephmarkus
Copy link

josephmarkus commented Jul 2, 2020

Is there a way to disable math syntax interpolation through Prettier config?

These are the only workarounds I found:

  • add prettier-ignore where dollar sign is used
  • disable prettier for a particular Markdown file
  • disable prettier for all Markdown files

None of which are ideal. I would argue that there are more plain text users than those that need inline math. Could we make this kind of interpolation optional through Prettier's configuration file?

@flikteoh
Copy link

Hi,

Has there been any concluded outcome of this issue?

Would definitely agree with options to disable math syntax through Prettier.

We're getting plenty of $ in mdx while we still wish to be using Prettier on our files.

@muescha
Copy link

muescha commented Jul 14, 2020

is the math only disabled if there are more supporters? - here is my one:

+1

@josephmarkus
Copy link

+1 @muescha

@jescalan
Copy link

🙏🏼 thank you so much for getting this fixed!

jgosmann added a commit to jgosmann/adventures that referenced this issue Jan 5, 2021
As prettier has finally been fixed to not re-add it.

See:
* prettier/prettier#6213
* mdx-js/mdx#606
@github-actions github-actions bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Feb 15, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lang:markdown Issues affecting Markdown locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:bug Issues identifying ugly output, or a defect in the program
Projects
None yet
Development

Successfully merging a pull request may close this issue.