Skip to content

Commit

Permalink
feat(CSV Crumbs): ✨ Use a CSV file to add Breadcrumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Oct 8, 2021
1 parent 4a48ea8 commit d794db9
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"Path View",
"Hierarchy Note",
"WriteBCToFile",
"LimitTrailFields"
"LimitTrailFields",
"CSV Crumbs"
]
}
65 changes: 56 additions & 9 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var obsidian = require('obsidian');
require('path');
require('fs');

var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};

Expand Down Expand Up @@ -24491,6 +24492,16 @@ class BreadcrumbsSettingTab extends obsidian.PluginSettingTab {
});
const generalDetails = containerEl.createEl("details");
generalDetails.createEl("summary", { text: "General Options" });
new obsidian.Setting(generalDetails)
.setName('CSV Breadcrumb Paths')
.setDesc('The file path of a csv files with breadcrumbs information.')
.addText(text => {
text.setValue(settings.CSVPaths);
text.inputEl.onblur = async () => {
settings.CSVPaths = text.inputEl.value;
await plugin.saveSettings();
};
});
new obsidian.Setting(generalDetails)
.setName("Refresh Index on Note Change")
.setDesc("Refresh the Breadcrumbs index data everytime you change notes. This is how Breadcrumbs used to work, making it responsive to changes immediately after changing notes. However, this can be very slow on large vaults, so it is off by default.")
Expand Down Expand Up @@ -37245,9 +37256,13 @@ class TrailPath extends SvelteComponent {
}
}

