Guide to Posting Code - Redux

Discourse uses Markdown for text formatting. There is a reference sheet and quick tutorial here: Markdown Reference

You can produce in-line code formatting by wrapping your code in backticks (`). For example, `Get-ChildItem` produces Get-ChildItem.

For larger code blocks, place a line containing three backticks above and below your code. For example

```
# 256-Color Foreground & Background Charts
echo β€œ`n$esc[1;4m256-Color Foreground & Background Charts$esc[0m”
foreach ($fgbg in 38,48) { # foreground/background switch
foreach ($color in 0…255) { # color range
#Display the colors
$field = β€œ$color”.PadLeft(4) # pad the chart boxes with spaces
Write-Host -NoNewLine β€œ$esc[fgbg;5;{color}m$field $esc[0m”
#Display 6 colors per line
if ( (($color+1)%6) -eq 4 ) { echo β€œ`r” }
}
echo `n
}
```

produces

# 256-Color Foreground & Background Charts
echo "`n$esc[1;4m256-Color Foreground & Background Charts$esc[0m"
foreach ($fgbg in 38,48) {  # foreground/background switch
  foreach ($color in 0..255) {  # color range
    #Display the colors
    $field = "$color".PadLeft(4)  # pad the chart boxes with spaces
    Write-Host -NoNewLine "$esc[$fgbg;5;${color}m$field $esc[0m"
    #Display 6 colors per line
    if ( (($color+1)%6) -eq 4 ) { echo "`r" }
  }
  echo `n
}

The preview feature (shown on the right when typing a post) is particularly helpful for ensuring that your code displays the way you want it to. Also, note that backticks placed inside of the code block formatting are reproduced literally, rather than being interpreted as in-line code tags. If you need to show backticks in your code, I recommend using this formatting option.

Compared with our previous solution this is fairly straightforward, though it does have some limitations. For instance, there does not seem to be any way to accomplish line numbering. If anyone is aware of a good Discourse plugin for this, please share!

3 Likes