# Inline code blocks in code fence mode

**URL:** https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833
**Category:** Bug
**Created:** [August 18, 2016, 10:04pm UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833 "2016-08-18T22:04:27Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![dfabulich](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dfabulich/32/108716_2.png) [@dfabulich](https://meta.discourse.org/u/dfabulich)
#### Post date: [August 18, 2016, 10:04pm UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/1 "2016-08-18T22:04:27Z")

</div>

In the default mode of the code `</>` button, when you select some text in the middle of a line and click the code button, it wraps that text in single backticks, like ``this``.

We learned about the “code formatting style = code-fences” mode from today’s blog post and turned it on right away, but I was surprised to find that selecting a word inline and clicking the code button no longer wraps it in single backticks; it wraps it in triple backticks and adds newlines before and after the selected word.

Users can delete the newlines to get the intended result (which is to say, you can write ````triple backticks```` on a single line and it DTRT) but I don’t think it’s correct to add newlines to selected text.

This is a bug, right? (Or is it by design for some reason?)

---

<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: [August 18, 2016, 11:45pm UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/2 "2016-08-18T23:45:02Z")

</div>

Possibly a bug, particularly if the behavior changes with that site setting. @tgxworld can you have a look?

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [August 23, 2016, 1:55am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/3 "2016-08-23T01:55:06Z")

</div>

Fixed in  
[https://github.com/discourse/discourse/commit/f32d8e28824e7e839df01a9621bb1202869dd10b](https://github.com/discourse/discourse/commit/f32d8e28824e7e839df01a9621bb1202869dd10b)

We were missing “code fences” test so I added them in as well

---

<div class="post-metadata">

### Author: ![dfabulich](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dfabulich/32/108716_2.png) [@dfabulich](https://meta.discourse.org/u/dfabulich)
#### Post date: [August 23, 2016, 2:14am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/6 "2016-08-23T02:14:48Z")

</div>

The message of that commit doesn’t sound right to me. On our (hosted) site, using `code formatting style=code-fences` would _always_ add newlines before and after the text, when the behavior for `4-spaces-indent` would indent multiline selections and add single backticks around single-line selections.

Is there somewhere I can try the fix out? (I guess I can try it out on our hosted site in a couple of weeks or something?)

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [August 23, 2016, 2:32am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/7 "2016-08-23T02:32:06Z")

</div>

😅 I was trying to make the commit message concise so some of the more specific details are lost.

You can try it on [try.discourse.org](http://try.discourse.org). 🙂

---

<div class="post-metadata">

### Author: ![dfabulich](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dfabulich/32/108716_2.png) [@dfabulich](https://meta.discourse.org/u/dfabulich)
#### Post date: [August 23, 2016, 2:48am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/8 "2016-08-23T02:48:32Z")

</div>

I can’t try it there because it uses the default 4-space indent setting, right? (Would you mind changing it to use code fences so I can give it a whirl?)

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [August 23, 2016, 2:51am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/9 "2016-08-23T02:51:45Z")

</div>

Sure I just switched [try.discourse.org](http://try.discourse.org) to use code fences.

---

<div class="post-metadata">

### Author: ![dfabulich](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dfabulich/32/108716_2.png) [@dfabulich](https://meta.discourse.org/u/dfabulich)
#### Post date: [August 23, 2016, 3:18am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/10 "2016-08-23T03:18:44Z")

</div>

If your fix did anything, I don’t see it!

Let me clarify how I think the code `</>` button works (should work) in 4SI (4-space indent) mode and in CF (code fences) mode.

In 4SI mode, the `</>` button has two very different behaviors, one where you select just part of a line (Example 1), and another different behavior where you select multiple lines (Example 2).

**Example 1 (single-line)**  
Input: `one two three` select the word “two”  
4SI Output: `one `two` three`

**Example 2 (multi-line)**  
Input: select all three lines:

```plaintext
multi
line
text

```

4SI Output:

```plaintext
    multi
    line
    text

```

4SI works as expected in both examples. CF mode works as expected in Example 2 (multi-line); CF mode does not work correctly in Example 1 (single-line) on [try.discourse.org](http://try.discourse.org) right now.

Input: `one two three` select the word “two”  
4SI Output: `one `two` three`  
CF Expected Output: `one `two` three` (same as 4SI)  
CF Actual Output:

````
one
```
two
```
three

````

The only difference between 4SI and CF is (should be) how they behave in Example 2 (multi-line). They should behave exactly the same in Example 1 (single-line).

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [August 23, 2016, 3:31am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/11 "2016-08-23T03:31:43Z")

</div>

> [@dfabulich](#):
>
> CF Expected Output: one `two` three (same as 4SI)

Hmm I’m not following here. Why should it be the same as the 4 spaces ident format? According to the example on [common mark](http://spec.commonmark.org/0.25/#example-87), the backticks are not supposed to be on the same line.

```plaintext

```

def method; puts “yay”; end

```plaintext

```

```plaintext
def method; puts "yay"; end

```

VS

````plaintext
 ```def method; puts "yay"; end```

````

`def method; puts "yay"; end`

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [August 23, 2016, 3:35am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/12 "2016-08-23T03:35:02Z")

</div>

Because `inline code style` is what’s desired in that case. It’s just when selecting the whole line, or multiple lines, that the behavior should change based on the setting.

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [August 23, 2016, 3:38am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/13 "2016-08-23T03:38:50Z")

</div>

Ah ic. Let me fix it now

---

<div class="post-metadata">

### Author: ![dfabulich](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dfabulich/32/108716_2.png) [@dfabulich](https://meta.discourse.org/u/dfabulich)
#### Post date: [August 23, 2016, 3:42am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/14 "2016-08-23T03:42:41Z")

</div>

Just to be crystal clear, let me lay out the four outputs side by side. I’ll put Example 2 first because I think it’s the least controversial.

**Example 2 (multi-line)**  
Input: select all three lines:

```plaintext
multi
line
text

```

4SI Output: (working as designed)

```plaintext
    multi
    line
    text

```

CF Output: (working as designed)

````
```
multi
line
text
```

````

**Example 1 (single-line)**

Input: `one two three` select the word “two”

4SI Output:  
`one `two` three`  
CF Expected Output:  
`one `two` three` (same as 4SI)

You quoted an example of a alternative output for inline code stile with code fences:

CF Alternative Output: `one ```two``` three`

“CF Expected Output” and “CF Alternative Output” render identically:  
one `two` three  
one `two` three

I could accept the “CF Alternative Output”, but that’s not what’s happening right now. Right now, instead of applying any kind of inline styling, it’s adding newlines.

Definitely Incorrect CF Output: (the current behavior for partial line selections)

````
one
```
two
```
three

````

That renders like this, which is not what’s desired:

one

```plaintext
two

```

three

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [August 23, 2016, 3:54am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/15 "2016-08-23T03:54:11Z")

</div>

> [@riking](#):
>
> Because inline code style is what’s desired in that case. It’s just when selecting the whole line, or multiple lines, that the behavior should change based on the setting.

Actually I don’t think this is true. Looking back at the code now, we’ve always added new lines after/before the opening/closing backticks.

---

<div class="post-metadata">

### Author: ![dfabulich](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dfabulich/32/108716_2.png) [@dfabulich](https://meta.discourse.org/u/dfabulich)
#### Post date: [August 23, 2016, 4:11am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/16 "2016-08-23T04:11:44Z")

</div>

Are you saying that this issue was “always” present in 4SI mode? Specifically, are you saying that what I called “Example 1 4SI Output” is not correct? You can try it yourself right here on meta.

one `two` three

Or are you saying that the issue was “always” present in CF mode? CF mode is a relatively new mode, off by default; we shouldn’t care very much about how CF mode “always” used to be.

As it stands today, if forum admins enable CF mode, there’s no way to click the code button tot get inline code styling. We don’t want that, right?

---

<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: [August 23, 2016, 4:15am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/17 "2016-08-23T04:15:25Z")

</div>

Why is this confusing?

- If you select text that is less than a single line use single ticks for inline code. Regardless of the setting, single ticks is correct for inline code with no carriage returns or line feeds in it.

- if you select multiple lines of text, use either triple back ticks or indent method depending on site setting.

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [August 23, 2016, 4:30am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/18 "2016-08-23T04:30:38Z")

</div>

> [@dfabulich](#):
>
> Or are you saying that the issue was “always” present in CF mode? CF mode is a relatively new mode, off by default; we shouldn’t care very much about how CF mode “always” used to be.

Not at all. I was just confused because the bug was “single line code selection generated multiline code fences” but the existing code was intentionally doing so. All cleared up now though 😄

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [August 23, 2016, 7:02am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/19 "2016-08-23T07:02:16Z")

</div>

@dfabulich Fixed 🙂

[https://github.com/discourse/discourse/commit/90571f036465c95591689b45eca34de1b18b4f98](https://github.com/discourse/discourse/commit/90571f036465c95591689b45eca34de1b18b4f98)

---

<div class="post-metadata">

### Author: ![dfabulich](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dfabulich/32/108716_2.png) [@dfabulich](https://meta.discourse.org/u/dfabulich)
#### Post date: [August 23, 2016, 7:14am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/20 "2016-08-23T07:14:01Z")

</div>

Cool! Can you make the fix available on [try.discourse.org](http://try.discourse.org) (and re-enable CF if necessary) so I can verify it?

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [August 23, 2016, 7:21am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/21 "2016-08-23T07:21:23Z")

</div>

The changes have been deployed to [try.discourse.org](http://try.discourse.org). 🙂

---

<div class="post-metadata">

### Author: ![dfabulich](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dfabulich/32/108716_2.png) [@dfabulich](https://meta.discourse.org/u/dfabulich)
#### Post date: [August 23, 2016, 7:24am UTC](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833/22 "2016-08-23T07:24:43Z")

</div>

Looks great! The bug appears to be fixed. Thanks!

[Next page](https://meta.discourse.org/t/inline-code-blocks-in-code-fence-mode/48833.md?page=2)