// import csv from 'csv-parse';
// import csv2json from 'csv2json'
// import { csvParse } from "d3-dsv";
const DEFAULT_SETTINGS = {
userHierarchies: [],
indexNote: [""],
CSVPaths: '',
hierarchyNotes: [""],
hierarchyNoteDownFieldName: "",
hierarchyNoteUpFieldName: "",
Expand Down Expand Up @@ -37609,6 +37624,33 @@ class BreadcrumbsPlugin extends obsidian.Plugin {
g.setEdge(currFileName, field, { dir, fieldName });
});
}
async getCSVRows(basePath) {
const { CSVPaths } = this.settings;
if (CSVPaths[0] === '')
return;
const fullPath = obsidian.normalizePath(CSVPaths[0]);
const content = await this.app.vault.adapter.read(fullPath);
const lines = content.split('\n');
const headers = lines[0].split(',').map(head => head.trim());
const CSVRows = [];
lines.slice(1).forEach(row => {
const rowObj = {};
row.split(',').map(head => head.trim()).forEach((item, i) => {
rowObj[headers[i]] = item;
});
CSVRows.push(rowObj);
});
console.log({ CSVRows });
return CSVRows;
}
addCSVCrumbs(g, CSVRows, dir, fieldName) {
CSVRows.forEach(row => {
g.setNode(row.file, { dir, fieldName });
if (fieldName === "" || !row[fieldName])
return;
g.setEdge(row.file, row[fieldName], { dir, fieldName });
});
}
async initGraphs() {
var _a;
const settings = this.settings;
Expand Down Expand Up @@ -37655,18 +37697,23 @@ class BreadcrumbsPlugin extends obsidian.Plugin {
});
graphs.hierGs.push(newGraphs);
});
relObjArr.forEach((relObj) => {
const currFileName = relObj.current.basename || relObj.current.name;
relObj.hierarchies.forEach((hier, i) => {
DIRECTIONS.forEach((dir) => {
Object.keys(hier[dir]).forEach((fieldName) => {
const g = graphs.hierGs[i][dir][fieldName];
const fieldValues = hier[dir][fieldName];
this.populateGraph(g, currFileName, fieldValues, dir, fieldName);
if (settings.CSVPaths !== '') {
const { basePath } = this.app.vault.adapter;
const CSVRows = await this.getCSVRows(basePath);
relObjArr.forEach((relObj) => {
const currFileName = relObj.current.basename || relObj.current.name;
relObj.hierarchies.forEach((hier, i) => {
DIRECTIONS.forEach((dir) => {
Object.keys(hier[dir]).forEach((fieldName) => {
const g = graphs.hierGs[i][dir][fieldName];
const fieldValues = hier[dir][fieldName];
this.populateGraph(g, currFileName, fieldValues, dir, fieldName);
this.addCSVCrumbs(g, CSVRows, dir, fieldName);
});
});
});
});
});
}
if (hierarchyNotesArr.length) {
const { hierarchyNoteUpFieldName, hierarchyNoteDownFieldName } = settings;
if (hierarchyNoteUpFieldName !== "") {
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@rollup/plugin-commonjs": "^18.1.0",
"@rollup/plugin-node-resolve": "^11.2.1",
"@rollup/plugin-typescript": "^8.2.1",
"@types/csv2json": "^1.4.2",
"@types/graphlib": "^2.1.7",
"@types/lodash": "^4.14.171",
"@types/node": "^14.14.37",
Expand All @@ -36,7 +37,11 @@
},
"dependencies": {
"@tsconfig/svelte": "^2.0.1",
"csv-parse": "^4.16.3",
"csv2json": "^2.0.2",
"csvjson-csv2json": "^5.0.6",
"d3": "^6.7.0",
"fs": "0.0.1-security",
"graphlib": "^2.1.8",
"hierarchy-js": "^1.0.4",
"juggl-api": "git+https://github.com/HEmile/juggl-api.git",
Expand Down
11 changes: 11 additions & 0 deletions src/BreadcrumbsSettingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,17 @@ export class BreadcrumbsSettingTab extends PluginSettingTab {
const generalDetails: HTMLDetailsElement = containerEl.createEl("details");
generalDetails.createEl("summary", { text: "General Options" });

new Setting(generalDetails)
.setName('CSV Breadcrumb Paths')
.setDesc('The file path of a csv files with breadcrumbs information.')
.addText(text => {
text.setValue(settings.CSVPaths)
text.inputEl.onblur = async () => {
settings.CSVPaths = text.inputEl.value
await plugin.saveSettings()
}
})

new Setting(generalDetails)
.setName("Refresh Index on Note Change")
.setDesc(
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { FrontMatterCache, Pos, TFile } from "obsidian";
export interface BreadcrumbsSettings {
userHierarchies: userHierarchy[];
indexNote: string[];
CSVPaths: string;
hierarchyNotes: string[];
hierarchyNoteDownFieldName: string;
hierarchyNoteUpFieldName: string;
Expand Down
64 changes: 55 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
addIcon,
EventRef,
MarkdownView,
normalizePath,
Notice, Plugin,
TFile,
WorkspaceLeaf
Expand Down Expand Up @@ -35,17 +36,27 @@ import {
getObsMetadataCache,
mergeGs,
oppFields, removeDuplicates,
splitAndTrim,
writeBCToFile
} from "src/sharedFunctions";
import StatsView from "src/StatsView";
import { VisModal } from "src/VisModal";
import TrailGrid from "./Components/TrailGrid.svelte";
import TrailPath from "./Components/TrailPath.svelte";
import * as fs from 'fs'
import { promises as fsp } from 'fs';
// import csv2json from "csv2json";
import { csvParse } from "d3-dsv";
import { head } from "lodash";
// import csv from 'csv-parse';
// import csv2json from 'csv2json'
// import { csvParse } from "d3-dsv";


const DEFAULT_SETTINGS: BreadcrumbsSettings = {
userHierarchies: [],
indexNote: [""],
CSVPaths: '',
hierarchyNotes: [""],
hierarchyNoteDownFieldName: "",
hierarchyNoteUpFieldName: "",
Expand Down Expand Up @@ -485,6 +496,36 @@ export default class BreadcrumbsPlugin extends Plugin {
});
}

async getCSVRows(basePath: string) {
const { CSVPaths } = this.settings
if (CSVPaths[0] === '') return
const fullPath = normalizePath(CSVPaths[0])

const content = await this.app.vault.adapter.read(fullPath)
const lines = content.split('\n')

const headers = lines[0].split(',').map(head => head.trim())
const CSVRows: { [key: string]: string }[] = []
lines.slice(1).forEach(row => {
const rowObj = {}
row.split(',').map(head => head.trim()).forEach((item, i) => {
rowObj[headers[i]] = item
})
CSVRows.push(rowObj)
})

console.log({ CSVRows })
return CSVRows

}
addCSVCrumbs(g: Graph, CSVRows: { [key: string]: string }[], dir: Directions, fieldName: string) {
CSVRows.forEach(row => {
g.setNode(row.file, { dir, fieldName });
if (fieldName === "" || !row[fieldName]) return;
g.setEdge(row.file, row[fieldName], { dir, fieldName })
})
}

async initGraphs(): Promise<BCIndex> {
const settings = this.settings;
debugGroupStart(settings, "debugMode", "Initialise Graphs");
Expand Down Expand Up @@ -551,20 +592,25 @@ export default class BreadcrumbsPlugin extends Plugin {
graphs.hierGs.push(newGraphs);
});

relObjArr.forEach((relObj) => {
const currFileName = relObj.current.basename || relObj.current.name;
if (settings.CSVPaths !== '') {
const { basePath } = this.app.vault.adapter
const CSVRows = await this.getCSVRows(basePath)
relObjArr.forEach((relObj) => {
const currFileName = relObj.current.basename || relObj.current.name;

relObj.hierarchies.forEach((hier, i) => {
DIRECTIONS.forEach((dir: Directions) => {
Object.keys(hier[dir]).forEach((fieldName) => {
const g = graphs.hierGs[i][dir][fieldName];
const fieldValues = hier[dir][fieldName];
relObj.hierarchies.forEach((hier, i) => {
DIRECTIONS.forEach((dir: Directions) => {
Object.keys(hier[dir]).forEach((fieldName) => {
const g = graphs.hierGs[i][dir][fieldName];
const fieldValues = hier[dir][fieldName];

this.populateGraph(g, currFileName, fieldValues, dir, fieldName);
this.populateGraph(g, currFileName, fieldValues, dir, fieldName);
this.addCSVCrumbs(g, CSVRows, dir, fieldName);
});
});
});
});
});
}

if (hierarchyNotesArr.length) {
const { hierarchyNoteUpFieldName, hierarchyNoteDownFieldName } = settings;
Expand Down

0 comments on commit d794db9

Please sign in to comment.