Skip to content

Commit

Permalink
run formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
GamerGirlandCo committed Oct 22, 2024
1 parent be4d4ea commit c9d0c5e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 81 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@
"sorted-btree": "^1.8.1",
"sucrase": "3.35.0",
"yaml": "^2.3.3"
}
},
"packageManager": "yarn@1.22.19+sha1.4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447"
}
1 change: 0 additions & 1 deletion src/index/types/json/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ export interface JsonMarkdownListItem {
$symbol?: string;
/** The contents of the list item. May be undefined if the list item could not be parsed. */
$text?: string;

}

export interface JsonMarkdownTaskItem extends JsonMarkdownListItem {
Expand Down
4 changes: 2 additions & 2 deletions src/index/types/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ export class MarkdownListItem implements Indexable, Linkbearing, Taggable, Field
$symbol?: string;
/** The text contents of the list item. */
$text?: string;

/** Create a list item from a serialized object. */
static from(
object: JsonMarkdownListItem,
Expand Down Expand Up @@ -622,7 +622,7 @@ export class MarkdownListItem implements Indexable, Linkbearing, Taggable, Field
get fields() {
return MarkdownListItem.FIELD_DEF(this);
}

/** Fetch a specific field by key. */
public field(key: string) {
return MarkdownListItem.FIELD_DEF(this, key)?.[0] ?? Fieldbearings.getWithDefault(this, key);
Expand Down
153 changes: 76 additions & 77 deletions src/ui/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,102 +5,101 @@ import { DateTime } from "luxon";
import { Vault } from "obsidian";

export function parseDotField(raw: string, obj: any) {
if(obj === null) return obj;
if(raw.contains("."))
return raw.split(".").reduce((tif, c) => {
if (typeof tif == "object")
return tif[c]
else return tif;
}, obj || {})
if(typeof obj === "object" && !Array.isArray(obj)) {
return obj[raw];
}
return obj;
if (obj === null) return obj;
if (raw.contains("."))
return raw.split(".").reduce((tif, c) => {
if (typeof tif == "object") return tif[c];
else return tif;
}, obj || {});
if (typeof obj === "object" && !Array.isArray(obj)) {
return obj[raw];
}
return obj;
}

export function getField(input: Indexable, field: string) {
if(input instanceof MarkdownPage) {
return input.field(field)?.value ?? null
} else if(input instanceof MarkdownSection) {
return input.field(field)?.value ?? null
} else if(input instanceof MarkdownBlock) {
return input.field(field)?.value ?? null
} else if(input instanceof MarkdownListItem) {
return input.field(field)?.value ?? null
} else {
return {}
}
}/** Trim empty ending lines. */
if (input instanceof MarkdownPage) {
return input.field(field)?.value ?? null;
} else if (input instanceof MarkdownSection) {
return input.field(field)?.value ?? null;
} else if (input instanceof MarkdownBlock) {
return input.field(field)?.value ?? null;
} else if (input instanceof MarkdownListItem) {
return input.field(field)?.value ?? null;
} else {
return {};
}
} /** Trim empty ending lines. */
function trimEndingLines(text: string): string {
let parts = text.split(/\r?\n/u);
let trim = parts.length - 1;
while (trim > 0 && parts[trim].trim() == "") trim--;
let parts = text.split(/\r?\n/u);
let trim = parts.length - 1;
while (trim > 0 && parts[trim].trim() == "") trim--;

return parts.join("\n");
return parts.join("\n");
}
/** Set the task completion key on check. */
export function setTaskCompletion(
originalText: string,
useEmojiShorthand: boolean,
completionKey: string,
completionDateFormat: string,
complete: boolean
originalText: string,
useEmojiShorthand: boolean,
completionKey: string,
completionDateFormat: string,
complete: boolean
): string {
const blockIdRegex = /\^[a-z0-9\-]+/i;

if (!complete && !useEmojiShorthand)
return trimEndingLines(setInlineField(originalText.trimEnd(), completionKey)).trimEnd();

let parts = originalText.split(/\r?\n/u);
const matches = blockIdRegex.exec(parts[parts.length - 1]);

let processedPart = parts[parts.length - 1].split(blockIdRegex).join(""); // last part without block id
if (useEmojiShorthand) {
processedPart = setEmojiShorthandCompletionField(
processedPart,
complete ? DateTime.now().toFormat("yyyy-MM-dd") : ""
);
} else {
processedPart = setInlineField(processedPart, completionKey, DateTime.now().toFormat(completionDateFormat));
}
processedPart = `${processedPart.trimEnd()}${matches?.length ? " " + matches[0].trim() : ""}`.trimEnd(); // add back block id
parts[parts.length - 1] = processedPart.trimStart();

return parts.join("\n");
const blockIdRegex = /\^[a-z0-9\-]+/i;

if (!complete && !useEmojiShorthand)
return trimEndingLines(setInlineField(originalText.trimEnd(), completionKey)).trimEnd();

let parts = originalText.split(/\r?\n/u);
const matches = blockIdRegex.exec(parts[parts.length - 1]);

let processedPart = parts[parts.length - 1].split(blockIdRegex).join(""); // last part without block id
if (useEmojiShorthand) {
processedPart = setEmojiShorthandCompletionField(
processedPart,
complete ? DateTime.now().toFormat("yyyy-MM-dd") : ""
);
} else {
processedPart = setInlineField(processedPart, completionKey, DateTime.now().toFormat(completionDateFormat));
}
processedPart = `${processedPart.trimEnd()}${matches?.length ? " " + matches[0].trim() : ""}`.trimEnd(); // add back block id
parts[parts.length - 1] = processedPart.trimStart();

return parts.join("\n");
}

export const LIST_ITEM_REGEX = /^[\s>]*(\d+\.|\d+\)|\*|-|\+)\s*(\[.{0,1}\])?\s*(.*)$/mu;

/** Rewrite a task with the given completion status and new text. */
export async function rewriteTask(vault: Vault, task: MarkdownTaskItem, desiredStatus: string, desiredText?: string) {
if (desiredStatus == task.$status && (desiredText == undefined || desiredText == task.$text)) return;
desiredStatus = desiredStatus == "" ? " " : desiredStatus;
if (desiredStatus == task.$status && (desiredText == undefined || desiredText == task.$text)) return;
desiredStatus = desiredStatus == "" ? " " : desiredStatus;

let rawFiletext = await vault.adapter.read(task.$file);
let hasRN = rawFiletext.contains("\r");
let filetext = rawFiletext.split(/\r?\n/u);
let rawFiletext = await vault.adapter.read(task.$file);
let hasRN = rawFiletext.contains("\r");
let filetext = rawFiletext.split(/\r?\n/u);

if (filetext.length < task.$line) return;
let match = LIST_ITEM_REGEX.exec(filetext[task.$line]);
if (!match || match[2].length == 0) return;
if (filetext.length < task.$line) return;
let match = LIST_ITEM_REGEX.exec(filetext[task.$line]);
if (!match || match[2].length == 0) return;

let taskTextParts = task.$text!.split("\n");
// if (taskTextParts[0].trim() != match[3].trim()) return;
let taskTextParts = task.$text!.split("\n");
// if (taskTextParts[0].trim() != match[3].trim()) return;

// We have a positive match here at this point, so go ahead and do the rewrite of the status.
let initialSpacing = /^[\s>]*/u.exec(filetext[task.$line])!![0];
if (desiredText) {
let desiredParts = desiredText.split("\n");
// We have a positive match here at this point, so go ahead and do the rewrite of the status.
let initialSpacing = /^[\s>]*/u.exec(filetext[task.$line])!![0];
if (desiredText) {
let desiredParts = desiredText.split("\n");

let newTextLines: string[] = [`${initialSpacing}${task.$symbol} [${desiredStatus}] ${desiredParts[0]}`].concat(
desiredParts.slice(1).map(l => initialSpacing + "\t" + l.trimStart())
);
let newTextLines: string[] = [`${initialSpacing}${task.$symbol} [${desiredStatus}] ${desiredParts[0]}`].concat(
desiredParts.slice(1).map((l) => initialSpacing + "\t" + l.trimStart())
);

filetext.splice(task.$line, task.$text!.split("\n").length, ...newTextLines);
} else {
filetext[task.$line] = `${initialSpacing}${task.$symbol} [${desiredStatus}] ${taskTextParts[0].trim()}`;
}
filetext.splice(task.$line, task.$text!.split("\n").length, ...newTextLines);
} else {
filetext[task.$line] = `${initialSpacing}${task.$symbol} [${desiredStatus}] ${taskTextParts[0].trim()}`;
}

let newText = filetext.join(hasRN ? "\r\n" : "\n");
await vault.adapter.write(task.$file, newText);
}
let newText = filetext.join(hasRN ? "\r\n" : "\n");
await vault.adapter.write(task.$file, newText);
}

0 comments on commit c9d0c5e

Please sign in to comment.