From df3de06683c03ec8c213931ee8d468d2f2c1cf12 Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Sat, 9 Oct 2021 13:53:37 +0200 Subject: [PATCH] fix(CSV Crumbs): :bug: fixes #142 CSV File shouldn't be needed to show regular BCs --- main.js | 50 ++++++++++++++++++++++++----------------- src/main.ts | 51 +++++++++++++++++++++--------------------- src/sharedFunctions.ts | 9 +++++++- 3 files changed, 63 insertions(+), 47 deletions(-) diff --git a/main.js b/main.js index 1e6967ac..572c2bbe 100644 --- a/main.js +++ b/main.js @@ -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 : {}; @@ -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) { @@ -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: [""], @@ -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) => { @@ -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 !== "") { diff --git a/src/main.ts b/src/main.ts index 99e9a442..4c8a875a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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 = { @@ -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) => { @@ -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 }); @@ -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; diff --git a/src/sharedFunctions.ts b/src/sharedFunctions.ts index ea914efe..6a95b8ec 100644 --- a/src/sharedFunctions.ts +++ b/src/sharedFunctions.ts @@ -621,7 +621,14 @@ export function hierToStr(hier: userHierarchy) { export function removeDuplicates(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,