🏠 back to Observable

Including HTML(and tex) into markdown

Hey there, I am trying to do this very simple thing (see notebook)

  const array = [tex`A=1`, tex`A=2`]

  return md`# Breaking Markdown

Not broken:
- ${array[0]}
- ${array[1]}

${array.map(a => `- ${a}\n`)}

${array.map(a => `- ${a}`).join('\n')}

However this renders

[object HTMLSpanElement]
[object HTMLSpanElement]

How do I avoid this?

Here’s a guide in response to your question:

And the solution:

md`A list of elements:

  ${array.map(a => html`<li>${a}`)}

You need to use html`- ${a}\n` or md`- ${a}\n` instead of `- ${a}\n`. It sometimes gets a bit tricky if you want the stuff inside to be parsed correctly by the outer markdown template. Generally safer to use raw html to define nested elements in sub-templates instead of using markdown syntax.

Edit: you can also do:

  const array = [tex`A=1`, tex`A=2`];
  const between = Array(array.length - 1).fill("\n- ");

  return md`# Breaking Markdown

Not broken:
${ md(["- ", ...between, ""], ...array) }

P.S. One other issue here is that you are re-using your array = [tex`A=1`, tex`A=2`] array which just has a single node for each of those tex templates; If you want to re-use that in multiple places you need to render new nodes each time.

1 Like

Hey mike this is fantastic, thank you!

1 Like

All of your suggestions work for lists but I cant make them work for tables :S, any hint?

Hmm, seems to work for me? What are you trying?

md`A column of elements:

  ${array.map(a => html`<tr><td>${a}`)}
md`A row of elements:

  ${array.map(a => html`<td>${a}`)}