[Question]
  [
## Background
As far as we can tell from trying everything (besides cheating with ZWSPs), you can't type
``\`
in [Stack Exchange chat](https://chat.stackexchange.com/). However, you can type
`\``
by doing
``\\```.
So, if you want to tell someone in chat how to type backslash-backtick, you'd need to type out
``\`\\`\```
to format
``\\```
into a codeblock.
## Challenge
Your challenge will be to take a string of backticks and backslashes and output a string of backticks and backslashes that will format into that (as a code block) when you type it into SE chat. You can test this in [the chat sandbox](https://chat.stackexchange.com/transcript/1).
One important thing to note is that a backslash prevents the next backtick from starting or ending a code block, but a backslash will not actually make the next backslash *not* escape - basically, three backslashes and a backtick will become two literal backslashes and a literal backtick, the latter of which will not function as a code block delimiter anymore.
Thus, one way to solve this challenge is to take each block of backslashes (including the zero-length ones between consecutive backticks) and add a backslash to each, then surround the string in backticks.
You may assume it is possible; that is, the input will not end with a backslash. You also do not need to handle empty input.
## Input / Output
Input and output will be taken and provided as a string of backticks and backslashes. For this challenge, you must use these characters specifically. You can input or output as a flat list of characters instead, and input and output do not have to use the same format.
## Test Cases
These test cases follow my example solution. I don't actually know if another solution exists, but if you can find an alternative that works in SE chat and isn't just the aforementioned approach, I may offer a bounty.
```
\`                => `\\``
`\\`\`\\``        => `\`\\\`\\`\\\`\``
`\`\\\`\\`\\\`\`` => `\`\\`\\\\`\\\`\\\\`\\`\``
``````            => `\`\`\`\`\`\``
\\\\\\\\\`        => `\\\\\\\\\\``
`                 => `\``
`\`\`\`\`\`\`\`   => `\`\\`\\`\\`\\`\\`\\`\\``
```
[Reference Implementation](https://tio.run/##ZY0xDsMgDEVn@xSIKWmWVt0q9RYdGSARESiIIELU5vTUbhSpVQdj8/3/c9qKm@O11qfzwYpHXu0NoeSNXgg@WnEXPqa1NC2CfQ02Fd702ZoJoTfDtASzOLuQ74wwr4W8NEuJMM5ZDM5kAghGcdCPu3Qni5asHJmOFKWkOIlvareLHbvZ/HcRbFg@5J8VwS4IKftYGkpS/rjCoLZWrbRSXHvTGpVGzSp/3g) in Python; you can input as many lines as you want and it will print a valid answer to each using my approach.
[Standard Loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are disallowed as usual.
This is a [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") challenge, so the shortest code in bytes per language wins in its language category. Happy golfing!
  
[Answer]
# [Pip](https://github.com/dloscutoff/pip), 6 bytes
```
RP``.q
```
[Try it online!](https://tio.run/##K8gs@P8/KCAhQa/w//@YBAA "Pip – Try It Online")
### Explanation
Um... well, this is interesting.
It just so happens that Pip's Pattern type (used for regex) is delimited with backticks. And that backticks can be escaped within a Pattern using backslashes.
... And furthermore, that the code for generating the Pip [repr](https://docs.python.org/3/library/functions.html#repr) of a Pattern is apparently [incorrect](https://tio.run/##K8gs@P8/ISYmIeH/fwA), because it should escape the backslashes too, but in fact it only escapes the backticks. Which is exactly the behavior this question asks for.
"It's not a bug, it's a feature"? ¯\\_(ツ)\_/¯
```
     q  Read a line of stdin
  ``.   Cast it to a Pattern by concatenating an empty Pattern to it
RP      Get the repr
```
---
Okay, I've looked at this again and I think it's **not actually a bug**. The reason why I thought it was a bug was because some of the inputs are not possible to enter as Pattern literals. Since they are "impossible" Patterns, it makes sense that their reprs are not correctly formed representations of Pattern literals.
```
Input  Repr    Comment
`      `\``    Properly formed: backtick is escaped with a backslash
\\`    `\\\``  Properly formed: backslash is also escaped with a backslash
\`     `\\``   Improperly formed: first backtick is not escaped
```
You can't enter a regex for `\`` in Pip because `\`` is not a properly formed regex. (If you want a regex that matches the string `\``, you should use `\\``, which is entered as ``\\\```. Clear as mud? Great.)
In summary, inputs with only even-length runs of backslashes output well-formed reprs, but if there is an odd run of backslashes, all bets are off.
[Answer]
# [Python 3](https://docs.python.org/3/), 35 bytes
```
lambda s:'`%s`'%s.replace('`','\`')
```
[Try it online!](https://tio.run/##bU7RCoMwDHz3K/IibUGEsTeh@5LA4raKha4Wq4x9fVeLbuq8hCbcHc2599B29hwaicHUz9ujBl8xyj2x3Je9cqa@K86IFQyJifBqtVFwqjIAXcC1gA4kaOvGgYvSO6PjjJrrtR14w7UAKaETAQl2kBcgRKJsejGtGykyiU0j2XbM1zYRizg7Jn/C38VVUYYLtqd/dPwGDoPPgVa1yn3Q9AE "Python 3 – Try It Online")
-1 thanks to dingledooper.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 11 bytes
```
ṣ”`j⁾\`⁾``j
```
[Try it online!](https://tio.run/##y0rNyan8///hzsWPGuYmZD1q3BeTACQSErL@P9yx6eGORYfbuR7u3nK4/VHTmsj//2MSFNCArZ1CQkxMQgIXiIwBM1GkgCJgUTAFVoYmAlcGEoBJQlWA1IMBho1IMIErBgZQrUYIA41RwOpwqIOQIJK7saAEAA "Jelly – Try It Online")
Jelly version of [Lynn's approach](https://codegolf.stackexchange.com/a/230316/66833), found independently
## How it works
```
ṣ”`j⁾\`⁾``j - Main link. Takes a string S on the left
ṣ”`         - Split S on backtick
   j⁾\`     - Join with "\`", essentially replacing backticks with "\`"
       ⁾``j - Surround with backticks
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 32 bytes
```
n=>'`'+n.replace(/`/g,'\\`')+'`'
```
[Try it online!](https://tio.run/##PYtBDoAgDASfUwgKL8CXcChBIBrSEjB@HwkH9zBzmOztX99Du@qzE59xJDvIHoCgSLdYiw9RGDR5A@cQpJplBKbOJerCWSQBOItbwx9LOA9yfA "JavaScript (Node.js) – Try It Online")
Self-explanatory trivial solution.
[Answer]
# [Trilangle](https://github.com/bbrk24/Trilangle), 52 bytes
```
"`oo,.<|...,>~i\"<..._".#.,oo`"<>..+...._#..,@.....^
```
Try it on [the online interpreter](https://bbrk24.github.io/Trilangle/#%22%60oo%2C.%3C%7C...%2C%3E%7Ei%5C%22%3C..._%22.%23.%2Coo%60%22%3C%3E..%2B...._%23..%2C%40.....%5E)!
I'm not a huge fan of how un-dense this is (`.` is NOP) but I couldn't make it any smaller. Unfolds to this:
```
         "
        ` o
       o , .
      < | . .
     . , > ~ i
    \ " < . . .
   _ " . # . , o
  o ` " < > . . +
 . . . . _ # . . ,
@ . . . . . ^ . . .
```
[![](https://i.stack.imgur.com/5CVhI.png)](https://i.stack.imgur.com/5CVhI.png)
On the red path, it prints the opening ```, and gets input. If there's no input left, it follows the green path, which prints the closing ``` and exits. Otherwise, it follows the blue path, which prints `\` and then compares the inputted character against `_` (0x5f). If it's less, it takes the yellow path to close the loop. Otherwise, it takes the magenta path to print ```, waste a lot of time/space, and *then* close the loop.
As I said above, there's so many NOPs I can't help but think I can contract this, but I have yet to find a smaller way to do this.
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 12 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
Look, Ma, no escape characters!
```
"`{r'`_i'\}`
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=ImB7cidgX2knXH1g&input=ImBcYFxgXGBcYFxgXGBcYCI)
[Answer]
# [Vyxal 2.3](https://github.com/Lyxal/Vyxal/releases/tag/v2.3.0), 7 bytes
```
\`‛\`Vq
```
Up until recently, q (uneval) simply surrounded the string in backticks. Now it escapes backslashes and backticks, which we don't want.
Also up until recently, strings in the input couldn't be delimited by backticks, but now they can so input is misparsed.
The rest is just replacing backticks with a backtick and a backslash.
So no TIO, but feel free to download the interpreter from the above link and try it out.
[Answer]
# [QuadR](https://github.com/abrudz/QuadRS), 11 bytes
```
^|$
`
`
\\`
```
[Try it online!](https://tio.run/##KyxNTCn6/z@uRoUrAQhjYhL@/48BsoCMGBABYgJpMBtMgUTAAKgWCkAaE2JQIAA "QuadR – Try It Online")
Simply replaces (i.e. inserts at) the beginning/end with a backtick,
and any backtick with a backslash and a backtick.
Equivalent to the Dyalog APL function `'^|$' '`'⎕R'`' '\`'`
[Answer]
# [05AB1E (legacy)](https://github.com/Adriandmen/05AB1E/tree/fb4a2ce2bce6660e1a680a74dd61b72c945e6c3b), 9 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
Z„\`.:Z.ø
```
Assumes the input will always contain a backtick.
[Try it online](https://tio.run/##MzBNTDJM/f8/6lHDvJgEPasovcM7/v@PSQAA) or [verify all test cases](https://tio.run/##MzBNTDJM/V9TVmmvpPCobZKCkn3l/6hHDfNiEvSsovQO7/iv8z8mgSshJiYhBkSAmEAazAZTIBEw4IqBAaAIWBUSBAA).
**Explanation:**
```
Z          # Get the maximum character of the (implicit) input-string (the backtick `)
    .:     # Replace all backticks in the input
 „\`       # with "\`"
      Z    # Then get the backtick again with the maximum (without popping the string)
       .ø  # Surround the string with this backtick as leading/trailing character
           # (after which the result is output implicitly)
```
Uses the legacy version of 05AB1E, because the newer 05AB1E version's min/max won't work on characters (it will return the first character, instead of the lowest/highest unicode character).
[Answer]
# [Nibbles](http://golfscript.com/nibbles/index.html), 9 bytes
```
;"`":<<*$.`%_$:"\\"
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m708LzMpKSe1eMGCpaUlaboWm62VEpSsbGy0VPQSVONVrJRiYpQgMlAFC246xyRwJcTEJMSACBATSIPZYAokAgZcMTAAFAGrQoIQswA)
```
;"`":<<*$.`%_$:"\\"
;                   Output b :=
 "`"                 backtick
    :               Join
     <<              remove last character from
       *              join by
        $              b
         .             for each s in
          `%            split
            _            input
             $           by b
              :         join
               "\\"      backslash
                         s
                     b
```
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 31 bytes
```
'`'+$args.replace('`','\`')+'`'
```
[Try it online!](https://tio.run/##K8gvTy0qzkjNyfn/Xz1BXVslsSi9WK8otSAnMTlVAyiiox6ToK6pDWT9B6mIiUmIAREJ6gA "PowerShell – Try It Online")
[Answer]
# [Perl 5](https://www.perl.org/) + `-pl140`, 16 bytes
```
$_=$\.s/`/\\`/gr
```
[Try it online!](https://tio.run/##K0gtyjH9/18l3lYlRq9YP0E/JiZBP73o//@EmISYGBCGUAkJ//ILSjLz84r/6xbkGJoYAAA "Perl 5 – Try It Online")
## Explanation
A relatively standard solution, this uses the `-l140` flag to set `$\` to ``` which is automatically output after every call to `print`. Since we're also using the `-p` flag, `$_` will be automatically `print`ed so we set it to `$\` concatenated with the `/r`esult of `s/`ubsitituing ``` with `\`` in the (implicit) input stored in `$_`.
---
# [Perl 5](https://www.perl.org/) + `-p0140l`, 19 bytes
```
$\.="$_\\`"}{$\.=$/
```
[Try it online!](https://tio.run/##K0gtyjH9/18lRs9WSSU@JiZBqbYaxFHR//8/ISYhJgaEIVRCwr/8gpLM/Lzi/7oFBoYmBjkA "Perl 5 – Try It Online")
## Explanation
Slightly more interesting but longer, this answer uses the `-0140` flag to split input on ``` instead of newlines which means the first part of the code (before the `}{`) will be called for each part of the string (up to, but excluding ```), appending the string itself concatenated with `\\`` to `$\` which is pre-initialised to `$/` (```) via `-l`. Once all the input has been parsed (`}{`), `$/` is appended to `$\` which is implicitly output due to `-p`.
[Answer]
# [Thunno](https://github.com/Thunno/Thunno), \$ 15 \log\_{256}(96) \approx \$ 12.35 bytes
```
'`"\`"Ar'`+'`s+
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72sJKM0Ly9_wYKlpSVpuhbr1ROUYhKUHIvUE7TVE4q1IaJQyQXrE2JQIEQYAA)
Port of Lynn's Python answer
#### Explanation
```
'`"\`"Ar'`+'`s+  # Implicit input
'`               # Push `
  "\`"           # Push \`
      Ar         # Replace ` with \`
        '`+      # Append a backtick
           '`s+  # Prepend a backtick
                 # Implicit output
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), ~~12~~ 7 bytes
```
q\\d\\V
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJBIiwiIiwicVxcXFxkXFxcXFYiLCIiLCJcXGBcbmBcXFxcYFxcYFxcXFxgYFxuYFxcYFxcXFxcXGBcXFxcYFxcXFxcXGBcXGBgXG5gYGBgYGBcblxcXFxcXFxcXFxcXFxcXFxcXGBcbmBcbmBcXGBcXGBcXGBcXGBcXGBcXGBcXGAiXQ==)
*-5 thanks to @lyxal*
#### Explanation
```
q\\d\\V  # Implicit input
q        # Repr (Vyxal strings are
         # delimited by backticks)
 \\d     # Double a backslash to get \\
    \\V  # Replace with one backslash
         # Implicit output
```
Old:
```
\`\\\`+V\`ø.  # Implicit input
\`            # Push the character `
  \\          # Push the character \
    \`+       # Add it to ` to get \`
              # This is one byte shorter than
              # using a string literal, `\\\``,
              # since strings in Vyxal are
              # delimited by backticks
       V      # Replace ` with \`
        \`ø.  # Surround with backticks
              # Implicit output
```
[Answer]
# [Red](http://www.red-lang.org), 42 bytes
```
func[s][rejoin[t:"`"replace/all s t"\`"t]]
```
[Try it online!](https://tio.run/##ZVBbDsIgEPznFBsu4H@NHsJfIKFpl1hDaAN4foQt2IqzJEtmZh/gcU4PnIViZkjm7SYRlPD4Whcn4sA197jZccLLaC0EiFxqHpVKZvU4Tk@IGCIIVugrdLjdQUupNeMlSbofLlIzRzyl3dlRuaA5C9Xk6qESwu/4WvKNbJMN/Q6HUJr9P6M2q8udgqyn5fqjmWJi84uLYOijVPoA "Red – Try It Online")
]