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

[Bug] Notation transfomers remove empty annotated lines #589

Open
5 tasks done
lachieh opened this issue Feb 15, 2024 · 8 comments
Open
5 tasks done

[Bug] Notation transfomers remove empty annotated lines #589

lachieh opened this issue Feb 15, 2024 · 8 comments

Comments

@lachieh
Copy link

lachieh commented Feb 15, 2024

Validations

Describe the bug

It seems that all of the notation transformers like transformerNotationHighlight and transformerNotationDiff that if the line is empty, the span.line is removed entirely.

For example, the following code run through codeToHtml:

hello.world() // [!code highlight]
// [!code highlight]

Results in:

<pre>
  <code>
    <span class="line highlighted"><span>hello</span><span>.</span><span>world</span><span>()</span></span>

  </code>
</pre>

But I would assume it should be:

<pre>
  <code>
    <span class="line highlighted"><span>hello</span><span>.</span><span>world</span><span>()</span></span>
    <span class="line highlighted"></span>
  </code>
</pre>

Reproduction

https://stackblitz.com/edit/vitejs-vite-axpred?file=main.js

Contributes

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests
@lachieh lachieh changed the title Notation transfomers removes empty annotated lines [Bug] Notation transfomers remove empty annotated lines Feb 15, 2024
@antfu
Copy link
Member

antfu commented Feb 16, 2024

This is intentional and expected behavior. I am ok with exposing an option to toggle that, pr welcome

@lachieh
Copy link
Author

lachieh commented Feb 16, 2024

Thanks for the reply. It feels a little unexpected since the span.line is not removed unless there is a notation comment. Updated example above to demonstrate. Is this documented?

How would you indicate a diff that includes a new line as part of the code change?

@antfu
Copy link
Member

antfu commented Feb 16, 2024

I guess it's not well-documented. It was migrated from https://github.com/innocenzi/shiki-processor and I kept the original behavior.

You can do // [!code highlight:2] on the first line to highlight the following two lines.

@lachieh
Copy link
Author

lachieh commented Feb 16, 2024

Thanks, that gets me across the line for now.

I still think that this is probably still a bug since it is doing the opposite of what the notation comment is suggesting it will. Combined with the transformerRemoveLineBreak transformer and a .line { display: block } then the output wraps onto the previous line.

I'd like to suggest that an option to toggle is added, but the default behavior be that it just does what the comment says it will. I'm happy to submit a PR.

@lachieh
Copy link
Author

lachieh commented Feb 20, 2024

Small follow-up to document behavior. The multi-line notation // [!code highlight:2] also removes the starting span.line if the comment is on an otherwise empty line.

lachieh added a commit to wasmCloud/wasmcloud.com that referenced this issue Feb 20, 2024
see shikijs/shiki#589

Signed-off-by: Lachlan Heywood <lachieh@users.noreply.github.com>
lachieh added a commit to wasmCloud/wasmcloud.com that referenced this issue Feb 21, 2024
see shikijs/shiki#589

Signed-off-by: Lachlan Heywood <lachieh@users.noreply.github.com>
lachieh added a commit to wasmCloud/wasmcloud.com that referenced this issue Feb 21, 2024
see shikijs/shiki#589

Signed-off-by: Lachlan Heywood <lachieh@users.noreply.github.com>
lachieh added a commit to wasmCloud/wasmcloud.com that referenced this issue Feb 21, 2024
see shikijs/shiki#589

Signed-off-by: Lachlan Heywood <lachieh@users.noreply.github.com>
ericgregory pushed a commit to wasmCloud/wasmcloud.com that referenced this issue Mar 18, 2024
see shikijs/shiki#589

Signed-off-by: Lachlan Heywood <lachieh@users.noreply.github.com>
@rishi-raj-jain
Copy link

Hey, I am writing this code block with Shiki:
Screenshot 2024-04-14 at 12 45 46 PM

and the output is as follows:
Screenshot 2024-04-14 at 12 45 54 PM

This is my shikiConfig:

    shikiConfig: {
      theme: 'github-dark',
      transformers: [transformerNotationDiff()],
    },

but I do not want the span.line if there's nothing in it. How do I get that?

@lachieh
Copy link
Author

lachieh commented Apr 14, 2024

Hey @rishi-raj-jain, that is not the same issue as this one and it is probably not shiki that is responsible for that extra line. It's probably whatever you're using for markdown processing.

I have tested it in an example and the output looks like this:

<code>
  <span class="line">
    <span style="color:#B8A965">cd</span>
    <span style="color:#C98A7D"> my-app</span>
  </span>
  <span class="line">
    <span style="color:#80A665">npm</span>
    <span style="color:#C98A7D"> run dev</span>
  </span>
</code>

As a fix, you could add this to your css which will hide the span if it is the last child and it has nothing in it:

span.line:last-child:empty {
   display: none;
}

If you still believe it is a problem with shiki, I'd suggest creating a minimal reproducible example of your own and posting a new issue.

@rishi-raj-jain
Copy link

Hey @lachieh,

Thank you for your quick informative response! I was searching for the empty lines issue and this is the closest thread I could find. Pardon the inconvenience caused 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants