Github markdown,表格单元格中代码块的语法高亮

时间:2014-02-19 10:45:54

标签: github html-table markdown syntax-highlighting github-flavored-markdown

Markdown具有管道表语法,但在某些情况下还不够。

| table | syntax | without multiline cell content |

因此,我们可以使用HTML表格标签。

<table>
<tr>
<td>
   ```csharp
   const int x = 3;
   const string y = "foo";
   readonly Object obj = getObject();
   ```
</td>
<td>
  ```nemerle
  def x : int = 3;
  def y : string = "foo";
  def obj : Object = getObject();
  ```
</td>
<td>
  Variables defined with <code>def</code> cannot be changed once defined. This is similar to <code>readonly</code> or <code>const</code> in C# or <code>final</code> in Java. Most variables in Nemerle aren't explicitly typed like this.
</td>
</tr>

但前段时间语法突出显示已被破坏,此wiki page现在看起来很难看。关于如何解决这个问题的任何想法?

6 个答案:

答案 0 :(得分:30)

你可以在表格中使用<pre>,正如teh_senaus所说。但是如果你这样做,语法高亮不会起作用......或者它会不会?

通过随机实验,我发现GitHub允许用<pre lang="csharp">指定它。这与```csharp将语法高亮设置为C#的效果相同。

在GitHub的帮助中心的任何地方都没有真正记录,也没有在linguist的文档中记录。但它可以工作,甚至在桌子内部。

因此,对于您的示例表,新代码如下:

<table>
<tr>
<td>
   <pre lang="csharp">
   const int x = 3;
   const string y = "foo";
   readonly Object obj = getObject();
   </pre>
</td>
<td>
  <pre lang="nemerle">
  def x : int = 3;
  def y : string = "foo";
  def obj : Object = getObject();
  </pre>
</td>
<td>
  Variables defined with <code>def</code> cannot be changed once defined. This is similar to <code>readonly</code> or <code>const</code> in C# or <code>final</code> in Java. Most variables in Nemerle aren't explicitly typed like this.
</td>
</tr>

答案 1 :(得分:3)

<td>和代码块之间添加空白行。

这是固定的降价幅度:

<table>
<tr>
<td>

  ```csharp
  const int x = 3;
  const string y = "foo";
  readonly Object obj = getObject();
  ```
</td>
<td>

  ```nemerle
  def x : int = 3;
  def y : string = "foo";
  def obj : Object = getObject();
  ```
</td>
<td>
  Variables defined with <code>def</code> cannot be changed once defined. This is similar to <code>readonly</code> or <code>const</code> in C# or <code>final</code> in Java. Most variables in Nemerle aren't explicitly typed like this.
</td>
</tr>
</table>

和结果:

enter image description here

答案 2 :(得分:2)

您可以使用<pre>。语法高亮不起作用,但至少它将被正确格式化。

<td><pre>
   const int x = 3;
   const string y = "foo";
   readonly Object obj = getObject();
</pre></td>

答案 3 :(得分:2)

另一种方法是使用多个`<br>,但语法突出显示无法正常工作。

|1|2|3
-|-|-
`const int x = 3;`<br>`   const string y = "foo";`<br>`readonly Object obj =getObject();`|`def x : int = 3;`<br>`def y : string = "foo";`<br>`def obj : Object = getObject(); `|Variables defined with `def` cannot be changed once defined. This is similar to `readonly` or `const` in C# or `final` in Java. Most variables in Nemerle aren't explicitly typed like this.explicitly typed like this.

答案 4 :(得分:0)

您也可以:

A | B | C
-- | -- | --
x | y | Some code : <pre lang=php>function sayHello($someArg)</pre>
1 | 2 | 3

答案 5 :(得分:0)

技巧是在代码中使用 backticks ,同时使用<pre>标签将其包装起来,如下所示:

<pre lang=html>`<input readonly>`</pre>

这是我自己的用例的屏幕截图:

enter image description here

相关问题