Skip to content

Commit

Permalink
Merge pull request #75 from saertna/Refactoring-interfaces-obsidian
Browse files Browse the repository at this point in the history
Refactoring interfaces obsidian
  • Loading branch information
saertna authored Jun 13, 2024
2 parents 2d6d4d7 + a72666c commit c846886
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 286 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ data.json

# zip-archive
obsidian-gamified-pkm/obsidian-gamified-pkm.zip
*.zip

# Exclude macOS Finder (System Explorer) View States
coverage/
/coverage/
.DS_Store
/cp-js2cortex.bat
ncryption
Expand Down
95 changes: 54 additions & 41 deletions src/creatmodchartcalculation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import {TFile} from 'obsidian';
import { debugLogs } from './constants';

export function findEarliestCreatedFile(files: TFile[]): TFile {
let earliestCreatedFile: TFile = files[0];
export interface AbstractFile {
path: string;
}

export interface FileInterface extends AbstractFile{
stat: {
ctime: number;
mtime: number;
size: number;
};
}

export interface VaultInterface {
getAbstractFileByPath(path: string): AbstractFile | null;
read(file: FileInterface): Promise<string>;
modify(file: FileInterface, data: string): Promise<void>;
}

export function findEarliestCreatedFile<T extends FileInterface>(files: T[]): T {
let earliestCreatedFile: T = files[0];
for (const file of files) {
if (file.stat.ctime < earliestCreatedFile.stat.ctime) {
earliestCreatedFile = file;
Expand All @@ -12,8 +30,8 @@ export function findEarliestCreatedFile(files: TFile[]): TFile {
}


export function findEarliestModifiedFile(files: TFile[]): TFile {
let earliestModifiedFile: TFile = files[0];
export function findEarliestModifiedFile<T extends FileInterface>(files: T[]): T {
let earliestModifiedFile: T = files[0];
for (const file of files) {
if (file.stat.mtime < earliestModifiedFile.stat.mtime) {
earliestModifiedFile = file;
Expand All @@ -23,8 +41,8 @@ export function findEarliestModifiedFile(files: TFile[]): TFile {
}


export function findEarliestDateFile(files: TFile[]): TFile {
let earliestCreatedFile: TFile = files[0];
export function findEarliestDateFile<T extends FileInterface>(files: T[]): T {
let earliestCreatedFile: T = files[0];
for (const file of files) {
if (file.stat.ctime < earliestCreatedFile.stat.ctime) {
earliestCreatedFile = file;
Expand All @@ -46,25 +64,21 @@ export function monthsBetween(startMonth: Date, endMonth: Date): number {
}


export function getCreationDates(files: TFile[]): Array<Date> {
export function getCreationDates<T extends FileInterface>(files: T[]): Array<Date> {
const creationDates: Array<Date> = [];

for (const file of files) {
creationDates.push(new Date(file.stat.ctime));
}

return creationDates;
}


export function getModificationDates(files: TFile[]): Array<Date> {
const creationDates: Array<Date> = [];

export function getModificationDates<T extends FileInterface>(files: T[]): Array<Date> {
const modificationDates: Array<Date> = [];
for (const file of files) {
creationDates.push(new Date(file.stat.mtime));
modificationDates.push(new Date(file.stat.mtime));
}

return creationDates;
return modificationDates;
}


Expand All @@ -79,35 +93,34 @@ export function createChartFormat(y_axis: string, countsStringMod: string, chart
return "```chart\ntype: bar\nlabels: [" + y_axis + "]\nseries:\n - title: modified\n data: [" + countsStringMod + "]\ntension: 0.2\nwidth: 80 %\nlabelColors: false\nfill: false\nbeginAtZero: false\nbestFit: false\nbestFitTitle: undefined\nbestFitNumber: 0\nstacked: true\nyTitle: \"Number of Notes\"\nxTitle: \"Months\"\nxMin: " + monatsbegrenzung + "\n```";
}


export async function replaceChartContent (avatarPageName: string, newContent: string) {
const existingFile = this.app.vault.getAbstractFileByPath(`${avatarPageName}.md`);
if (existingFile == null) {
if(debugLogs) console.debug(`File ${avatarPageName}.md does not exist`);
return;
}
const file = existingFile as TFile;

const content = await this.app.vault.read(file);
let reference: number | null = null;
let end: number | null = null;
let start: number | null = null;
export async function replaceChartContent(
avatarPageName: string,
newContent: string,
vault: VaultInterface,
debugLogs = false
): Promise<void> {
const existingFile = vault.getAbstractFileByPath(`${avatarPageName}.md`);
if (!existingFile) {
if (debugLogs) console.debug(`File ${avatarPageName}.md does not exist`);
return;
}

const file = existingFile as FileInterface;
const content = await vault.read(file);
const lines = content.split("\n");
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (line === "^ChartMonth") {
if (reference === null) {
reference = i;
}
}
}
if (reference != null){
end = reference;
start = reference - 19;
const newLines = [...lines.slice(0, start), newContent, ...lines.slice(end)];
await this.app.vault.modify(file, newLines.join("\n"));
}

const referenceIndex = lines.findIndex(line => line.trim() === "^ChartMonth");
if (referenceIndex === -1) return;

// Calculate start index for replacement (ensure it doesn't go below 0)
const start = Math.max(referenceIndex - 19, 0);

// Slice lines and replace the specified block with new content
const newLines = [...lines.slice(0, start), newContent, ...lines.slice(referenceIndex + 1)];
await vault.modify(file, newLines.join("\n"));
}




Loading

0 comments on commit c846886

Please sign in to comment.