squidfunk / mkdocs-material

Documentation that simply works
https://squidfunk.github.io/mkdocs-material/
MIT License
18.65k stars 3.35k forks source link

"Copy" in code blocks inject double newlines #7170

Closed jippi closed 2 weeks ago

jippi commented 3 weeks ago

Context

I want to make it easier for folks to copy configuration examples from my documentation page, by using the copy feature.

Bug description

When clicking the copy icon in all code blcoks on my documentation site, I get extra blank newlines in between the content.

I initially thought it was due to Annotations in code blocks, but does not seem to be the case.

Manually "drag cursor over text and copy" works fine.

Related links

Reproduction

Click any copy icon from my related links section and see that you get double newlines

For example; the Generate labels via script code block:

Expected

label:
  # Generate list of labels via script
  - strategy: generate
    # With a description (optional)
    description: "Modified this service directory"
    # With the color $pink
    color: "$pink"
    # From this script, returning a list of labels
    script: >
      /* Generate a list of files changed in the MR inside pkg/service/ */
      merge_request.modified_files_list("pkg/service/")

      /* Remove the filename from the path
        pkg/service/example/file.go => pkg/service/example */
      | map({ filepath_dir(#) })

      /* Remove the prefix "pkg/" from the path
         pkg/service/example => service/example */
      | map({ trimPrefix(#, "pkg/") })

      /* Remove duplicate values from the output */
      | uniq()

Actual

label:

  # Generate list of labels via script

  - strategy: generate

    # With a description (optional)

    description: "Modified this service directory"

    # With the color $pink

    color: "$pink"

    # From this script, returning a list of labels

    script: >

      /* Generate a list of files changed in the MR inside pkg/service/ */

      merge_request.modified_files_list("pkg/service/")

      /* Remove the filename from the path

        pkg/service/example/file.go => pkg/service/example */

      | map({ filepath_dir(#) })

      /* Remove the prefix "pkg/" from the path

         pkg/service/example => service/example */

      | map({ trimPrefix(#, "pkg/") })

      /* Remove duplicate values from the output */

      | uniq()

Steps to reproduce

  1. Go to any page
  2. Click the copy icon
  3. Paste into any editor or window

Browser

No response

Before submitting

facelessuser commented 3 weeks ago

This seems to have been introduced in one of the recent Material releases. Locally, I had mkdocs-material-9.5.6, but after updating to mkdocs-material-9.5.21, I was able to reproduce the issue.

I should note that I could only reproduce with the line_spans: __span option. I suspect that the copy logic gets tripped up by line spans and injects extra new lines. That is my guess at least.

squidfunk commented 3 weeks ago

Thanks for reporting. Please provide a minimal reproduction, as requested by our bug reporting guidelines.

jippi commented 3 weeks ago

Bisecting releases, it works as expected in 9.5.17 and fails in 9.5.18 and above; and removing line_spans: __span makes it work across all versions

squidfunk commented 2 weeks ago

Fixed in 937d7ccff103c96be0fa086cb30909bb1844b1fc. I was able to reproduce it and found out that it's related to https://github.com/squidfunk/mkdocs-material/commit/4eb1a4393741b1ecf8019dd53292b7a4d02e16f5, which fixes line highlights on mobile, but introduced the side effect of empty lines when copying.

luonghuyquang commented 2 weeks ago

You did 'Fixed empty lines when copying (9.5.18 regression)'. Do we need to install specific version 9.5.18? An upgrade would install 9.5.21 again.

squidfunk commented 2 weeks ago

I'm in the process of creating a release for this. "9.5.18 regression" means the bug was introduced in 9.5.18 and it worked before, which was confirmed by the OP, saying that downgrading to 9.5.17 fixes the issue.

squidfunk commented 2 weeks ago

Released as part of 9.5.22.

jippi commented 2 weeks ago

Thank you @squidfunk ❤️