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

fix: remove trailing newline added by markdown-it #585

Merged
merged 6 commits into from
Feb 12, 2024
Merged

Conversation

KermanX
Copy link
Contributor

@KermanX KermanX commented Feb 11, 2024

Description

Scope: @shikijs/markdown-it

Shiki transpiles blank lines into <span class="line"></span>. And the source code received in markdownit.options.highlight has a trailing new line. This results in a blank line in the rendered HTML.

This PR fixes this by removing the last character of the source code.

Example input:

<!-- Blank -->
'''ts
'''

<!-- One blank line -->
'''ts

'''

<!-- Normal code -->
'''ts
123
'''

Output (simplified):

<!-- Blank -->
<pre><code>
  <span class="line"></span>
</code></pre>

<!-- One blank line -->
<pre><code>
  <span class="line"></span>
</code></pre>

<!-- Normal code -->
<pre><code>
  <span class="line">123</span>
</code></pre>

Linked Issues

fix slidevjs/slidev#1276

Copy link

netlify bot commented Feb 11, 2024

Deploy Preview for shiki-next ready!

Name Link
🔨 Latest commit bb410c3
🔍 Latest deploy log https://app.netlify.com/sites/shiki-next/deploys/65ca0bc23da1c000085497bb
😎 Deploy Preview https://deploy-preview-585--shiki-next.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Feb 11, 2024

Deploy Preview for shiki-matsu ready!

Name Link
🔨 Latest commit bb410c3
🔍 Latest deploy log https://app.netlify.com/sites/shiki-matsu/deploys/65ca0bc2150d10000862973a
😎 Deploy Preview https://deploy-preview-585--shiki-matsu.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@codecov-commenter
Copy link

codecov-commenter commented Feb 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (2f64ffd) 96.14% compared to head (d284098) 96.15%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #585   +/-   ##
=======================================
  Coverage   96.14%   96.15%           
=======================================
  Files          70       70           
  Lines        5651     5664   +13     
  Branches      749      749           
=======================================
+ Hits         5433     5446   +13     
  Misses        212      212           
  Partials        6        6           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -50,7 +50,7 @@ export function setupMarkdownIt(
})

