Skip to content

Commit 574d760

Browse files
committed
added setting for CR/LF handling
1 parent 23be371 commit 574d760

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

main.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface Table2CSVSettings {
1818
sepChar: string;
1919
quoteData: boolean;
2020
saveToClipboardToo: boolean;
21+
removeCRLF: string;
2122
}
2223

2324
const DEFAULT_SETTINGS: Table2CSVSettings = {
@@ -26,7 +27,8 @@ const DEFAULT_SETTINGS: Table2CSVSettings = {
2627
fileNumber: '001',
2728
sepChar: ',',
2829
quoteData: false,
29-
saveToClipboardToo: false
30+
saveToClipboardToo: false,
31+
removeCRLF: 'removeCRLF-space'
3032
}
3133

3234
export default class Table2CSVPlugin extends Plugin {
@@ -49,7 +51,7 @@ export default class Table2CSVPlugin extends Plugin {
4951
const viewMode = view.getMode();
5052
if (viewMode=="preview") {
5153
// Now convert the tables
52-
const csvString = htmlToCSV(view.previewMode.containerEl, this.settings.sepChar, this.settings.quoteData);
54+
const csvString = htmlToCSV(view.previewMode.containerEl, this.settings.sepChar, this.settings.quoteData, this.settings.removeCRLF);
5355

5456
// TODO: prüfen, ob csvString leer oder nicht! Nur wenn nicht, Datei anlegen etc.
5557
if (csvString.length > 0) {
@@ -108,6 +110,8 @@ export default class Table2CSVPlugin extends Plugin {
108110

109111
// This adds a settings tab so the user can configure various aspects of the plugin
110112
this.addSettingTab(new Table2CSVSettingTab(this.app, this));
113+
114+
console.log(`Table to CSV plugin: Version ${this.manifest.version} loaded.`);
111115
}
112116

113117
onunload() {
@@ -123,29 +127,40 @@ export default class Table2CSVPlugin extends Plugin {
123127
}
124128

125129

126-
function htmlToCSV(html: HTMLElement, sep: string, quote: boolean) {
130+
function htmlToCSV(html: HTMLElement, sep: string, quote: boolean, removeCRLF: string) {
127131
var data = [];
128132
var table = html.querySelector("table");
129-
console.log(`htmlToCSV::table: ${table}`);
133+
//console.log(`htmlToCSV::table: ${table}`);
130134

131135
if (table) {
132136
var rows = table.rows;
133-
console.log(`htmlToCSV::rows: ${rows}`);
137+
//console.log(`htmlToCSV::rows: ${rows}`);
134138
for (var i = 0; i < rows.length; i++) {
135139
var row = [], cols = rows[i].querySelectorAll("td, th");
136140

137141
for (var j = 0; j < cols.length; j++) {
138-
if (!quote) {
139-
row.push((cols[j] as HTMLElement).innerText);
140-
} else {
141-
row.push('"' + (cols[j] as HTMLElement).innerText + '"');
142+
var cellContent = (cols[j] as HTMLElement).innerText;
143+
144+
// handle the optional replacement of CR/LF characters:
145+
if (removeCRLF=='removeCRLF-clear') {
146+
cellContent = cellContent.replace(/(\r\n|\n|\r)/gm, "");
147+
} else if (removeCRLF=='removeCRLF-space') {
148+
cellContent = cellContent.replace(/(\r\n|\n|\r)/gm, " ");
149+
} else if (removeCRLF=='removeCRLF-string1') {
150+
cellContent = cellContent.replace(/(\r\n|\n|\r)/gm, "[CR]");
142151
}
152+
153+
// handle the quoting of data cells:
154+
// for now it's just the hard-coded character "
155+
if (quote) cellContent = '"' + cellContent + '"';
156+
157+
row.push(cellContent);
143158
}
144159

145160
data.push(row.join(sep));
146161
}
147162
}
148-
console.log(`htmlToCSV::data.length: ${data.length}`);
163+
//console.log(`htmlToCSV::data.length: ${data.length}`);
149164
if (data.length > 0)
150165
return data.join("\n");
151166
else
@@ -220,7 +235,7 @@ class Table2CSVSettingTab extends PluginSettingTab {
220235

221236
new Setting(containerEl)
222237
.setName('Quote data')
223-
.setDesc('Do you want quotation marks around each cell\'s data?')
238+
.setDesc('Do you want quotation marks (") around each cell\'s data?')
224239
.addToggle( toggle => toggle
225240
.setValue(this.plugin.settings.quoteData)
226241
.onChange(async (value) => {
@@ -239,5 +254,19 @@ class Table2CSVSettingTab extends PluginSettingTab {
239254
this.plugin.settings.saveToClipboardToo = value;
240255
await this.plugin.saveSettings();
241256
}));
257+
258+
new Setting(containerEl)
259+
.setName('Handling of CR/LF in data')
260+
.setDesc('Chose how to handle the occurance of return and linefeed characters in data cells.')
261+
.addDropdown( dropdown => dropdown
262+
.addOption('removeCRLF-clear', 'Remove all CR & LF characters')
263+
.addOption('removeCRLF-space', 'Replace all CR & LF characters with one space')
264+
.addOption('removeCRLF-string1', 'Replace all CR & LF characters with string [CR]')
265+
.setValue(this.plugin.settings.removeCRLF)
266+
.onChange(async (value) => {
267+
this.plugin.settings.removeCRLF = value;
268+
await this.plugin.saveSettings();
269+
}))
270+
242271
}
243272
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "obsidian-table-to-csv-exporter",
33
"name": "Table to CSV Exporter",
4-
"version": "0.1.2",
4+
"version": "0.1.3",
55
"minAppVersion": "0.14.6",
66
"description": "This plugin allows for exporting tables from a pane in reading mode into CSV files.",
77
"author": "Stefan Wolfrum",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-table-to-csv-exporter",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "This plugin allows to export tables in a preview pane to be exported to CSV files.",
55
"main": "main.js",
66
"scripts": {

0 commit comments

Comments
 (0)