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

Backslash escapes inside math and code are fontified as Markdown markup #766

Closed
saf-dmitry opened this issue May 21, 2023 · 9 comments
Closed

Comments

@saf-dmitry
Copy link
Contributor

saf-dmitry commented May 21, 2023

Backslashes inside math and code are fontified as Markdown markup.

Expected Behavior

Backslashes as part of math or code are fontified according to the type of content (i.e., LaTeX or corresponding code language).

Actual Behavior

Backslashes inside math or code are fontified and propertized as Markdown markup:

Screen Shot 2023-05-21 at 18 18 36

Screen Shot 2023-05-21 at 18 38 47

This is especially misleading when hiding Markdown syntax is enabled.

Steps to Reproduce

See the examples above.

Backtrace

Software Versions

  • Markdown Mode: From Git on 2023-05-21
  • Emacs: 28.2.1
  • OS: macOS 10.15

Note

Commit ae1085a

@saf-dmitry saf-dmitry changed the title Backslash escapes inside math are fontified as Markdown markup Backslash escapes inside math and code are fontified as Markdown markup May 21, 2023
saf-dmitry referenced this issue May 21, 2023
This ensures that when using `markdown-hide-markup`, escape sequences are
properly unescaped.
@jimporter
Copy link
Contributor

jimporter commented May 21, 2023

I think this would just need to add an extra check to not fontify backslash escapes when fontification has already added one of the following faces to the backslash:

  • markdown-inline-code-face
  • markdown-pre-face
  • markdown-math-face

That should be pretty straightforward with some new markdown-match-escape function that checks the backslash's face before returning whether it matched.

Maybe there's a cleaner way to do that though?

@saf-dmitry
Copy link
Contributor Author

I think this would just need to add an extra check to not fontify backslash escapes when fontification has already added one of the following faces to the backslash:

  • markdown-inline-code-face
  • markdown-pre-face
  • markdown-math-face

That should be pretty straightforward with some new markdown-match-escape function that checks the backslash's face before returning whether it matched.

Yes, that is how I would approach this. Another point to consider is, it should fontify only backslashes not preceding by another backslashes, i.e., in \\ only the first backslash should be fontified because the second backslash stands for itself. Generally speaking, in something like \\\\\\ only odd, i.e., 1, 3, and 5 backslash positions should be fontified.

@jimporter
Copy link
Contributor

Yes, that is how I would approach this. Another point to consider is, it should fontify only backslashes not preceding by another backslashes, i.e., in \\ only the first backslash should be fontified because the second backslash stands for itself. Generally speaking, in something like \\\\\\ only odd, i.e., 1, 3, and 5 backslash positions should be fontified.

This part should already work. The regexp is (approximately) \\., so it'll match a backslash followed by anything. That means it'll see the first backslash, then skip past the next one before trying again. If you type a bunch of backslashes in a row into a markdown-mode buffer, it should do the right thing.

@jimporter
Copy link
Contributor

@saf-dmitry Ok, hopefully my PR fixes this. Can you try it out to see?

@saf-dmitry
Copy link
Contributor Author

saf-dmitry commented May 21, 2023

I just played with it a little bit and it looks good.

The markdown-literal-faces variable can be re-used for various other purposes as well, so I would consider adding the following faces to the list:

  • markdown-url-face
  • markdown-plain-url-face
  • markdown-language-keyword-face
  • markdown-language-info-face
  • markdown-metadata-key-face
  • markdown-metadata-value-face
  • markdown-html-entity-face
  • markdown-html-tag-name-face
  • markdown-html-tag-delimiter-face
  • markdown-html-attr-name-face
  • markdown-html-attr-value-face

And maybe these faces too:

  • markdown-link-title-face
  • markdown-reference-face
  • markdown-footnote-marker-face
  • markdown-line-break-face
  • markdown-comment-face

@jimporter
Copy link
Contributor

markdown-link-title-face

Link titles look like they support escape sequences, so I think they should be excluded from markdown-literal-faces. The others make sense though (I think...)

@saf-dmitry
Copy link
Contributor Author

saf-dmitry commented Jul 14, 2024

It seems that the current solution works in code blocks only if native fontification is disabled (which is the default). If the variable markdown-fontify-code-blocks-natively is set to t, the backslash fontification issue comes again:

Screen Shot 2024-07-14 at 08 16 04

I think we should reopen the issue.

jimporter added a commit to jimporter/markdown-mode that referenced this issue Jul 14, 2024
Previously, if hiding markup and using native fontification of code blocks,
backslash escapes could get hidden inside code. See jrblevin#766.
@jimporter
Copy link
Contributor

It seems that the current solution works in code blocks only if native fontification is disabled (which is the default). If the variable markdown-fontify-code-blocks-natively is set to t, the backslash fontification issue comes again:

@saf-dmitry Thanks for noticing, here's a fix: #836

@saf-dmitry
Copy link
Contributor Author

Thank you for the prompt fix. It works.

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

2 participants