80

I have a table:

| YAY! | TABLE | \^^/ | 1-liner JSON column! |
| ---- | ----- | ---- | -------------------- |
| That |  has  | JSON | `{a: 1, b: 2, c: 3}` |
| Here |  is   | more | `{d: 4, e: 5, f: 6}` |

Is there any way for me to insert in multiline code blocks into a generated table cell?

3 Answers 3

100

Replace ` with <code> tags and use &nbsp; and <br>for indentation.

Similarly you can use <pre> tags instead of ```.

Sign up to request clarification or add additional context in comments.

13 Comments

<br/> also works independently from ` or <code> to force multiline cells. I've used/tested this in combination with pandoc converting my.md to HTML.
My intention wasn't saying you were either but to clarify for others that might not be aware of this fact! ;) Anyway, I've run across Problems when using &nbsp; and pandoc to convert my .md to HTML as pandoc escapes &nbsp; to &amp;nbsp; which in turn gets displayed as  in my browser. Did you ever come across such issues? To what formats are you converting your .md files and which tools are being used?
You'll need to use <pre> instead of <code> for multiline code block inside table on github ;)
Can't make it work guys, GitHub shows this code here on one line in a table cell, <br/> is hidden: <pre><code>{<br/>&nbsp;&nbsp;showToday: false,<br/>&nbsp;&nbsp;showClear: false,<br/>&nbsp;&nbsp;showClose: false<br/>}</code></pre> Any ideas? Thanks!
@tonix Oh I make it work, you don't need <pre><code> just only <pre>. You can find a working example in my repo: github.com/prettier/plugin-pug/blob/main/README.md
|
20

Answer by @Meredith is the perfect answer to this. I'd like to add more details and examples below

You cannot replace ` with <code> if you need to add other HTML tags inside the <code> element in your table cell. Instead you need to use backtick (`) inside the <pre> tag like this:

Example:

Markdown Input HTML Output HTML Preview
<pre>
<p>Test Line</p>
</pre>
<pre><p>Test line</p></pre>

Test Line

Extended Example:

Markdown Input HTML Output HTML Preview
`{a: 1, b: 2, c: 3}` <code>{a: 1, b: 2, c: 3}</code> {a: 1, b: 2, c: 3}
<pre> {JSON: <br>
&emsp; ["Key1":"Value1",<br>
&emsp; "Key2":"Value2"] <br>
}</pre>
<pre> {JSON: <br> &emsp;["Key1":"Value1",<br> &emsp;"Key2":"Value2"]<br> } </pre>
{JSON: 
 ["Key1":"Value1",
 "Key2":"Value2"]
}

3 Comments

From experience: this is unmaintainable. In my situation a html table was the solution.
I agree tyntam. If the OG question was regarding the maintainability of this, then I would not recommend this either. But an example use case where this will be useful is for storing your markdown table in a database (maybe for a blog CMS) - if you use HTML table, you'll use more memory than you would with this approach. Especially, if only few of your table cells have multi-line markdown.
checkout this answer for syntax highlighting code-blocks inside table
1

In docusaurus you should use <br/>

<br> generates the error "Expected a closing tag for <br>"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.