elrindir / obsidian-csv-codeblock

Plugin for obsidian to render csv syntax in codeblocks.
MIT License
5 stars 1 forks source link

CSV Codeblock

This is a plugin for Obsidian (https://obsidian.md). This plugin renders codeblocks with csv format as tables.

Usage

Just put the csv identifyer in a codeblock:


```csv
Pos, Date, Name, Price
1, 2023-09-28, Spices, 150$
2, 2023-02-29, Shovel, 1.25$

![A preview of the rendered table](https://github.com/elrindir/obsidian-csv-codeblock/blob/master/rendered_table.png)

# Stolen Code

I stole the code from https://docs.obsidian.md/Plugins/Editor/Markdown+post+processing#Post-process+Markdown+code+blocks

```ts
import { Plugin } from "obsidian";

export default class ExamplePlugin extends Plugin {
  async onload() {
    this.registerMarkdownCodeBlockProcessor("csv", (source, el, ctx) => {
      const rows = source.split("\n").filter((row) => row.length > 0);

      const table = el.createEl("table");
      const body = table.createEl("tbody");

      for (let i = 0; i < rows.length; i++) {
        const cols = rows[i].split(",");

        const row = body.createEl("tr");

        for (let j = 0; j < cols.length; j++) {
          row.createEl("td", { text: cols[j] });
        }
      }
    });
  }
}

Changelog

v1.1 Added TSV Support

on Jul 18, 2024

Now supports tsv in codeblocks. Thanks mklepaczewski for the request.

v1.0 Initial Release

on Sep 28, 2023