Code Monkey home page Code Monkey logo

markdown-it-complex-table's Introduction

markdown-it-complex-table

Markdown syntax is designed to be readable and writeable, but Markdown table is not. Because common text editor don't have concept of column and isn't always monospace fonts.

The best way to express column in common text editor is YAML. This plugin provides a simple way to write complex html.

Install

npm install markdown-it-complex-table

Usage

import md from "markdown-it"
import complexTable from "markdown-it-complex-table"

const mdString = `
\`\`\`yaml:table
- 
  - !td
  - Mars
  - !cs 
  - Venus
  - !cs 
- 
  - !rs
  - &h1 Produced
  - &h2 Sold
  - *h1
  - *h2
---
- 
  - !th Teddy Bears
  - 50,000
  - 30,000
  - 100,000
  - 80,000
- 
  - !th Board Games
  - 10,000
  - 5,000
  - 12,000
  - 9,000
\`\`\`
` // from https://www.w3.org/WAI/tutorials/tables/irregular/

const htmlString = md().use(complexTable).parse(mdString) // "<table>...</table>"

htmlString is the following.

Mars Venus
Produced Sold Produced Sold
Teddy Bears 50,000 30,000 100,000 80,000
Board Games 10,000 5,000 12,000 9,000
HTML
<table>
    <col>
    <colgroup span="2"></colgroup>
    <colgroup span="2"></colgroup>
    <thead>
        <tr>
          <td rowspan="2"></td>
          <th colspan="2" scope="colgroup">Mars</th>
          <th colspan="2" scope="colgroup">Venus</th>
        </tr>
        <tr>
            <th scope="col">Produced</th>
            <th scope="col">Sold</th>
            <th scope="col">Produced</th>
            <th scope="col">Sold</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th scope="row">Teddy Bears</th>
            <td>50,000</td>
            <td>30,000</td>
            <td>100,000</td>
            <td>80,000</td>
        </tr>
        <tr>
            <th scope="row">Board Games</th>
            <td>10,000</td>
            <td>5,000</td>
            <td>12,000</td>
            <td>9,000</td>
        </tr>
    </tbody>
</table>

Rules

please see here.

Examples

You can use Markdown sytnax in table.

import md from "markdown-it"
import complexTable from "markdown-it-complex-table"
import hljs from "highlight.js"

const mdString = `
\`\`\`yaml:table
[Emphasis, Lists, Code Blocks]
---
- "**bold**"
- |
  - first
  - second
- |
  ~~~python
  import os
  print(f'Hello, {os.getlogin()}')
  ~~~
\`\`\`
`

const htmlString = md({
  highlight: (code, info) => {
    if (info && hljs.getLanguage(info)) {
      return hljs.highlight(code, { language: info }).value
    }
    return ""
  },
}).use(complexTable).render(mdString)

htmlString is the following.

Emphasis Lists Code Blocks
bold
  • first
  • second
import os
print(f'Hello, {os.getlogin()}')
HTML
<table>
    <thead>
        <tr>
            <th scope="col">Emphasis</th>
            <th scope="col">Lists</th>
            <th scope="col">Code Blocks</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><strong>bold</strong></td>
            <td>
                <ul>
                    <li>first</li>
                    <li>second</li>
                </ul>
            </td>
            <td>
                <pre><code class="language-python"><span class="hljs-keyword">import</span> os
<span class="hljs-built_in">print</span>(<span class="hljs-string">f'Hello, <span class="hljs-subst">{os.getlogin()}</span>'</span>)
</code></pre>
            </td>
        </tr>
    </tbody>
</table>

markdown-it-complex-table's People

Contributors

nosuke23 avatar

Stargazers

Mccc avatar septs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.