strikethrough code in markdown on github

末鹿安然 提交于 2019-12-03 05:53:51

This would only be possible with raw HTML, which GitHub doesn't allow. But you may be able to use a diff instead.

Code blocks are for "pre-formatted" text only. The only formatting you can get in a code block is the formatting that can be represented in plain text (indentation, capitalization, etc). There is no mechanism to mark up the content of a code block (as bold, italic, stricken, underlined, etc). This was an intentional design decision. Otherwise, how would you be able to show Markdown text in a code block? If you want formatted text, then you need to use something other than a code block.

As the rules state:

HTML is a publishing format; Markdown is a writing format. Thus, Markdown’s formatting syntax only addresses issues that can be conveyed in plain text.

For any markup that is not covered by Markdown’s syntax, you simply use HTML itself.

Therefore you would need to format your own custom HTML code block with the various bits marked up properly:

<pre><code><del>some stricken code</del>
<del>A second line of stricken code</del>
</pre></code>

However, for security reasons, GitHub will strip out any such raw HTML in your Markdown. So while this works where you have full control of the entire stack, on a hosted service it is most likely not possible.

However, I'm assuming you want to show some changes made to a block of code. As it turns out, a specific format already exists for that. Namely a diff. Just use a fenced code block with diff as the language and GitHub will format it correctly:

```diff
  Unchanged Line
- Removed Line
+ Added Line
```

You can see how GitHub displays the above code block live (you can also see that in raw), but I've included a screenshot below for convenience.

I realize that the formatting does not use strike-through, but is does use a commonly used and understood format. For more complex blocks, you should probably use thediff utility program to generate the diff for you.

Jason Deppen

Expanding on Waylan's answer:

This may be obvious to others, but it caught me. When you have indented lines, be sure + or - is the first character on the line or it won't highlight.

diff

<div>
  Unchanged Line
  <ul>
    - <li>This won't work</li>
-    <li>This will</li>
+    <li>1st character, then indent</li>
  </ul>
</div>

On the subject of marking up the content of a code block, to tack an italicized string on to the end of a line of "code", try something like:

<code>id\_pn\_aside\_subscriber\_form\__form\_id_</code>

(You can see this in action at: https://github.com/devonostendorf/post-notif#how-do-you-use-the-stylesheet_filename-attribute-with-the-shortcode)

I had a hard time finding an example that matched this precise use case, so I hope this proves useful for anyone else trying to accomplish a similar effect.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!