Skip to content

Commit

Permalink
fix(CSV Crumbs): 🐛 fixes #142 CSV File shouldn't be needed to show re…
Browse files Browse the repository at this point in the history
…gular BCs
  • Loading branch information
SkepticMystic committed Oct 9, 2021
1 parent 5ef9616 commit df3de06
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 47 deletions.
50 changes: 30 additions & 20 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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 @@ -6638,6 +6637,14 @@ function hierToStr(hier) {
function removeDuplicates(arr) {
return [...new Set(arr)];
}
/**
* Adds or updates the given yaml `key` to `value` in the given TFile
* @param {string} key
* @param {string} value
* @param {TFile} file
* @param {FrontMatterCache|undefined} frontmatter
* @param {{[fun:string]:(...args:any} api
*/
const createOrUpdateYaml = async (key, value, file, frontmatter, api) => {
let valueStr = value.toString();
if (!frontmatter || frontmatter[key] === undefined) {
Expand Down Expand Up @@ -37256,9 +37263,6 @@ class TrailPath extends SvelteComponent {
}
}

// import csv from 'csv-parse';
// import csv2json from 'csv2json'
// import { csvParse } from "d3-dsv";
const DEFAULT_SETTINGS = {
userHierarchies: [],
indexNote: [""],
Expand Down Expand Up @@ -37626,13 +37630,14 @@ class BreadcrumbsPlugin extends obsidian.Plugin {
}
async getCSVRows(basePath) {
const { CSVPaths } = this.settings;
if (CSVPaths[0] === '')
return;
const CSVRows = [];
if (CSVPaths[0] === '') {
return CSVRows;
}
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) => {
Expand Down Expand Up @@ -37697,23 +37702,28 @@ class BreadcrumbsPlugin extends obsidian.Plugin {
});
graphs.hierGs.push(newGraphs);
});
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);
const useCSV = settings.CSVPaths !== '';
let basePath;
let CSVRows;
if (useCSV) {
basePath = this.app.vault.adapter.basePath;
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);
if (useCSV) {
this.addCSVCrumbs(g, CSVRows, dir, fieldName);
});
}
});
});
});
}
});
if (hierarchyNotesArr.length) {
const { hierarchyNoteUpFieldName, hierarchyNoteDownFieldName } = settings;
if (hierarchyNoteUpFieldName !== "") {
Expand Down
51 changes: 25 additions & 26 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,12 @@ import {
getNeighbourObjArr,
getObsMetadataCache,
mergeGs,
oppFields, removeDuplicates,
splitAndTrim,
writeBCToFile
oppFields, removeDuplicates, 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 = {
Expand Down Expand Up @@ -498,14 +488,14 @@ export default class BreadcrumbsPlugin extends Plugin {

async getCSVRows(basePath: string) {
const { CSVPaths } = this.settings
if (CSVPaths[0] === '') return
const CSVRows: { [key: string]: string }[] = []
if (CSVPaths[0] === '') { return CSVRows }
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) => {
Expand All @@ -518,6 +508,7 @@ export default class BreadcrumbsPlugin extends Plugin {
return CSVRows

}

addCSVCrumbs(g: Graph, CSVRows: { [key: string]: string }[], dir: Directions, fieldName: string) {
CSVRows.forEach(row => {
g.setNode(row.file, { dir, fieldName });
Expand Down Expand Up @@ -592,25 +583,33 @@ export default class BreadcrumbsPlugin extends Plugin {
graphs.hierGs.push(newGraphs);
});

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;
const useCSV = settings.CSVPaths !== ''
let basePath: string;
let CSVRows: { [key: string]: string }[];

if (useCSV) {
basePath = this.app.vault.adapter.basePath
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);
if (useCSV) {
this.addCSVCrumbs(g, CSVRows, dir, fieldName);
});
}
});
});
});
}
});


if (hierarchyNotesArr.length) {
const { hierarchyNoteUpFieldName, hierarchyNoteDownFieldName } = settings;
Expand Down
9 changes: 8 additions & 1 deletion src/sharedFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,14 @@ export function hierToStr(hier: userHierarchy) {
export function removeDuplicates<T>(arr: T[]) {
return [...new Set(arr)];
}

/**
* Adds or updates the given yaml `key` to `value` in the given TFile
* @param {string} key
* @param {string} value
* @param {TFile} file
* @param {FrontMatterCache|undefined} frontmatter
* @param {{[fun:string]:(...args:any} api
*/
export const createOrUpdateYaml = async (
key: string,
value: string,
Expand Down

0 comments on commit df3de06

Please sign in to comment.