# Users pasting unformatted code results in unwanted styles

**URL:** https://meta.discourse.org/t/users-pasting-unformatted-code-results-in-unwanted-styles/29120
**Category:** Support
**Created:** [22 Maggio 2015, 11:56am UTC](https://meta.discourse.org/t/users-pasting-unformatted-code-results-in-unwanted-styles/29120 "2015-05-22T11:56:27Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![gingerling](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gingerling/32/42360_2.png) [@gingerling](https://meta.discourse.org/u/gingerling)
#### Post date: [22 Maggio 2015, 11:56am UTC](https://meta.discourse.org/t/users-pasting-unformatted-code-results-in-unwanted-styles/29120/1 "2015-05-22T11:56:27Z")

</div>

Hi

we have a lot of this [Bounce not Working - ℹ️ Support - phpList Discuss](https://discuss.phplist.org/t/bounce-not-working/239) where people post code in from some config file and all the commented out lines beginning with a hash become #bold - I guess due to the markup

is there some way to change this? It’s driving me nuts 😕

---

<div class="post-metadata">

### Author: ![calmh](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/calmh/32/115466_2.png) [@calmh](https://meta.discourse.org/u/calmh)
#### Post date: [22 Maggio 2015, 12:17pm UTC](https://meta.discourse.org/t/users-pasting-unformatted-code-results-in-unwanted-styles/29120/2 "2015-05-22T12:17:02Z")

</div>

Use an actual code tag?

```plaintext
Here's some code
# with comments

```

---

<div class="post-metadata">

### Author: ![gingerling](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gingerling/32/42360_2.png) [@gingerling](https://meta.discourse.org/u/gingerling)
#### Post date: [22 Maggio 2015, 12:35pm UTC](https://meta.discourse.org/t/users-pasting-unformatted-code-results-in-unwanted-styles/29120/3 "2015-05-22T12:35:55Z")

</div>

ok, so I will personally do that, but other people are not doing that so I have to keep going in and editing.

I guess that leaves me with two possible actions:

1. if discourse could differentiate between code and normal text it could suggest they use the code tag OR
2. if we can change the markup for bold to something that is not a hash

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [22 Maggio 2015, 9:32pm UTC](https://meta.discourse.org/t/users-pasting-unformatted-code-results-in-unwanted-styles/29120/4 "2015-05-22T21:32:00Z")

</div>

Users will have bad results if they paste in raw code without trying to format it as code. There is no way to work around this, no magical AI that can “detect” code and format it for the user.

Two ways:

1. Use the ````` triple backticks at the top and bottom to mark a code block

2. Select the code then press ctrl+k (or use the ![](https://global.discourse-cdn.com/meta/original/3X/4/4/446a393464c889a0ff93160582024f6720610441.png) toolbar button) to indent it all by four spaces

---

<div class="post-metadata">

### Author: ![tDo](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tdo/32/115520_2.png) [@tDo](https://meta.discourse.org/u/tDo)
#### Post date: [23 Maggio 2015, 9:20am UTC](https://meta.discourse.org/t/users-pasting-unformatted-code-results-in-unwanted-styles/29120/5 "2015-05-23T09:20:09Z")

</div>

While it’s true that it would be very hard to impossible to actually detect something like code in pasted content and educating a user about that kind of content-handling would probably suffice it could still be possible to add some kind of “weird content paste”-detection. Let my try to explain:

Something like code-blocks is typically pasted into a response. Since discourse is automatically doing a markdown transformation for the live-preview it should be possible to verify what kind of content has been pasted. If it for example consists of mostly headlines (e.g. 10 headlines and like only 2 paragraphs) it could be possible that a user pasted text which should actually be formatted in a different way (in this case as a code-block). The editor could then show a small notification telling the user that some more work should be done, before actually saving the message.

Still this kind of detection would be very hard to implement and might even use lots of computing resources on the client, which especially for mobile devices might just be too much.

---

<div class="post-metadata">

### Author: ![gingerling](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gingerling/32/42360_2.png) [@gingerling](https://meta.discourse.org/u/gingerling)
#### Post date: [23 Maggio 2015, 10:17am UTC](https://meta.discourse.org/t/users-pasting-unformatted-code-results-in-unwanted-styles/29120/6 "2015-05-23T10:17:13Z")

</div>

I will just have to see how this evolves. So far everyone pasting code is not using code tag and they don’t take any action despite the massive bold headlines showing up in the preview window. I might just have to hack in some kind or note some where in the post window, or make a popup notice or something. Am surprised if it is just the phpList community that have an issue with this however. Guess most people are pasting from the config.php though, which has a lot of commented out sections by nature.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [23 Maggio 2015, 10:33am UTC](https://meta.discourse.org/t/users-pasting-unformatted-code-results-in-unwanted-styles/29120/7 "2015-05-23T10:33:21Z")

</div>

For your particular site possibly an interesting approach would be to run all “text chunks” pasted in via 4-5 regexes. For PHPs case you could quite easily come up with a few regexes that would be very accurate at predicting that a chunk pasted in is PHP. Then the editor could auto wrap. This would not have any performance impact, but hooking it in would be a bit tricky.

A more general approach as proposed by @tDo may be a bit harder to come up with. Clearly if we ever shipped something like this it would have to be opt in.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [23 Maggio 2015, 10:34am UTC](https://meta.discourse.org/t/users-pasting-unformatted-code-results-in-unwanted-styles/29120/8 "2015-05-23T10:34:56Z")

</div>

Another very interesting thing @gingerling that you can try is giving users a “silver” badge first time they add a code block. It can create some incentive to learn how to use the system. (this can be easily automated with the current badge system)

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [23 Maggio 2015, 10:40am UTC](https://meta.discourse.org/t/users-pasting-unformatted-code-results-in-unwanted-styles/29120/9 "2015-05-23T10:40:04Z")

</div>

I know we have discussed this before with @cpradio … we did eventually implement something like this on Stack Overflow but it is kind of tricky. And that was a site dedicated to code in a way that Discourse isn’t, necessarily.

---

<div class="post-metadata">

### Author: ![gingerling](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gingerling/32/42360_2.png) [@gingerling](https://meta.discourse.org/u/gingerling)
#### Post date: [25 Maggio 2015, 9:21am UTC](https://meta.discourse.org/t/users-pasting-unformatted-code-results-in-unwanted-styles/29120/10 "2015-05-25T09:21:27Z")

</div>

> [@sam](#):
>
> For your particular site possibly an interesting approach would be to run all “text chunks” pasted in via 4-5 regexes. For PHPs case you could quite easily come up with a few regexes that would be very accurate at predicting that a chunk pasted in is PHP. Then the editor could auto wrap. This would not have any performance impact, but hooking it in would be a bit tricky.

interesting - i will discuss this with our developers.

I think for now, I can edit, select, control K, and this will do.

Part of our issue, which is why:

> [@sam](#):
>
> giving users a “silver” badge first time they add a code block. It can create some incentive to learn how to use the system. (this can be easily automated with the current badge system)

might not work, is that we have many people who only ever ask one question and very few returning visitors.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [28 Giugno 2021, 10:55pm UTC](https://meta.discourse.org/t/users-pasting-unformatted-code-results-in-unwanted-styles/29120/13 "2021-06-28T22:55:38Z")

</div>

Buone notizie! Abbiamo ora un componente ufficiale per questo

> [@Unformatted Code Detector](https://meta.discourse.org/t/unformatted-code-detector-theme-component/112773):
>
> discourse2Summary Unformatted Code Detector detects unformatted code and gives a warning before posting.eyeglassesPreview [Preview on Discourse Theme Creator](https://discourse.theme-creator.io/theme/Discourse/unformatted-code-detector)hammer_and_wrenchRepository Link [https://github.com/discourse/unformatted-code-detector](https://github.com/discourse/unformatted-code-detector)open_bookNew to Discourse Themes? [Beginner’s guide to using Discourse Themes](https://meta.discourse.org/t/beginners-guide-to-using-discourse-themes/91966) Install this theme component Features Users posting unformatted code will see a warning message instructing them how to format it correctly…
