Copy & paste from code block to nano editor issue #3895

Open
opened 2026-02-05 07:47:44 +03:00 by OVERLORD · 6 comments
Owner

Originally created by @jeffshead on GitHub (Jul 2, 2023).

Attempted Debugging

  • I have read the debugging page

Searched GitHub Issues

  • I have searched GitHub for the issue.

Describe the Scenario

I don't consider this a BookStack bug but since BookStack has code blocks to make it easy to copy/paste code, I believe this "enhancement" is worth considering if it can be implemented.

ISSUE:

When coping multiple lines from a BookStack code block and pasting them into nano, in a terminal window, the new line characters are lost or replaced and some lines are out of order, as depicted in the image below:

missing line breaks

It should look like the image below (copy/paste from EditPad Pro):

with line breaks

Work-around:

Copy code from the BookStack code-block, paste the code into a wordpad app such as EditPad Pro and then copy the text from the wordpad app and paste that into nano.

I get the same results with PuTTY, KiTTY, MobaXterm, etc. I'm working on a Windows PC. Not sure if that matters.

Additional info:

https://github.com/vercel/hyper/issues/1448#issuecomment-367890105
http://savannah.gnu.org/bugs/?49176#comment5

Exact BookStack Version

23.05.2

Log Content

N/A

PHP Version

8.x

Hosting Environment

AlmaLinux 8.8, Docker

Originally created by @jeffshead on GitHub (Jul 2, 2023). ### Attempted Debugging - [X] I have read the debugging page ### Searched GitHub Issues - [X] I have searched GitHub for the issue. ### Describe the Scenario I don't consider this a BookStack bug but since BookStack has code blocks to make it easy to copy/paste code, I believe this "enhancement" is worth considering if it can be implemented. ### ISSUE: When coping multiple lines from a BookStack code block and pasting them into nano, in a terminal window, the new line characters are lost or replaced and some lines are out of order, as depicted in the image below: ![missing line breaks](https://i.imgur.com/nHLoiWq.png) It should look like the image below (copy/paste from EditPad Pro): ![with line breaks](https://i.imgur.com/FsWZIFp.png) ### Work-around: Copy code from the BookStack code-block, paste the code into a wordpad app such as EditPad Pro and then copy the text from the wordpad app and paste that into nano. I get the same results with PuTTY, KiTTY, MobaXterm, etc. I'm working on a Windows PC. Not sure if that matters. ### Additional info: https://github.com/vercel/hyper/issues/1448#issuecomment-367890105 http://savannah.gnu.org/bugs/?49176#comment5 ### Exact BookStack Version 23.05.2 ### Log Content N/A ### PHP Version 8.x ### Hosting Environment AlmaLinux 8.8, Docker
OVERLORD added the 🐛 Bug📝 WYSIWYG Editor labels 2026-02-05 07:47:44 +03:00
Author
Owner

@ssddanbrown commented on GitHub (Jul 3, 2023):

Thanks @jeffshead.

Not really sure where to take this for BookStack.
We could change line endings depending on platform, which is what it looks like VSCode did, but I am hesitant since as far as I can tell we've always provided \n endings for code blocks, and it's hard to foresee the potential corner cases this change may affect and their significance relative to your scenario.
It is something we can control though rather simply via CodeMirror config.

@ssddanbrown commented on GitHub (Jul 3, 2023): Thanks @jeffshead. Not really sure where to take this for BookStack. We could change line endings depending on platform, which is what it [looks like VSCode did](https://github.com/microsoft/vscode/commit/c935020a8aead42e97614ca875853d26089d5e1a), but I am hesitant since as far as I can tell we've always provided `\n` endings for code blocks, and it's hard to foresee the potential corner cases this change may affect and their significance relative to your scenario. It is something we can control though [rather simply via CodeMirror config](https://codemirror.net/docs/ref/#state.EditorState^lineSeparator).
Author
Owner

@kobaz commented on GitHub (Mar 16, 2024):

Having also a problem problem pasting into a console, or emacs... etc

copy/paste this code from a code block:

START TRANSACTION;
WITH delete_needed AS (
 select phone_directory_id from asterisk.v_phone_buttons where compressed_mac is null
)
DELETE FROM
 asterisk.phone_buttons
WHERE
 phone_directory_id IN (SELECT phone_directory_id FROM delete_needed)
RETURNING *
;


And it looks like this when using a straight console
2024-03-15_23-01

The arrows are pointing to non-ascii characters that are getting pasted in... if you paste this into emacs it will completely mash up the paste where it looks like this:

2024-03-15_23-02

@kobaz commented on GitHub (Mar 16, 2024): Having also a problem problem pasting into a console, or emacs... etc copy/paste this code from a code block: ``` START TRANSACTION; WITH delete_needed AS ( select phone_directory_id from asterisk.v_phone_buttons where compressed_mac is null ) DELETE FROM asterisk.phone_buttons WHERE phone_directory_id IN (SELECT phone_directory_id FROM delete_needed) RETURNING * ; ``` And it looks like this when using a straight console ![2024-03-15_23-01](https://github.com/BookStackApp/BookStack/assets/1695852/55e4d12e-bf3c-4fb0-b54b-824fb8f19c28) The arrows are pointing to non-ascii characters that are getting pasted in... if you paste this into emacs it will completely mash up the paste where it looks like this: ![2024-03-15_23-02](https://github.com/BookStackApp/BookStack/assets/1695852/cb25480b-16d9-4056-bf31-6904dec751dc)
Author
Owner

@bdolez commented on GitHub (Dec 4, 2024):

Hi,

I don't know if there is any news regarding this issue. I identified that every raw space in the copied text was replaced with a UTF8 "non-breaking space" combination ( "\302\240" ). Is it possible to disable this and use a simple space character ?

@bdolez commented on GitHub (Dec 4, 2024): Hi, I don't know if there is any news regarding this issue. I identified that every raw space in the copied text was replaced with a UTF8 "non-breaking space" combination ( "\302\240<space>" ). Is it possible to disable this and use a simple space character ?
Author
Owner

@ssddanbrown commented on GitHub (Dec 4, 2024):

Note that there are many variables that could affect this (source content, browser, terminal, paste/clipboard method/source, target application, OS).

As a baseline, here's some testing steps and my results:


Steps

  1. Go to https://demo.bookstackapp.com/books/bookstack-demo-site/page/mixed-content-example-page
  2. Copy the contents of the "Code Example" section.
  3. Paste that into terminal, to store in a file (eg. in text editor).
  4. Dump the hex contents via something like xxd.

Environment

  • Browser: Firefox 133
  • OS: Fedora 41
  • Terminal: Gnome Terminal 3.54.0
  • Shell: ZSHELL
  • Copy & paste steps: Select all via mouse > copy > paste into nano editor via mouse > save

Result

> xxd code.txt 
00000000: 2f2a 2a0a 202a 2047 6574 2061 2070 6174  /**. * Get a pat
00000010: 6820 746f 2061 2074 6865 6d65 2072 6573  h to a theme res
00000020: 6f75 7263 652e 0a20 2a2f 0a66 756e 6374  ource.. */.funct
00000030: 696f 6e20 7468 656d 655f 7061 7468 2873  ion theme_path(s
00000040: 7472 696e 6720 2470 6174 6820 3d20 2727  tring $path = ''
00000050: 293a 203f 7374 7269 6e67 0a7b 0a20 2020  ): ?string.{.   
00000060: 2024 7468 656d 6520 3d20 636f 6e66 6967   $theme = config
00000070: 2827 7669 6577 2e74 6865 6d65 2729 3b0a  ('view.theme');.
00000080: 0a20 2020 2069 6620 2821 2474 6865 6d65  .    if (!$theme
00000090: 2920 7b0a 2020 2020 2020 2020 7265 7475  ) {.        retu
000000a0: 726e 206e 756c 6c3b 0a20 2020 207d 0a0a  rn null;.    }..
000000b0: 2020 2020 7265 7475 726e 2062 6173 655f      return base_
000000c0: 7061 7468 2827 7468 656d 6573 2f27 202e  path('themes/' .
000000d0: 2024 7468 656d 6520 2e20 2824 7061 7468   $theme . ($path
000000e0: 203f 2044 4952 4543 544f 5259 5f53 4550   ? DIRECTORY_SEP
000000f0: 4152 4154 4f52 202e 2024 7061 7468 203a  ARATOR . $path :
00000100: 2024 7061 7468 2929 3b0a 7d0a             $path));.}.
  • Standard newlines retained, code presents as expected.
  • Spaces are retained as standard spaces.
@ssddanbrown commented on GitHub (Dec 4, 2024): Note that there are many variables that could affect this (source content, browser, terminal, paste/clipboard method/source, target application, OS). As a baseline, here's some testing steps and my results: --- ### Steps 1. Go to https://demo.bookstackapp.com/books/bookstack-demo-site/page/mixed-content-example-page 2. Copy the contents of the "Code Example" section. 3. Paste that into terminal, to store in a file (eg. in text editor). 4. Dump the hex contents via something like `xxd`. --- ### Environment - Browser: Firefox 133 - OS: Fedora 41 - Terminal: Gnome Terminal 3.54.0 - Shell: ZSHELL - Copy & paste steps: Select all via mouse > copy > paste into nano editor via mouse > save --- ### Result ``` > xxd code.txt 00000000: 2f2a 2a0a 202a 2047 6574 2061 2070 6174 /**. * Get a pat 00000010: 6820 746f 2061 2074 6865 6d65 2072 6573 h to a theme res 00000020: 6f75 7263 652e 0a20 2a2f 0a66 756e 6374 ource.. */.funct 00000030: 696f 6e20 7468 656d 655f 7061 7468 2873 ion theme_path(s 00000040: 7472 696e 6720 2470 6174 6820 3d20 2727 tring $path = '' 00000050: 293a 203f 7374 7269 6e67 0a7b 0a20 2020 ): ?string.{. 00000060: 2024 7468 656d 6520 3d20 636f 6e66 6967 $theme = config 00000070: 2827 7669 6577 2e74 6865 6d65 2729 3b0a ('view.theme');. 00000080: 0a20 2020 2069 6620 2821 2474 6865 6d65 . if (!$theme 00000090: 2920 7b0a 2020 2020 2020 2020 7265 7475 ) {. retu 000000a0: 726e 206e 756c 6c3b 0a20 2020 207d 0a0a rn null;. }.. 000000b0: 2020 2020 7265 7475 726e 2062 6173 655f return base_ 000000c0: 7061 7468 2827 7468 656d 6573 2f27 202e path('themes/' . 000000d0: 2024 7468 656d 6520 2e20 2824 7061 7468 $theme . ($path 000000e0: 203f 2044 4952 4543 544f 5259 5f53 4550 ? DIRECTORY_SEP 000000f0: 4152 4154 4f52 202e 2024 7061 7468 203a ARATOR . $path : 00000100: 2024 7061 7468 2929 3b0a 7d0a $path));.}. ``` - Standard newlines retained, code presents as expected. - Spaces are retained as standard spaces.
Author
Owner

@crashr commented on GitHub (Jan 22, 2025):

@ssddanbrown I think I found out what the problem is. The nbsp for example are caused by this code fom TinyMCE:

Image

As you see it happens when you first paste code in the normal editor, then mark the code and then open the code editor such that the selected code gets automatically pasted into the code editor. It replaces spaces at the beginning and at the end of each line and also affects line break tags. I assume this behavior makes sence if the selected text is not code from an editor but in this case I would consider it as a bug in BookStack. A solution would be to just remove part of the code since it isn't needed in this usecase anyway. Or consider providing a patch which can be applied after installation.

@crashr commented on GitHub (Jan 22, 2025): @ssddanbrown I think I found out what the problem is. The nbsp for example are caused by this code fom TinyMCE: ![Image](https://github.com/user-attachments/assets/0215e08c-dcd2-4fd9-b2bc-be2d3103f898) As you see it happens when you first paste code in the normal editor, then mark the code and then open the code editor such that the selected code gets automatically pasted into the code editor. It replaces spaces at the beginning and at the end of each line and also affects line break tags. I assume this behavior makes sence if the selected text is not code from an editor but in this case I would consider it as a bug in BookStack. A solution would be to just remove part of the code since it isn't needed in this usecase anyway. Or consider providing a patch which can be applied after installation.
Author
Owner

@ssddanbrown commented on GitHub (Jan 22, 2025):

Thanks @crashr, can confirm those steps reproduce the issue, at least for non-breaking spaces being introduced to code block text. Not sure it totally confirms the OP scenario, but might be related (other encoded whitespace).

I'm not putting any further work into the TinyMCE editor (unless essential) since it's on it's way, but I've re-labelled this as a WYSIWYG bug so this can be checked if an issue in the new editor, and to think of options to address existing occurrences.

I'm thinking we could replace non-breaking-spaces with spaces in the code editor, but not sure if there's a legitimate case for non-breaking-spaces in code blocks.

@ssddanbrown commented on GitHub (Jan 22, 2025): Thanks @crashr, can confirm those steps reproduce the issue, at least for non-breaking spaces being introduced to code block text. Not sure it totally confirms the OP scenario, but might be related (other encoded whitespace). I'm not putting any further work into the TinyMCE editor (unless essential) since it's on it's way, but I've re-labelled this as a WYSIWYG bug so this can be checked if an issue in the new editor, and to think of options to address existing occurrences. I'm thinking we could replace non-breaking-spaces with spaces in the code editor, but not sure if there's a legitimate case for non-breaking-spaces in code blocks.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/BookStack#3895