Skip to content

Commit

Permalink
feat: share new and edited notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Jun 17, 2022
1 parent 5c6c2fb commit 629296e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
29 changes: 28 additions & 1 deletion mkdocsPublisher/githubInteraction/getFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ export class GetFiles {
return false;
}

async getLastEditedTimeRepo(octokit: Octokit, githubRepo: {file: string, sha: string}, settings: MkdocsPublicationSettings) {
const commits = await octokit.request('GET /repos/{owner}/{repo}/commits', {
owner: settings.githubName,
repo: settings.githubRepo,
path: githubRepo.file,
})
const lastCommittedFile = commits.data[0];
return new Date(lastCommittedFile.commit.committer.date);
}

async getAllFileFromRepo(ref = "main", octokit: Octokit, settings: MkdocsPublicationSettings) {
const filesInRepo = [];
try {
Expand Down Expand Up @@ -212,5 +222,22 @@ export class GetFiles {
return newFiles;
}


async getAllplusNewEditedFiles(allFileWithPath:{converted: string, real: string}[] , githubSharedFiles: { file: string, sha: string }[], vault: Vault): Promise<TFile[]>{
const newFiles = this.getNewFiles(allFileWithPath, githubSharedFiles, vault); //new file : present in allFileswithPath but not in githubSharedFiles
for (const file of allFileWithPath) {
if (githubSharedFiles.some((x) => x.file === file.converted.trim())) {
const githubSharedFile = githubSharedFiles.find((x) => x.file === file.converted.trim());
const repoEditedTime = await this.getLastEditedTimeRepo(this.octokit, githubSharedFile, this.settings);
const fileInVault = vault.getAbstractFileByPath(file.real.trim())
if (fileInVault && (fileInVault instanceof TFile) && (fileInVault.extension === 'md')) {
const vaultEditedTime = new Date(fileInVault.stat.mtime);
if (vaultEditedTime > repoEditedTime) {
console.log('edited file ', fileInVault.path, ' / ', vaultEditedTime, ' vs ', repoEditedTime)
newFiles.push(fileInVault);
}
}
}
}
return newFiles;
}
}
32 changes: 18 additions & 14 deletions mkdocsPublisher/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import {MkdocsPublicationSettings, DEFAULT_SETTINGS} from './settings/interface'
import { GetFiles } from "./githubInteraction/getFiles";
import {GithubBranch} from "./githubInteraction/branch";
import { Octokit } from "@octokit/core";
import {deleteUnsharedDeletedNotes, shareAllMarkedNotes, shareOneNote} from "./utils/commands";
import {
deleteUnsharedDeletedNotes,
shareAllEditedNotes,
shareAllMarkedNotes,
shareNewNote,
shareOneNote
} from "./utils/commands";


export default class MkdocsPublication extends Plugin {
Expand Down Expand Up @@ -124,20 +130,18 @@ export default class MkdocsPublication extends Plugin {
this.addCommand({
id: "obs2mk-upload-new",
name: "Share newly note",
// @ts-ignore I really need async here
checkCallback: async (checking) => {
callback: async () => {
await shareNewNote(githubBranch, publish, this.settings, octokit, shareFiles, branchName, this.app.vault);
}
});

this.addCommand({
id: "obs2mk-upload-edited",
name: "Upload all new and edited note",
callback: async () => {
const statusBarElement = this.addStatusBarItem();
const branchMaster = await githubBranch.getMasterBranch();
const sharedFilesWithPaths= shareFiles.getAllFileWithPath();
const githubSharedNotes = await shareFiles.getAllFileFromRepo(branchMaster, octokit, this.settings);
const newlySharedNotes = shareFiles.getNewFiles(sharedFilesWithPaths, githubSharedNotes, this.app.vault);
if (newlySharedNotes.length > 0) {
if (!checking) {
await githubBranch.newBranch(branchName);
await shareAllMarkedNotes(publish, this.settings, octokit, shareFiles, githubBranch, statusBarElement, branchName, newlySharedNotes);
} return true;
} return false;
},
await shareAllEditedNotes(statusBarElement, publish, this.settings, octokit, shareFiles, githubBranch, branchName, this.app.vault);
}
});
}

Expand Down
14 changes: 14 additions & 0 deletions mkdocsPublisher/utils/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,17 @@ export async function shareOneNote(branchName: string, githubBranch: GithubBranc
}


}

export async function shareAllEditedNotes(statusBarElement: HTMLElement, publish: MkdocsPublish, settings: MkdocsPublicationSettings, octokit: Octokit, shareFiles: GetFiles, githubBranch: GithubBranch, branchName: string, vault: Vault) {
const branchMaster = await githubBranch.getMasterBranch();
const sharedFilesWithPaths = shareFiles.getAllFileWithPath();
const githubSharedNotes = await shareFiles.getAllFileFromRepo(branchMaster, octokit, settings);
const newlySharedNotes = await shareFiles.getAllplusNewEditedFiles(sharedFilesWithPaths, githubSharedNotes, vault);
if (newlySharedNotes.length > 0) {
await githubBranch.newBranch(branchName);
await shareAllMarkedNotes(publish, settings, octokit, shareFiles, githubBranch, statusBarElement, branchName, newlySharedNotes);
} else {
new Notice("No new notes to publish.");
}
}

0 comments on commit 629296e

Please sign in to comment.