Skip to content

[V2] Copy button in CodeBlock ignores blank line #2284

Closed

Description

@kohheepeace

🐛 Bug Report

Copy code by using "Copy" button in code block ignores blank lines when I paste to editor.

Have you read the Contributing Guidelines on issues?

yes

To Reproduce

Step 1... Visit https://v2.docusaurus.io/docs/creating-pages#adding-a-new-page
Step 2... Click "Copy" button in CodeBlock
Step 3... Paste to editor

Expected behavior

Code should be pasted to the editor as it is seen in document.

Actual Behavior

This is a gif to reproduce the behavior which I think bug.
2020-02-15 17 33 51

Reproducible Demo

You can reproduce this bug in https://v2.docusaurus.io/docs/creating-pages#adding-a-new-page

Working Around

Add this code in src/theme/CodeBlock/index.js

if (line.length === 1 && line[0].content === '') {
  line[0].content = "\n";
}

So code looks like...

src/theme/CodeBlock/index.js

...
if (line.length === 1 && line[0].content === '') {
  line[0].content = '\n'; // eslint-disable-line no-param-reassign
}

const lineProps = getLineProps({line, key: i});

if (highlightLines.includes(i + 1)) {
  lineProps.className = `${lineProps.className} docusaurus-highlight-code-line`;
}

return (
  <div key={i} {...lineProps}>
    {line.map((token, key) => (
      <span key={key} {...getTokenProps({token, key})} />
    ))}
  </div>
);
...

You can check it works here.
https://my-website-docusaurus.kohheepeace.now.sh/docs/creating-pages

I swizzled src/theme/CodeBlcok/index.js and add the below code.

if (line.length === 1 && line[0].content === '') {
  line[0].content = "\n";
}

*Sorry if this is not bug! Thanks for making docusaurus 😄

Activity

  1. added
    bugAn error in the Docusaurus core causing instability or issues with its execution
    status: needs triageThis issue has not been triaged by maintainers
    on Feb 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn error in the Docusaurus core causing instability or issues with its executionstatus: needs triageThis issue has not been triaged by maintainers

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions