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

Change order of code block formatting to prevent removal of backticks #301

Merged
merged 2 commits into from
Nov 1, 2021

Conversation

mtandre
Copy link
Member

@mtandre mtandre commented Oct 27, 2021

Description of Change

This adjusts the order of the triple backtick code block formatting so that backticks (`) in the code are preserved.

The formatCodeContent() function replaces backticks with their html entities and then the conversion to text via cheerio.text() reverts them back into backticks. So then, when the single backtick formatter runs it replaces those backticks within the triple backtick code blocks and the disappear from the rendered code block. This prevents that by moving the formatCodeContent() to after the cheerio.text() conversion.

This also add a backtick example into the triple backtick code block in the supportedFormats.html fixture for testing.

Related Issue

Fixes #294

Motivation and Context

I recently ran into this issue where backtick in code blocks were disappearing and noticed there was open issue for it.

Checklist

  • Ran npm run lint and updated code style accordingly
  • npm run test passes
  • PR has a description and all contributors/stakeholder are noted/cc'ed
  • tests are updated and/or added to cover new code
  • relevant documentation is changed and/or added

@mtandre mtandre requested a review from afischer October 27, 2021 21:43
Copy link
Member

@afischer afischer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mtandre, thanks for the contribution (and added test!)

Still seeing slightly weird behavior:

  • Inline code seems to still incorrectly escape backticks
  • Block code with a language is great!
  • Block code without a language eats the first line (NB: I just tested this on main and it is still an issue, so this is a different bug. Feel free to fix if you can in this PR, otherwise I we can follow up

image

Here's the text I used to generate this:

Inline code `const js = `one ${true} language`;`


Block code with language:


```js
const js = `one ${true} language`
console.log(js)
```
Block code without language:
```
const js = `one ${true} language`
console.log(js)
```

- add support for backticks in inline code blocks via double backticks per markdown "spec"
- fix bug that eats first line of code block when no language hint exists
@mtandre
Copy link
Member Author

mtandre commented Oct 28, 2021

@afischer I've updated the PR:

  • support the backticks in inline code blocks by using double backticks, similar to markdown
  • fix the bug that eats first line of a code block when no language hint exists by updating the regex and moving the removal of the language hint to after it is confirmed as a language

Let me know your thoughts.

Copy link
Member

@afischer afischer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! I'll add examples of all these to our supported documents file.

@afischer afischer merged commit cd5251f into main Nov 1, 2021
@afischer afischer deleted the bug-code-backtick branch November 1, 2021 14:42
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.

Single backticks in code blocks getting substituted out
2 participants