commit 26de64d667cbf2aa56ff7e20a9dc89d19285e5b2 Author: Leon Mika Date: Mon Jul 7 22:22:28 2025 +1000 Initial commit diff --git a/main.js b/main.js new file mode 100644 index 0000000..d2422a0 --- /dev/null +++ b/main.js @@ -0,0 +1,50 @@ +const { Plugin } = require('obsidian'); + +class CsvParser { + constructor(lines) { + let rows = lines.split("\n"); + this._cells = rows.map((l) => l.split(",")); + } + + cells() { + return this._cells; + } +} + +class CsvPlugin extends Plugin { + async onload() { + this.addStyle(); + this.registerMarkdownCodeBlockProcessor('csv', (source, el, ctx) => { + const wrapper = el.createEl('div'); + wrapper.addClass('csv-table-wrapper'); + + const table = wrapper.createEl('table'); + table.addClass('csv-table'); + + let cp = new CsvParser(source); + let twrap = null; + + for (let row of cp.cells()) { + if (twrap == null) { + twrap = table.createEl('thead'); + } else if (twrap.nodeName == 'THEAD') { + twrap = table.createEl('tbody'); + } + + let rowElem = twrap.createEl('tr'); + for (let col of row) { + rowElem.createEl('td', { text: col }); + } + } + }); + } + + addStyle() { + const link = document.createElement('link'); + link.rel = 'stylesheet'; + link.href = this.app.vault.adapter.getResourcePath('styles.css'); + document.head.appendChild(link); + } +} + +module.exports = CsvPlugin; \ No newline at end of file diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..4febe86 --- /dev/null +++ b/manifest.json @@ -0,0 +1,10 @@ +{ + "id": "csv-blocks", + "name": "CSV Blocks", + "version": "1.0.0", + "minAppVersion": "0.15.0", + "description": "A plugin that renders CSV code blocks as a table", + "author": "Leon Mika", + "authorUrl": "", + "isDesktopOnly": false +} \ No newline at end of file diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..a3e6662 --- /dev/null +++ b/styles.css @@ -0,0 +1,17 @@ +div.csv-table-wrapper { + max-height: 300px; + overflow-y: auto; +} + +.csv-table { + border-collapse: collapse; +} + +.csv-table thead td { + font-weight: bold; +} + +.csv-table td { + border: 1px solid var(--background-modifier-border); + padding: 4px 8px; +} \ No newline at end of file