Skip to content

Commit

Permalink
Fix bug in login functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Hananoshika Yomaru committed Nov 22, 2023
1 parent 9cdc72d commit 626eff1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from "./utils/obsidian";
import { shouldIgnoreFile } from "./utils/shouldIgnoreFile";
import { getNewTextFromFile } from "./utils/getNewTextFromFile";
import { isValidFrontmatter } from "@/utils/yaml";

const userClickTimeout = 5000;

Expand Down Expand Up @@ -143,8 +144,7 @@ export default class FrontmatterGeneratorPlugin extends Plugin {
runFileSync(file: TFile, editor: Editor) {
const data = getDataFromTextSync(editor.getValue());
if (shouldIgnoreFile(this.settings, file, data)) return;
const invalidFrontmatter = data.text && !data.yamlText && !data.body;
if (invalidFrontmatter) return;
if (!isValidFrontmatter(data)) return;
const newText = getNewTextFromFile(
this.settings.template,
file,
Expand All @@ -163,8 +163,7 @@ export default class FrontmatterGeneratorPlugin extends Plugin {

const data = await getDataFromFile(this, file);
if (shouldIgnoreFile(this.settings, file, data)) return;
const invalidFrontmatter = data.text && !data.yamlText && !data.body;
if (invalidFrontmatter) return;
if (!isValidFrontmatter(data)) return;
// from the frontmatter template and the file, generate some new properties
const newText = getNewTextFromFile(
this.settings.template,
Expand Down Expand Up @@ -223,6 +222,7 @@ export default class FrontmatterGeneratorPlugin extends Plugin {

this.registerEvent(
this.app.vault.on("modify", async (file) => {
console.log("modify", file);
if (!this.settings.runOnModify) return;
if (this.lock) return;
try {
Expand Down
8 changes: 8 additions & 0 deletions src/utils/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,11 @@ export function isNumeric(str: string) {
export function stripCr(text: string) {
return text.replace(/\r/g, "");
}

export function trimFirstSpaces(input: string) {
// Get the index of the last space of the leading space group
const leadingSpaces = input.match(/^[ \t]+/);
const start = leadingSpaces ? leadingSpaces[0].length : 0;
// Replace start and end spaces using the indices
return input.substring(start);
}
22 changes: 21 additions & 1 deletion src/utils/yaml.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { load, dump } from "js-yaml";
import { escapeDollarSigns, yamlRegex } from "./regex";
import { isNumeric } from "./strings";
import { isNumeric, trimFirstSpaces } from "./strings";
import { Data } from "@/utils/obsidian";

export const OBSIDIAN_TAG_KEY_SINGULAR = "tag";
export const OBSIDIAN_TAG_KEY_PLURAL = "tags";
Expand Down Expand Up @@ -501,3 +502,22 @@ export const splitYamlAndBody = (markdown: string) => {
body: parts.slice(2).join("---"),
};
};

export const isValidFrontmatter = (data: Data) => {
const { yamlText, text } = data;

if (!yamlText) {
const textAfterTrimFirstSpace = trimFirstSpaces(text);
// try to get new yaml text
const yamlText = getYAMLText(textAfterTrimFirstSpace);
if (yamlText) {
// console.log(
// "frontmatter is not valid because there is a space before the yaml"
// );
return false;
}
}
if (data.text && !data.yamlText && !data.body) return false;

return true;
};

0 comments on commit 626eff1

Please sign in to comment.