return highlighter.codeToHtml(
code,
code.slice(0, -1),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
code.slice(0, -1),
code.trimEnd(),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was wondering maybe there will be someone who intended to have a blank line or whitespaces at the end of the code?🤔

Copy link
Member

Choose a reason for hiding this comment

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

I found that VitePress is doing this mandatory: https://github.com/vuejs/vitepress/blob/c582a8d5fd82b84d412c7e6c84e74faeb23beac6/src/node/markdown/plugins/highlight.ts#L144

I wonder why markdown-it is doing this (if it always adds a tailing newline) - do you have a reference? .slice(0, -1) looks a bit magic and unreliable to me as it might strip out real code if the tailing newline is not a determined behavior.

Copy link
Contributor Author

@KermanX KermanX Feb 11, 2024

Choose a reason for hiding this comment

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

I think markdown-it always adds a tailing newline, bacause:

https://github.com/markdown-it/markdown-it/blob/master/lib/rules_block/fence.mjs#L89:

  token.content = state.getLines(startLine + 1, nextLine, len, true)
//                                                              ^ "keepLastLF"

and https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.mjs#L43:

    highlighted = options.highlight(token.content, langName, langAttrs) || escapeHtml(token.content)

also https://github.com/markdown-it/markdown-it/blob/master/lib/rules_block/code.mjs#L26:

  token.content = state.getLines(startLine, last, 4 + state.blkIndent, false) + '\n'

@antfu antfu merged commit b559cde into shikijs:main Feb 12, 2024
12 checks passed
diegohaz referenced this pull request in ariakit/ariakit Feb 23, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [shiki](https://github.com/shikijs/shiki)
([source](https://github.com/shikijs/shiki/tree/HEAD/packages/shiki))
| [`1.1.1` ->
`1.1.6`](https://renovatebot.com/diffs/npm/shiki/1.1.1/1.1.6) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/shiki/1.1.6?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/shiki/1.1.6?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/shiki/1.1.1/1.1.6?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/shiki/1.1.1/1.1.6?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>shikijs/shiki (shiki)</summary>

### [`v1.1.6`](https://github.com/shikijs/shiki/releases/tag/v1.1.6)

[Compare
Source](https://github.com/shikijs/shiki/compare/v1.1.5...v1.1.6)

#####    🚀 Features

- Provide lang sub module for alias  -  by
[@&#8203;antfu](https://github.com/antfu) in
[https://github.com/shikijs/shiki/issues/596](https://github.com/shikijs/shiki/issues/596)
[<samp>(b61a6)</samp>](https://github.com/shikijs/shiki/commit/b61a689b)
- Include new `Typst` grammar  -  by
[@&#8203;antfu](https://github.com/antfu)
[<samp>(9f70e)</samp>](https://github.com/shikijs/shiki/commit/9f70eed4)

#####    🐞 Bug Fixes

- **vitepress**: Missing `:shown` prop for query, fix
[#&#8203;595](https://github.com/shikijs/shiki/issues/595)  -  by
[@&#8203;antfu](https://github.com/antfu) in
[https://github.com/shikijs/shiki/issues/595](https://github.com/shikijs/shiki/issues/595)
[<samp>(b667b)</samp>](https://github.com/shikijs/shiki/commit/b667b75a)

#####     [View changes on
GitHub](https://github.com/shikijs/shiki/compare/v1.1.5...v1.1.6)

### [`v1.1.5`](https://github.com/shikijs/shiki/releases/tag/v1.1.5)

[Compare
Source](https://github.com/shikijs/shiki/compare/v1.1.4...v1.1.5)

#####    🐞 Bug Fixes

- **monaco**: Editor colors interop, fix
[#&#8203;594](https://github.com/shikijs/shiki/issues/594)  -  by
[@&#8203;antfu](https://github.com/antfu) in
[https://github.com/shikijs/shiki/issues/594](https://github.com/shikijs/shiki/issues/594)
[<samp>(78825)</samp>](https://github.com/shikijs/shiki/commit/788250d7)

#####     [View changes on
GitHub](https://github.com/shikijs/shiki/compare/v1.1.4...v1.1.5)

### [`v1.1.4`](https://github.com/shikijs/shiki/releases/tag/v1.1.4)

[Compare
Source](https://github.com/shikijs/shiki/compare/v1.1.3...v1.1.4)

#####    🚀 Features

- Add length and time limit to tokenizer  -  by
[@&#8203;ije](https://github.com/ije) and
[@&#8203;antfu](https://github.com/antfu) in
[https://github.com/shikijs/shiki/issues/588](https://github.com/shikijs/shiki/issues/588)
[<samp>(2803f)</samp>](https://github.com/shikijs/shiki/commit/2803f898)

#####    🐞 Bug Fixes

- **monaco**: Normalize hex color for monaco, fix
[#&#8203;594](https://github.com/shikijs/shiki/issues/594)  -  by
[@&#8203;antfu](https://github.com/antfu) in
[https://github.com/shikijs/shiki/issues/594](https://github.com/shikijs/shiki/issues/594)
[<samp>(ecb36)</samp>](https://github.com/shikijs/shiki/commit/ecb36e23)

#####     [View changes on
GitHub](https://github.com/shikijs/shiki/compare/v1.1.3...v1.1.4)

### [`v1.1.3`](https://github.com/shikijs/shiki/releases/tag/v1.1.3)

[Compare
Source](https://github.com/shikijs/shiki/compare/v1.1.2...v1.1.3)

#####    🚀 Features

- New terraform lang, update deps  -  by
[@&#8203;antfu](https://github.com/antfu)
[<samp>(52d0a)</samp>](https://github.com/shikijs/shiki/commit/52d0a62d)

#####    🐞 Bug Fixes

- **vitepress**: Don't set exit code when throws is set to false  -  by
[@&#8203;antfu](https://github.com/antfu)
[<samp>(e2a0a)</samp>](https://github.com/shikijs/shiki/commit/e2a0a432)

#####     [View changes on
GitHub](https://github.com/shikijs/shiki/compare/v1.1.2...v1.1.3)

### [`v1.1.2`](https://github.com/shikijs/shiki/releases/tag/v1.1.2)

[Compare
Source](https://github.com/shikijs/shiki/compare/v1.1.1...v1.1.2)

#####    🚀 Features

- New lang Move and new theme Vesper  -  by
[@&#8203;antfu](https://github.com/antfu)
[<samp>(8247b)</samp>](https://github.com/shikijs/shiki/commit/8247ba72)

#####    🐞 Bug Fixes

- Remove trailing newline added by markdown-it  -  by
[@&#8203;KermanX](https://github.com/KermanX) and
[@&#8203;antfu](https://github.com/antfu) in
[https://github.com/shikijs/shiki/issues/585](https://github.com/shikijs/shiki/issues/585)
[<samp>(b559c)</samp>](https://github.com/shikijs/shiki/commit/b559cde5)
- **twoslash**: Improve error handling  -  by
[@&#8203;antfu](https://github.com/antfu)
[<samp>(73fdb)</samp>](https://github.com/shikijs/shiki/commit/73fdbd17)

#####     [View changes on
GitHub](https://github.com/shikijs/shiki/compare/v1.1.1...v1.1.2)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/ariakit/ariakit).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xNTMuMiIsInVwZGF0ZWRJblZlciI6IjM3LjIwMC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
GrimLink added a commit to GrimLink/astro that referenced this pull request Nov 29, 2024
This is a similar solution as seen in shikijs/shiki#585
GrimLink added a commit to GrimLink/astro that referenced this pull request Nov 29, 2024
This is a similar solution as seen in shikijs/shiki#585
GrimLink added a commit to GrimLink/astro that referenced this pull request Nov 29, 2024
This is a similar solution as seen in shikijs/shiki#585
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.

Highlighter shiki adds a trailing new line to code blocks
3 participants