Skip to content

Commit

Permalink
feat: Adding a command to share only new shared notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Jun 17, 2022
1 parent 3418f2f commit 176f586
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 146 deletions.
15 changes: 12 additions & 3 deletions mkdocsPublisher/githubInteraction/branch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Octokit } from "@octokit/core";
import { MkdocsPublicationSettings } from "../settings/interface";
import {Octokit} from "@octokit/core";
import {MkdocsPublicationSettings} from "../settings/interface";

export class GithubBranch {
settings: MkdocsPublicationSettings;
Expand All @@ -9,7 +9,16 @@ export class GithubBranch {
this.settings = settings;
this.octokit = octokit;
}


async getMasterBranch() {
const allBranch = await this.octokit.request('GET' + ' /repos/{owner}/{repo}/branches', {
owner: this.settings.githubName,
repo: this.settings.githubRepo,
});
const mainBranch = allBranch.data.find((branch: { name: string; }) => branch.name === 'main' || branch.name === 'master');
return mainBranch.name;
}

async newBranch(branchName: string) {
const allBranch = await this.octokit.request('GET' + ' /repos/{owner}/{repo}/branches', {
owner: this.settings.githubName,
Expand Down
85 changes: 70 additions & 15 deletions mkdocsPublisher/githubInteraction/getFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {
} from "obsidian";
import { MkdocsPublicationSettings } from "../settings/interface";
import { Octokit } from "@octokit/core";
import {getReceiptFolder, getImageLinkOptions} from "../utils/utils";
import {getReceiptFolder, getImageLinkOptions} from "../utils/filePathConvertor";

export class GetFiles {
vault: Vault;
metadataCache: MetadataCache;
settings: MkdocsPublicationSettings;
octokit: Octokit;

constructor(
vault: Vault,
metadataCache: MetadataCache,
Expand All @@ -26,7 +26,7 @@ export class GetFiles {
this.settings = settings;
this.octokit = octokit;
}

getSharedFiles(): TFile[] {
const files = this.vault.getMarkdownFiles();
const shared_File: TFile[] = [];
Expand All @@ -43,7 +43,7 @@ export class GetFiles {
}
return shared_File;
}

getAllFileWithPath() {
const files = this.vault.getFiles();
const allFileWithPath = [];
Expand All @@ -52,14 +52,20 @@ export class GetFiles {
const fileExtension = file.extension;
if (fileExtension.match(/(png|jpe?g|svg|bmp|gif)$/i)) {
const filepath = getImageLinkOptions(file, this.settings);
allFileWithPath.push(filepath);
allFileWithPath.push({
'converted': filepath,
'real': file.path
});
} else if (file.extension == "md") {
const frontMatter = this.metadataCache.getCache(
file.path
).frontmatter;
if (frontMatter && frontMatter[shareKey] === true && file.extension === "md") {
const filepath = getReceiptFolder(file, this.settings, this.metadataCache);
allFileWithPath.push(filepath);
allFileWithPath.push({
'converted': filepath,
'real': file.path
});
}
}
}
Expand All @@ -69,7 +75,7 @@ export class GetFiles {
getLinkedImageAndFiles(file: TFile) {
const linkedFiles = this.getLinkedFiles(file);
const imageEmbedded = this.metadataCache.getFileCache(file).embeds;
if (imageEmbedded != undefined){
if (imageEmbedded != undefined) {
for (const image of imageEmbedded) {
try {
const imageLink = this.metadataCache.getFirstLinkpathDest(image.link, file.path)
Expand All @@ -88,8 +94,8 @@ export class GetFiles {
}
return linkedFiles;
}

getLinkedFiles(file: TFile): {linked: TFile, linkFrom: string, altText: string}[] {
getLinkedFiles(file: TFile): { linked: TFile, linkFrom: string, altText: string }[] {
const embedCaches = this.metadataCache.getCache(file.path).links;
const embedList = [];
if (embedCaches != undefined) {
Expand All @@ -103,8 +109,8 @@ export class GetFiles {
if (linkedFile.extension === 'md') {
embedList.push({
'linked': linkedFile,
'linkFrom' : embedCache.link,
'altText' : embedCache.displayText
'linkFrom': embedCache.link,
'altText': embedCache.displayText
})
}
}
Expand Down Expand Up @@ -141,7 +147,7 @@ export class GetFiles {
}
return [];
}

checkExcludedFolder(file: TFile) {
const excludedFolder = this.settings.ExcludedFolder.split(",").filter(
(x) => x != ""
Expand All @@ -155,7 +161,56 @@ export class GetFiles {
}
return false;
}




async getAllFileFromRepo(ref = "main", octokit: Octokit, settings: MkdocsPublicationSettings) {
const filesInRepo = [];
try {
const repoContents = await octokit.request(
"GET" + " /repos/{owner}/{repo}/git/trees/{tree_sha}",
{
owner: settings.githubName,
repo: settings.githubRepo,
tree_sha: ref,
recursive: "true",
}
);

if (repoContents.status === 200) {
const files = repoContents.data.tree;
for (const file of files) {
const basename = (name: string) =>
/([^/\\.]*)(\..*)?$/.exec(name)[1]; //don't delete file starting with .
if (
file.type === "blob" &&
basename(file.path).length > 0 &&
basename(file.path) != 'vault_published'
) {
filesInRepo.push({
file: file.path,
sha: file.sha,
});
}
}
}
} catch (e) {
console.log(e)
}
return filesInRepo;
}

getNewFiles(allFileWithPath:{converted: string, real: string}[] , githubSharedFiles: { file: string, sha: string }[], vault: Vault): TFile[] {
const newFiles = []; //new file : present in allFileswithPath but not in githubSharedFiles
for (const file of allFileWithPath) {
if (!githubSharedFiles.some((x) => x.file === file.converted.trim())) {
//get TFile from file
const fileInVault = vault.getAbstractFileByPath(file.real.trim())
if (fileInVault && (fileInVault instanceof TFile) && (fileInVault.extension === 'md')) {
newFiles.push(fileInVault);
}
}
}
return newFiles;
}


}
157 changes: 29 additions & 128 deletions mkdocsPublisher/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Notice, Plugin, TFile } from "obsidian";
import {Plugin, TFile } from "obsidian";
import {
MkdocsSettingsTab,
} from "./settings";
import { ShareStatusBar } from "./utils/status_bar";
import MkdocsPublish from "./githubInteraction/upload";
import { disablePublish, noticeMessage } from "./utils/utils";
import { disablePublish } from "./utils/utils";
import {MkdocsPublicationSettings, DEFAULT_SETTINGS} from './settings/interface'
import { deleteFromGithub } from './githubInteraction/delete'
import { GetFiles } from "./githubInteraction/getFiles";
import {GithubBranch} from "./githubInteraction/branch";
import { Octokit } from "@octokit/core";
import {deleteUnsharedDeletedNotes, shareAllMarkedNotes, shareOneNote} from "./utils/commands";


export default class MkdocsPublication extends Plugin {
Expand All @@ -28,6 +27,7 @@ export default class MkdocsPublication extends Plugin {
);
const githubBranch = new GithubBranch(this.settings, octokit);
const shareFiles = new GetFiles(this.app.vault, this.app.metadataCache, this.settings, octokit);
const branchName = app.vault.getName() + "-" + new Date().toLocaleDateString('en-US').replace(/\//g, '-');


this.registerEvent(
Expand All @@ -45,22 +45,7 @@ export default class MkdocsPublication extends Plugin {
)
.setIcon("share")
.onClick(async () => {
try {
const branchName = this.app.vault.getName() + "-" + new Date().toLocaleDateString('en-US').replace(/\//g, '-');
await githubBranch.newBranch(branchName);
const publishSuccess =
await publish.publish(file, true, branchName);
if (publishSuccess) {
const update=await githubBranch.updateRepository(branchName);
if (update) {
await noticeMessage(publish, file, this.settings)
}
}

} catch (e) {
console.error(e);
new Notice("Error publishing to " + this.settings.githubRepo + ".");
}
await shareOneNote(branchName, githubBranch, publish, this.settings, file);
});
});
menu.addSeparator();
Expand All @@ -83,24 +68,7 @@ export default class MkdocsPublication extends Plugin {
)
.setIcon("share")
.onClick(async () => {
try {
const branchName = this.app.vault.getName() + "-" + new Date().toLocaleDateString('en-US').replace(/\//g, '-');
await githubBranch.newBranch(branchName);
const publishSuccess =
await publish.publish(view.file, true, branchName);
if (publishSuccess) {
const update = await githubBranch.updateRepository(branchName);
if (update) {
await noticeMessage(publish, view.file, this.settings);
} else {
new Notice("Error publishing to " + this.settings.githubRepo + ".");

}
}
} catch (e) {
console.error(e);
new Notice("Error publishing to " + this.settings.githubRepo + ".");
}
await shareOneNote(branchName, githubBranch, publish, this.settings, view.file);
});
});
}
Expand All @@ -120,31 +88,7 @@ export default class MkdocsPublication extends Plugin {
)
) {
if (!checking) {
try {
const { workspace} =
this.app;
const currentFile = workspace.getActiveFile();
const branchName = this.app.vault.getName() + "-" + new Date().toLocaleDateString('en-US').replace(/\//g, '-');
const githubBranch = new GithubBranch(this.settings, octokit);
githubBranch.newBranch(branchName);
const publishSuccess = publish.publish(
currentFile,
true,
branchName
);
if (publishSuccess) {
const update = githubBranch.updateRepository(branchName);
if (update) {
noticeMessage(publish, currentFile, this.settings);
} else {
new Notice("Error publishing to " + this.settings.githubRepo + ".");
}

}
} catch (e) {
console.error(e);
new Notice("Error publishing to github.");
}
shareOneNote(branchName, githubBranch, publish, this.settings, this.app.workspace.getActiveFile());
}
return true;
}
Expand All @@ -159,15 +103,7 @@ export default class MkdocsPublication extends Plugin {
checkCallback: (checking) => {
if (this.settings.autoCleanUp) {
if (!checking) {
try {
new Notice(`Starting cleaning ${this.settings.githubRepo} `)
const branchName = this.app.vault.getName() + "-" + new Date().toLocaleDateString('en-US').replace(/\//g, '-');
githubBranch.newBranch(branchName);
deleteFromGithub(false, this.settings,octokit, branchName, shareFiles);
githubBranch.updateRepository(branchName);
} catch (e) {
console.error(e);
}
deleteUnsharedDeletedNotes(githubBranch, this.settings, octokit, shareFiles, branchName);
}
return true;
}
Expand All @@ -179,63 +115,28 @@ export default class MkdocsPublication extends Plugin {
id: "obs2mk-publish-all",
name: "Share all marked notes",
callback: async () => {
const statusBarItems = this.addStatusBarItem();
try {
const sharedFiles = shareFiles.getSharedFiles();
const statusBar = new ShareStatusBar(
statusBarItems,
sharedFiles.length
);
let errorCount = 0;
if (sharedFiles.length > 0) {
const publishedFiles = sharedFiles.map(
(file) => file.name
);
// octokit list of published files in Source
const publishedFilesText = JSON.stringify(publishedFiles).toString();
const githubBranch = new GithubBranch(this.settings, octokit);
const branchName = this.app.vault.getName() + "-" + new Date().toLocaleDateString('en-US').replace(/\//g, '-');
const statusBarElement = this.addStatusBarItem()
const sharedFiles = shareFiles.getSharedFiles();
await shareAllMarkedNotes(publish, this.settings, octokit, shareFiles, githubBranch, statusBarElement, branchName, sharedFiles);
}
});

this.addCommand({
id: "obs2mk-upload-new",
name: "Share newly note",
// @ts-ignore I really need async here
checkCallback: async (checking) => {
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);
const vaultPublisherJSON = this.settings.folderDefaultName.length>0? `${this.settings.folderDefaultName}/vault_published.json`:`vault_published.json`;
await publish.uploadText(
"vault_published.json",
publishedFilesText,
vaultPublisherJSON,"", branchName
);
for (
let files = 0;
files < sharedFiles.length;
files++
) {
try {
const file = sharedFiles[files];
statusBar.increment();
await publish.publish(file, false, branchName);
} catch {
errorCount++;
new Notice(
`Unable to publish note ${sharedFiles[files].name}, skipping it`
);
}
}
statusBar.finish(8000);
const noticeValue = `${publishedFiles.length - errorCount} notes`
await deleteFromGithub(true, this.settings, octokit, branchName, shareFiles);
const update=await githubBranch.updateRepository(branchName);
if (update) {
await noticeMessage(publish, noticeValue, this.settings)
} else {
new Notice("Error publishing to " + this.settings.githubRepo + ".");

}
}
} catch (e) {
// statusBarItems.remove();
console.error(e);
new Notice(
"Unable to publish multiple notes, something went wrong."
);
}
await shareAllMarkedNotes(publish, this.settings, octokit, shareFiles, githubBranch, statusBarElement, branchName, newlySharedNotes);
} return true;
} return false;
},
});
}
Expand Down
Loading

0 comments on commit 176f586

Please sign in to comment.