Skip to content

support html past (e.g., jupyter notebook, QUIP) #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"esbuild": "0.13.12",
"obsidian": "^0.15.1",
"tslib": "2.3.1",
"typescript": "4.4.4"
"typescript": "4.4.4",
"cheerio": "^1.0.0-rc.10"
}
}
11 changes: 10 additions & 1 deletion src/excel-markdown-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,14 @@ export function excelRowsToMarkdown(rows: string[][]): string {
}

export function isExcelData(rows: string[][]): boolean {
return rows && rows[0] && rows[0].length > 1 ? true : false
if (!(rows && rows[0] && rows[0].length > 1)) {
return false;
}
// If we have multiple entries per row, each if this is the case for all rows.
const width: number = rows[0].length;
for (var row of rows) {
console.log("lenght: " + row.length);
if (row.length != width) { return false; }
}
return true;
}
32 changes: 32 additions & 0 deletions src/html-markdown-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as cheerio from 'cheerio';

/**
* Parse HTML content into text with \t and \n
* @param table_html The raw html from clipboard
* @returns Text string similiar to the row content from excel
*/
export function parseHtml(table_html: string): string {
const $ = cheerio.load(table_html);
let trs : cheerio.Cheerio<cheerio.Element> = $('tbody').find('tr');

let table_str: string = "";
trs.each((r_idx: number, row: cheerio.Element) => {
const cells: cheerio.Cheerio<cheerio.Element> = $(row).find('td,th');
const row_text: string[] = [];
cells.each((c_idx: number, cell: cheerio.Element) => {
// ignore hidden rows
const style: string | undefined = $(row).attr('style');
if (style) {
const m = style.match(/.*display.*:.*none.*/g);
if (m && m.length > 0) return;
}

// only consider cell text and ignore html
const content: string = $(cell).text().trim();
row_text.push(content);

});
table_str = table_str + row_text.join("\t") + "\n";
});
return table_str;
}
17 changes: 15 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Editor, MarkdownView, Plugin } from 'obsidian';
import { excelToMarkdown, getExcelRows, isExcelData, excelRowsToMarkdown } from './excel-markdown-tables';
import {parseHtml} from './html-markdown-helpers';

export default class ExcelToMarkdownTablePlugin extends Plugin {
pasteHandler = (evt: ClipboardEvent, editor: Editor) => {
Expand All @@ -9,17 +10,29 @@ export default class ExcelToMarkdownTablePlugin extends Plugin {

// Check for `Shift + Mod + V` triggered event.
// Do not handle `Shift + Mod + V` events.
if (evt.clipboardData.types.length === 1 && evt.clipboardData.types[0] === 'text/plain') {
if (evt.clipboardData.types.length === 1 && evt.clipboardData.types[0] === 'text/plain') {
return;
}

const rawData = evt.clipboardData.getData("text");
const rows = getExcelRows(rawData);

if (isExcelData(rows)) {
const markdownData = excelRowsToMarkdown(rows);
editor.replaceSelection(markdownData + '\n');
evt.preventDefault();
return;
}

// Try parsing as HTML
const rawHtml = evt.clipboardData.getData("text/html");
const htmlRows = getExcelRows(parseHtml(rawHtml));

if (isExcelData(htmlRows)) {
const markdownData = excelRowsToMarkdown(htmlRows);
editor.replaceSelection(markdownData + '\n');
evt.preventDefault();
return;
}
}

Expand Down
Loading