diff options
| author | Conway <[email protected]> | 2026-09-02 16:56:20 -0400 |
|---|---|---|
| committer | Conway <[email protected]> | 2026-09-02 16:56:20 -0400 |
| commit | 84560e948dbbc24cdcd593bb0be591085f0ad1b2 (patch) | |
| tree | 4aaadd4b07d38572627f134873976ed4d81edb2a /.obsidian/plugins/table-trimmer | |
| parent | 5eeea439d6ee3c2f5ddbdd56ebb6a19ef199686e (diff) | |
Diffstat (limited to '.obsidian/plugins/table-trimmer')
| -rw-r--r-- | .obsidian/plugins/table-trimmer/main.js | 98 |
1 files changed, 8 insertions, 90 deletions
diff --git a/.obsidian/plugins/table-trimmer/main.js b/.obsidian/plugins/table-trimmer/main.js index f162293..760b820 100644 --- a/.obsidian/plugins/table-trimmer/main.js +++ b/.obsidian/plugins/table-trimmer/main.js @@ -2,18 +2,12 @@ /* Table Blank Line Remover. Obsidian will not normally let you delete the blank line above a table in Live Preview. This plugin takes that section of the document and surgically removes the newline that gets reintroduced. */ -const { Plugin, Notice } = require("obsidian"); - -const VERSION = "1.4.0"; +const { Plugin } = require("obsidian"); // Guard against pathological scans through a note that is mostly blank lines. const MAX_BLANK_SCAN = 100; -/* ------------------------------------------------------------------ * - * Pure markdown helpers. - * - * They all take a `doc`: { count: number, get(i): string }, 0-indexed. - * ------------------------------------------------------------------ */ +// The markdown helpers below all take a `doc`: { count: number, get(i): string }, 0-indexed. function isBlank(text) { return text.trim() === ""; @@ -93,12 +87,9 @@ function lastNonBlankAbove(doc, i) { return j; } -/* ------------------------------------------------------------------ * - * The edit: replace the lines around the table wholesale, leaving the - * blank line out. Scoped to the smallest region that still reads as a - * block to Obsidian rather than as a newline being deleted. - * ------------------------------------------------------------------ */ - +// The edit: replace the lines around the table wholesale, leaving the blank +// line out. Scoped to the smallest region that still reads as a block to +// Obsidian rather than as a newline being deleted. function blockRewrite(doc, plan) { const n = plan.blankLine; const t = plan.tableLine; @@ -129,11 +120,9 @@ function blockRewrite(doc, plan) { }; } -/* ------------------------------------------------------------------ * - * Edit plans: { blankLine, caret, direction, tableLine }, where `caret` - * is already in post-deletion coordinates and `tableLine` is in - * pre-deletion coordinates. Null means "not our business". - * ------------------------------------------------------------------ */ +/* Edit plans: { blankLine, caret, direction, tableLine }, where `caret` is + * already in post-deletion coordinates and `tableLine` is in pre-deletion + * coordinates. Null means "not our business". */ function planBackspace(doc, cursor) { const { line, ch } = cursor; @@ -202,22 +191,10 @@ function docFromEditor(editor) { return { count: editor.lineCount(), get: (i) => editor.getLine(i) }; } -/* ------------------------------------------------------------------ * - * Plugin - * ------------------------------------------------------------------ */ - class TableBlankLineRemover extends Plugin { onload() { - console.log(`[TBLR] Table Blank Line Remover v${VERSION}`); - // Capture phase: ahead of CodeMirror and Obsidian's own handling. this.registerDomEvent(window, "keydown", (evt) => this.onKeyDown(evt), { capture: true }); - - this.addCommand({ - id: "tighten-headings-above-tables", - name: "Remove blank lines between headings and tables in this note", - editorCallback: (editor) => this.tightenHeadings(editor), - }); } onKeyDown(evt) { @@ -270,65 +247,6 @@ class TableBlankLineRemover extends Plugin { editor.setCursor(plan.caret); return true; } - - // Bulk pass. Deliberately limited to headings: a table pulled tight against - // a paragraph gets absorbed into that paragraph by the markdown parser, - // which is not a change anyone wants applied to a whole note at once. - tightenHeadings(editor) { - const doc = docFromEditor(editor); - const targets = []; - - for (let i = 0; i < doc.count; i++) { - if (!tableStartsAt(doc, i)) continue; - const above = lastNonBlankAbove(doc, i); - if (above < 0 || above === i - 1) continue; - if (!/^\s{0,3}#{1,6}\s/.test(doc.get(above))) continue; - targets.push({ table: i, firstBlank: above + 1 }); - } - - if (targets.length === 0) { - new Notice("No blank lines between headings and tables."); - return; - } - - // Bottom-up, one blank line at a time, so the line numbers above each - // removal stay valid. The table climbs by one after each success. - let removed = 0; - for (let t = targets.length - 1; t >= 0; t--) { - const { table, firstBlank } = targets[t]; - let shift = 0; - for (let i = table - 1; i >= firstBlank; i--) { - const ok = this.applyPlan(editor, { - blankLine: i, - caret: { line: i, ch: 0 }, - direction: "above", - tableLine: table - shift, - }); - if (ok) { - removed++; - shift++; - } - } - } - - new Notice( - `Removed ${removed} blank line${removed === 1 ? "" : "s"} above ` + - `${targets.length} table${targets.length === 1 ? "" : "s"}.` - ); - } } module.exports = TableBlankLineRemover; -// Obsidian accepts either shape depending on how the file was built; expose both. -module.exports.default = TableBlankLineRemover; -module.exports._internals = { - isDelimiterRow, - isTableRowLine, - isTableStart, - isInsideCodeBlock, - tableStartsAt, - tableStartFor, - blockRewrite, - planBackspace, - planDelete, -}; |
