Skip to content

Commit

Permalink
- Updated: refactored, formatted code, added code review suggestions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesb committed Apr 28, 2021
1 parent 7931037 commit 44004c5
Show file tree
Hide file tree
Showing 31 changed files with 1,675 additions and 1,585 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ main.js

# Comment one of the following lock file in your plugin!
pnpm-lock.yaml
npm-lock.yaml
npm-lock.yaml
13 changes: 13 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
dist/

# npm
node_modules
package-lock.json

# Outputs
main.js
*.js.map

# Comment one of the following lock file in your plugin!
pnpm-lock.yaml
npm-lock.yaml
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
13 changes: 12 additions & 1 deletion data.json
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
{"vimMode":false,"defaultPriority":30,"queueFolderPath":"IW-Queues","queueFilePath":"queue.md","defaultAFactor":2,"defaultInterval":1,"defaultQueueType":"simple","defaultPriorityMin":35,"defaultPriorityMax":90,"skipAddNoteWindow":true}
{
"vimMode": false,
"defaultPriority": 30,
"queueFolderPath": "IW-Queues",
"queueFilePath": "queue.md",
"defaultAFactor": 2,
"defaultInterval": 1,
"defaultQueueType": "afactor",
"defaultPriorityMin": 27,
"defaultPriorityMax": 85,
"skipAddNoteWindow": true
}
20 changes: 10 additions & 10 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"id": "obsidian-incremental-writing",
"name": "Incremental Writing",
"description": "Incrementally review notes and blocks over time.",
"isDesktopOnly": true,
"version": "0.3.4",
"author": "Experimental Learning",
"authorUrl": "https://github.com/bjsi/incremental-writing",
"js": "main.js"
}
{
"id": "obsidian-incremental-writing",
"name": "Incremental Writing",
"description": "Incrementally review notes and blocks over time.",
"isDesktopOnly": true,
"version": "0.3.4",
"author": "Experimental Learning",
"authorUrl": "https://github.com/bjsi/incremental-writing",
"js": "main.js"
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@rollup/plugin-typescript": "8.1.1",
"@types/node": "14.14.25",
"obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master",
"prettier": "2.2.1",
"rollup": "2.38.5",
"tslib": "2.1.0",
"typescript": "4.1.3"
Expand Down
30 changes: 15 additions & 15 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import typescript from "@rollup/plugin-typescript";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";

export default {
input: "./src/main.ts",
output: {
dir: ".",
sourcemap: "inline",
format: "cjs",
exports: "default",
},
external: ["obsidian"],
plugins: [typescript(), nodeResolve({ browser: true }), commonjs()],
};
import typescript from "@rollup/plugin-typescript";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";

export default {
input: "./src/main.ts",
output: {
dir: ".",
sourcemap: "inline",
format: "cjs",
exports: "default",
},
external: ["obsidian"],
plugins: [typescript(), nodeResolve({ browser: true }), commonjs()],
};
27 changes: 0 additions & 27 deletions src/hashset.ts

This file was deleted.

69 changes: 36 additions & 33 deletions src/helpers/block-utils.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
import { ObsidianUtilsBase } from "./obsidian-utils-base"
import { App, TFile } from "obsidian"
import { ObsidianUtilsBase } from "./obsidian-utils-base";
import { App, TFile } from "obsidian";

export class BlockUtils extends ObsidianUtilsBase {
constructor(app: App) {
super(app);
}

constructor (app: App){
super(app);
}

// TODO: Rewrite
getBlock(inputLine: string, noteFile: TFile): string { //Returns the string of a block ID if block is found, or "" if not.
let noteBlocks = this.app.metadataCache.getFileCache(noteFile).blocks;
console.log("Checking if line '" + inputLine + "' is a block.");
let blockString = "";
if (noteBlocks) { // the file does contain blocks. If not, return ""
for (let eachBlock in noteBlocks) { // iterate through the blocks.
console.log("Checking block ^" + eachBlock);
let blockRegExp = new RegExp("(" + eachBlock + ")$", "gim");
if (inputLine.match(blockRegExp)) { // if end of inputLine matches block, return it
blockString = eachBlock;
console.log("Found block ^" + blockString);
return blockString;
}
}
return blockString;
}
return blockString;
// TODO: Rewrite
getBlock(inputLine: string, noteFile: TFile): string {
//Returns the string of a block ID if block is found, or "" if not.
let noteBlocks = this.app.metadataCache.getFileCache(noteFile).blocks;
console.log("Checking if line '" + inputLine + "' is a block.");
let blockString = "";
if (noteBlocks) {
// the file does contain blocks. If not, return ""
for (let eachBlock in noteBlocks) {
// iterate through the blocks.
console.log("Checking block ^" + eachBlock);
let blockRegExp = new RegExp("(" + eachBlock + ")$", "gim");
if (inputLine.match(blockRegExp)) {
// if end of inputLine matches block, return it
blockString = eachBlock;
console.log("Found block ^" + blockString);
return blockString;
}
}
return blockString;
}
return blockString;
}

createBlockHash(): string {
// Credit to https://stackoverflow.com/a/1349426
let result = '';
var characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < 7; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
createBlockHash(): string {
// Credit to https://stackoverflow.com/a/1349426
let result = "";
var characters = "abcdefghijklmnopqrstuvwxyz0123456789";
var charactersLength = characters.length;
for (var i = 0; i < 7; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
}
45 changes: 21 additions & 24 deletions src/helpers/date-utils.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
export class DateUtils {
static addDays(date: Date, days: number) {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}
static addDays(date: Date, days: number) {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}

static formatDate(d: Date) {

var month = '' + (d.getMonth() + 1);
var day = '' + d.getDate();
var year = d.getFullYear();
static formatDate(d: Date) {
var month = "" + (d.getMonth() + 1);
var day = "" + d.getDate();
var year = d.getFullYear();

if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
if (month.length < 2) month = "0" + month;
if (day.length < 2) day = "0" + day;

return [year, month, day].join('-');
}
return [year, month, day].join("-");
}

static isValid(date: Date) {
return (date instanceof Date && !isNaN(date.valueOf()))
}
static isValid(date: Date) {
return date instanceof Date && !isNaN(date.valueOf());
}

static dateDifference(date1: Date, date2: Date) {
const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
// @ts-ignore
return Math.round(Math.abs((date1 - date2) / oneDay));
}
static dateDifference(date1: Date, date2: Date) {
const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
// @ts-ignore
return Math.round(Math.abs((date1 - date2) / oneDay));
}
}
111 changes: 58 additions & 53 deletions src/helpers/file-utils.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,63 @@
import { normalizePath, MarkdownView, App, TFile, TFolder } from "obsidian"
import { ObsidianUtilsBase } from "./obsidian-utils-base"
import { normalizePath, MarkdownView, App, TFile, TFolder } from "obsidian";
import { ObsidianUtilsBase } from "./obsidian-utils-base";

// TODO: read: https://github.com/lynchjames/obsidian-day-planner/blob/d1eb7ce187e7757b7a3880358a6ee184b3b025da/src/file.ts#L48

export class FileUtils extends ObsidianUtilsBase {

constructor(app: App) {
super(app);
}

async createIfNotExists(file: string, data: string) {
const normalizedPath = normalizePath(file);
if (!await this.app.vault.adapter.exists(normalizedPath)) {
let folderPath = this.getParentOfNormalized(normalizedPath);
await this.createFolders(folderPath);
await this.app.vault.create(normalizedPath, data);
}
}

toLinkText(file: TFile) {
return this.app.metadataCache.fileToLinktext(file, file.path, true);
}

getParentOfNormalized(normalizedPath: string) {
let pathSplit = normalizedPath.split('/');
return pathSplit.slice(0, pathSplit.length - 1).join('/');
}

async createFolders(normalizedPath: string) {
let current = normalizedPath;
while (current && !await this.app.vault.adapter.exists(current)) {
await this.app.vault.createFolder(current);
current = this.getParentOfNormalized(current);
}
}

isDescendantOf(file: TFile, folder: TFolder): boolean {
let ancestor = file.parent;
while (ancestor && !ancestor.isRoot()) {
if (ancestor === folder) {
return true;
}
ancestor = ancestor.parent;
}
return false;
}

async goTo(filePath: string, newLeaf: boolean) {
let file = this.app.vault.getAbstractFileByPath(filePath) as TFile;
let link = this.app.metadataCache.fileToLinktext(file, "");
await this.app.workspace.openLinkText(link, "", newLeaf);
}

getActiveNoteFile() {
return (this.app.workspace.activeLeaf.view as MarkdownView).file;
}
constructor(app: App) {
super(app);
}

async createIfNotExists(file: string, data: string) {
const normalizedPath = normalizePath(file);
if (!(await this.app.vault.adapter.exists(normalizedPath))) {
let folderPath = this.getParentOfNormalized(normalizedPath);
await this.createFolders(folderPath);
await this.app.vault.create(normalizedPath, data);
}
}

getTFile(filePath: string) {
let file = this.app.vault.getAbstractFileByPath(filePath);
if (file instanceof TFile) return file;
return null;
}

toLinkText(file: TFile) {
return this.app.metadataCache.fileToLinktext(file, "", true);
}

getParentOfNormalized(normalizedPath: string) {
let pathSplit = normalizedPath.split("/");
return pathSplit.slice(0, pathSplit.length - 1).join("/");
}

async createFolders(normalizedPath: string) {
let current = normalizedPath;
while (current && !(await this.app.vault.adapter.exists(current))) {
await this.app.vault.createFolder(current);
current = this.getParentOfNormalized(current);
}
}

isDescendantOf(file: TFile, folder: TFolder): boolean {
let ancestor = file.parent;
while (ancestor && !ancestor.isRoot()) {
if (ancestor === folder) {
return true;
}
ancestor = ancestor.parent;
}
return false;
}

async goTo(filePath: string, newLeaf: boolean) {
let file = this.app.vault.getAbstractFileByPath(filePath) as TFile;
let link = this.app.metadataCache.fileToLinktext(file, "");
await this.app.workspace.openLinkText(link, "", newLeaf);
}

getActiveNoteFile() {
return this.app.workspace.getActiveViewOfType(MarkdownView)?.file;
}
}
Loading

0 comments on commit 44004c5

Please sign in to comment.