7

I am using the markedjs markdown parser on my website, but the table syntax didn't style the right way (no borders and stripes). My code:

<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Marked in the browser</title>
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
</head>
<body>
<div id="content"></div>
<script>

    document.getElementById('content').innerHTML =
        marked(`
First Header | Second Header
------------ | -------------
Content Cell | Content Cell
Content Cell | Content Cell
        `);
</script>
</body>
</html>
cxw
  • 16,685
  • 2
  • 45
  • 81
neifnei
  • 95
  • 1
  • 8

1 Answers1

2

This is not a marked issue, but a CSS issue. Since you are not specifying any styling for your table, you are not getting any :) .

Edit Here is an example that uses Bootstrap with markedjs.

Non-Bootstrap example:

<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Marked in the browser</title>
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <style>
        table { border-collapse: collapse; }
        tr { border-bottom: solid 1px black; }
        tr:nth-child(even) {background-color: #f2f2f2;}
    </style>
</head>
<body>
<div id="content"></div>
<script>

    document.getElementById('content').innerHTML =
        marked(`
First Header | Second Header
------------ | -------------
Content Cell | Content Cell
Content Cell | Content Cell
        `);
</script>
</body>
</html>
  • table { border-collapse: collapse; } enables per-tr borders, per this answer.
  • tr { ... } gives you the lines between rows.
  • tr:nth-child(even) { ... } gives you the shading.
cxw
  • 16,685
  • 2
  • 45
  • 81
  • Thank you. Can I use bootstrap with markedjs, Do I need to add bootstrap class (like "table-striped") to the table tag dynamically every time after converting markdown to html? – neifnei Mar 24 '18 at 16:32
  • Edited re. Bootstrap. As far as adding the bootstrap classes, I would suggest asking a separate question, since that might be more generally applicable. Based on [this](https://github.com/markedjs/marked/blob/master/docs/USING_PRO.md#user-content-block-level-renderer-methods) and [this](https://github.com/markedjs/marked/issues/334#issuecomment-33571577), I think you need to write a custom renderer. – cxw Mar 24 '18 at 16:46