Skip to content

Commit

Permalink
fix(yaml): prevent empty and wrong yaml
Browse files Browse the repository at this point in the history
Also fixes error with token (related to #200)
  • Loading branch information
Mara-Li authored Jan 26, 2024
1 parent f7fbc36 commit 2d4c1c3
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 177 deletions.
4 changes: 2 additions & 2 deletions src/GitHub/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Octokit } from "@octokit/core";
import i18next from "i18next";
import {Notice } from "obsidian";

import GithubPublisherPlugin from "../main";
import GithubPublisher from "../main";
import {
RepoFrontmatter,
} from "../settings/interface";
Expand All @@ -14,7 +14,7 @@ export class GithubBranch extends FilesManagement {

constructor(
octokit: Octokit,
plugin: GithubPublisherPlugin,
plugin: GithubPublisher,
) {
super(octokit, plugin);
}
Expand Down
16 changes: 8 additions & 8 deletions src/GitHub/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
getImagePath,
getReceiptFolder,
} from "../conversion/file_path";
import GithubPublisherPlugin from "../main";
import GithubPublisher from "../main";
import {
Deleted,
GitHubPublisherSettings,
Expand Down Expand Up @@ -51,7 +51,7 @@ import { FilesManagement } from "./files";

export default class Publisher {
octokit: Octokit;
plugin: GithubPublisherPlugin;
plugin: GithubPublisher;
vault: Vault;
metadataCache: MetadataCache;
settings: GitHubPublisherSettings;
Expand All @@ -65,7 +65,7 @@ export default class Publisher {

constructor(
octokit: Octokit,
plugin: GithubPublisherPlugin,
plugin: GithubPublisher,
) {
this.vault = plugin.app.vault;
this.metadataCache = plugin.app.metadataCache;
Expand Down Expand Up @@ -214,15 +214,15 @@ export default class Publisher {

let text = await this.vault.cachedRead(file);
const multiProperties: MultiProperties = {
settings: this.settings,
plugin: this.plugin,
frontmatter: {
general: frontmatterSettings,
repo: repo.frontmatter,
},
repository: repo.repo,
filepath: getReceiptFolder(file, repo.repo, this.plugin, repo.frontmatter),
};
text = await mainConverting(text, file, this.plugin.app, frontmatter, linkedFiles, this.plugin, multiProperties);
text = await mainConverting(text, file, frontmatter, linkedFiles, multiProperties);
const path = multiProperties.filepath;
const repoFrontmatter = Array.isArray(repo.frontmatter)
? repo.frontmatter
Expand All @@ -238,7 +238,7 @@ export default class Publisher {
const fileError: string[] = [];
for (const repo of repoFrontmatter) {
const monoProperties: MonoProperties = {
settings: this.settings,
plugin: this.plugin,
frontmatter: {
general: frontmatterSettings,
repo
Expand Down Expand Up @@ -465,12 +465,12 @@ export default class Publisher {
isUpdated: needToByUpdated,
file: imageFile.name
};
}
}
const folder = dryRunPath.split("/").slice(0, -1).join("/");
const folderExists = this.vault.getAbstractFileByPath(folder);
if (!folderExists || !(folderExists instanceof TFolder))
await this.vault.createFolder(folder);
await this.vault.createBinary(path, imageBin);
await this.vault.createBinary(dryRunPath, imageBin);
return {
isUpdated: true,
file: imageFile.name
Expand Down
32 changes: 16 additions & 16 deletions src/commands/suggest_other_repo_commands_modal.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import i18next from "i18next";
import {App, FuzzySuggestModal } from "obsidian";
import { App, FuzzySuggestModal } from "obsidian";
import { defaultRepo } from "src/utils/data_validation_test";

import GithubPublisherPlugin from "../main";
import {FolderSettings, Repository} from "../settings/interface";
import {logs} from "../utils";
import GithubPublisher from "../main";
import { FolderSettings, Repository } from "../settings/interface";
import { logs } from "../utils";
import {
createLinkOnActiveFile,
deleteCommands, repositoryValidityActiveFile, shareActiveFile,
Expand All @@ -23,15 +23,15 @@ interface GithubPublisherCommands {
* @extends FuzzySuggestModal
* @category Command
* @category SuggestModal
* @category GithubPublisherPlugin
* @category GithubPublisher
* @description This class is used to choose which repo to run the command on
*/

export class ChooseWhichRepoToRun extends FuzzySuggestModal<Repository> {
plugin: GithubPublisherPlugin;
plugin: GithubPublisher;
branchName: string;

constructor(app: App, plugin: GithubPublisherPlugin, branchName: string) {
constructor(app: App, plugin: GithubPublisher, branchName: string) {
super(app);
this.plugin = plugin;
this.branchName = branchName;
Expand All @@ -53,14 +53,14 @@ export class ChooseWhichRepoToRun extends FuzzySuggestModal<Repository> {
* Just return the repo data
*/
export class ChooseRepoToRun extends FuzzySuggestModal<Repository> {
plugin: GithubPublisherPlugin;
plugin: GithubPublisher;
branchName: string;
keyToFind: string | null;
type: "folder" | "file";
fileName: string | null;
onSubmit: (item: Repository) => void;

constructor(app: App, plugin: GithubPublisherPlugin, keyToFind: null|string = null, branchName: string, type:"folder"|"file", fileName: string | null, onSubmit: (item: Repository) => void) {
constructor(app: App, plugin: GithubPublisher, keyToFind: null | string = null, branchName: string, type: "folder" | "file", fileName: string | null, onSubmit: (item: Repository) => void) {
super(app);
this.plugin = plugin;
this.branchName = branchName;
Expand All @@ -78,15 +78,15 @@ export class ChooseRepoToRun extends FuzzySuggestModal<Repository> {
repoFound.push(defRepo);
}
if (this.keyToFind) {
repoFound=repoFound.concat(this.plugin.settings.github.otherRepo.filter((repo: Repository) => repo.shareKey == this.keyToFind));
repoFound = repoFound.concat(this.plugin.settings.github.otherRepo.filter((repo: Repository) => repo.shareKey == this.keyToFind));
if (this.keyToFind === defRepo.shareKey) {
repoFound.push(defRepo);
}
}
}
repoFound=repoFound.concat(this.plugin.settings.github.otherRepo.filter((repo: Repository) => repo.shareAll?.enable && !this.fileName?.startsWith(repo.shareAll?.excludedFileName)));
repoFound = repoFound.concat(this.plugin.settings.github.otherRepo.filter((repo: Repository) => repo.shareAll?.enable && !this.fileName?.startsWith(repo.shareAll?.excludedFileName)));
repoFound.push(defRepo);
repoFound=[...new Set(repoFound)];
repoFound = [...new Set(repoFound)];
if (repoFound.length === 0)
return this.plugin.settings.github.otherRepo;
return repoFound;
Expand All @@ -106,17 +106,17 @@ export class ChooseRepoToRun extends FuzzySuggestModal<Repository> {
*/

export class SuggestOtherRepoCommandsModal extends FuzzySuggestModal<GithubPublisherCommands> {
plugin: GithubPublisherPlugin;
plugin: GithubPublisher;
branchName: string;
repo: Repository;
constructor(app: App, plugin: GithubPublisherPlugin, branchName: string, repo: Repository) {
constructor(app: App, plugin: GithubPublisher, branchName: string, repo: Repository) {
super(app);
this.plugin = plugin;
this.branchName = branchName;
this.repo = repo;
}
getItems(): GithubPublisherCommands[] {
const cmd = [
const cmd = [
{
commands: "shareAllMarkedNotes",
name: i18next.t("commands.uploadAllNotes")
Expand Down Expand Up @@ -188,7 +188,7 @@ export class SuggestOtherRepoCommandsModal extends FuzzySuggestModal<GithubPubli
repositoryValidityActiveFile(this.plugin, this.branchName, this.repo);
break;
}
logs({settings: this.plugin.settings}, `run command ${item.commands}`);
logs({ settings: this.plugin.settings }, `run command ${item.commands}`);
}
}

39 changes: 20 additions & 19 deletions src/conversion/compiler/dataview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
*/

import i18next from "i18next";
import { Component, FrontMatterCache, htmlToMarkdown,TFile, Vault } from "obsidian";
import { Component, FrontMatterCache, htmlToMarkdown,TFile } from "obsidian";
import { getAPI, isPluginEnabled,Literal, Success } from "obsidian-dataview";
import GithubPublisher from "src/main";
import { FrontmatterConvert, GitHubPublisherSettings, LinkedNotes, MultiProperties } from "src/settings/interface";
import { FrontmatterConvert, LinkedNotes, MultiProperties } from "src/settings/interface";
import { logs, notif } from "src/utils";

import { convertToInternalGithub, convertWikilinks, escapeRegex } from "../links";
Expand All @@ -31,11 +31,12 @@ import { convertToInternalGithub, convertWikilinks, escapeRegex } from "../links
export async function convertDataviewQueries(
text: string,
path: string,
plugin: GithubPublisher,
frontmatter: FrontMatterCache | undefined | null,
sourceFile: TFile,
properties: MultiProperties,
): Promise<string> {
const plugin = properties.plugin;
const settings = plugin.settings;
let replacedText = text;
const dataViewRegex = /```dataview\s(.+?)```/gsm;
//@ts-ignore
Expand All @@ -57,7 +58,7 @@ export async function convertDataviewQueries(
const inlineJsDataViewRegex = new RegExp(`\`${escapeRegex(inlineJsQueryPrefix)}(.+?)\``, "gsm");
const inlineJsMatches = text.matchAll(inlineJsDataViewRegex);
if (!matches && !inlineMatches && !dataviewJsMatches && !inlineJsMatches) {
logs({ settings: properties.settings }, "No dataview queries found");
logs({ settings }, "No dataview queries found");
return replacedText;
}
const error = i18next.t("error.dataview");
Expand All @@ -78,8 +79,8 @@ export async function convertDataviewQueries(
}
replacedText = replacedText.replace(block, markdown);
} catch (e) {
logs({ settings: properties.settings, e: true }, e);
notif({ settings: properties.settings }, error);
logs({ settings, e: true }, e);
notif({ settings }, error);
return queryBlock[0];
}
}
Expand All @@ -101,8 +102,8 @@ export async function convertDataviewQueries(
const markdown = removeDataviewQueries(div.innerHTML, properties.frontmatter.general);
replacedText = replacedText.replace(block, markdown);
} catch (e) {
logs({ settings: properties.settings, e: true }, e);
notif({ settings: properties.settings }, error);
logs({ settings, e: true }, e);
notif({ settings }, error);
return queryBlock[0];
}
}
Expand All @@ -127,8 +128,8 @@ export async function convertDataviewQueries(
replacedText = replacedText.replace(code, removeDataviewQueries(dvApi.settings.renderNullAs, properties.frontmatter.general));
}
} catch (e) {
logs({ settings: properties.settings, e: true }, e);
notif({ settings: properties.settings }, error);
logs({ settings, e: true }, e);
notif({ settings }, error);
return inlineQuery[0];
}
}
Expand Down Expand Up @@ -159,12 +160,12 @@ export async function convertDataviewQueries(
const markdown = removeDataviewQueries(htmlToMarkdown(div.innerHTML), properties.frontmatter.general);
replacedText = replacedText.replace(code, markdown);
} catch (e) {
logs({ settings: properties.settings, e: true }, e);
notif({ settings: properties.settings }, error);
logs({ settings, e: true }, e);
notif({ settings }, error);
return inlineJsQuery[0];
}
}
return await convertDataviewLinks(replacedText, frontmatter, sourceFile, plugin, properties);
return await convertDataviewLinks(replacedText, frontmatter, sourceFile, properties);
}

/**
Expand All @@ -191,22 +192,21 @@ async function convertDataviewLinks(
md: string,
frontmatter: FrontMatterCache | undefined | null,
sourceFile: TFile,
plugin: GithubPublisher,
properties: MultiProperties): Promise<string> {
const dataviewPath = getDataviewPath(md, properties.settings, app.vault);

const dataviewPath = getDataviewPath(md, properties.plugin);
md = await convertToInternalGithub(
md,
dataviewPath,
sourceFile,
plugin,
frontmatter,
properties
);
return convertWikilinks(
md,
properties.frontmatter.general,
dataviewPath,
properties.settings,
properties.plugin.settings,
frontmatter
);
}
Expand All @@ -221,9 +221,10 @@ async function convertDataviewLinks(

export function getDataviewPath(
markdown: string,
settings: GitHubPublisherSettings,
vault: Vault
plugin: GithubPublisher
): LinkedNotes[] {
const { settings} = plugin;
const vault = plugin.app.vault;
if (!settings.conversion.dataview) {
return [];
}
Expand Down
Loading

0 comments on commit 2d4c1c3

Please sign in to comment.