Skip to content

Commit 44004c5

Browse files
author
jamesb
committed
- Updated: refactored, formatted code, added code review suggestions.
1 parent 7931037 commit 44004c5

31 files changed

+1675
-1585
lines changed

Diff for: .gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ main.js
1010

1111
# Comment one of the following lock file in your plugin!
1212
pnpm-lock.yaml
13-
npm-lock.yaml
13+
npm-lock.yaml

Diff for: .prettierignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
dist/
2+
3+
# npm
4+
node_modules
5+
package-lock.json
6+
7+
# Outputs
8+
main.js
9+
*.js.map
10+
11+
# Comment one of the following lock file in your plugin!
12+
pnpm-lock.yaml
13+
npm-lock.yaml

Diff for: .prettierrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

Diff for: data.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
{"vimMode":false,"defaultPriority":30,"queueFolderPath":"IW-Queues","queueFilePath":"queue.md","defaultAFactor":2,"defaultInterval":1,"defaultQueueType":"simple","defaultPriorityMin":35,"defaultPriorityMax":90,"skipAddNoteWindow":true}
1+
{
2+
"vimMode": false,
3+
"defaultPriority": 30,
4+
"queueFolderPath": "IW-Queues",
5+
"queueFilePath": "queue.md",
6+
"defaultAFactor": 2,
7+
"defaultInterval": 1,
8+
"defaultQueueType": "afactor",
9+
"defaultPriorityMin": 27,
10+
"defaultPriorityMax": 85,
11+
"skipAddNoteWindow": true
12+
}

Diff for: manifest.json

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
{
2-
"id": "obsidian-incremental-writing",
3-
"name": "Incremental Writing",
4-
"description": "Incrementally review notes and blocks over time.",
5-
"isDesktopOnly": true,
6-
"version": "0.3.4",
7-
"author": "Experimental Learning",
8-
"authorUrl": "https://github.com/bjsi/incremental-writing",
9-
"js": "main.js"
10-
}
1+
{
2+
"id": "obsidian-incremental-writing",
3+
"name": "Incremental Writing",
4+
"description": "Incrementally review notes and blocks over time.",
5+
"isDesktopOnly": true,
6+
"version": "0.3.4",
7+
"author": "Experimental Learning",
8+
"authorUrl": "https://github.com/bjsi/incremental-writing",
9+
"js": "main.js"
10+
}

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@rollup/plugin-typescript": "8.1.1",
2020
"@types/node": "14.14.25",
2121
"obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master",
22+
"prettier": "2.2.1",
2223
"rollup": "2.38.5",
2324
"tslib": "2.1.0",
2425
"typescript": "4.1.3"

Diff for: rollup.config.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import typescript from "@rollup/plugin-typescript";
2-
import { nodeResolve } from "@rollup/plugin-node-resolve";
3-
import commonjs from "@rollup/plugin-commonjs";
4-
5-
export default {
6-
input: "./src/main.ts",
7-
output: {
8-
dir: ".",
9-
sourcemap: "inline",
10-
format: "cjs",
11-
exports: "default",
12-
},
13-
external: ["obsidian"],
14-
plugins: [typescript(), nodeResolve({ browser: true }), commonjs()],
15-
};
1+
import typescript from "@rollup/plugin-typescript";
2+
import { nodeResolve } from "@rollup/plugin-node-resolve";
3+
import commonjs from "@rollup/plugin-commonjs";
4+
5+
export default {
6+
input: "./src/main.ts",
7+
output: {
8+
dir: ".",
9+
sourcemap: "inline",
10+
format: "cjs",
11+
exports: "default",
12+
},
13+
external: ["obsidian"],
14+
plugins: [typescript(), nodeResolve({ browser: true }), commonjs()],
15+
};

Diff for: src/hashset.ts

-27
This file was deleted.

Diff for: src/helpers/block-utils.ts

+36-33
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,43 @@
1-
import { ObsidianUtilsBase } from "./obsidian-utils-base"
2-
import { App, TFile } from "obsidian"
1+
import { ObsidianUtilsBase } from "./obsidian-utils-base";
2+
import { App, TFile } from "obsidian";
33

44
export class BlockUtils extends ObsidianUtilsBase {
5+
constructor(app: App) {
6+
super(app);
7+
}
58

6-
constructor (app: App){
7-
super(app);
8-
}
9-
10-
// TODO: Rewrite
11-
getBlock(inputLine: string, noteFile: TFile): string { //Returns the string of a block ID if block is found, or "" if not.
12-
let noteBlocks = this.app.metadataCache.getFileCache(noteFile).blocks;
13-
console.log("Checking if line '" + inputLine + "' is a block.");
14-
let blockString = "";
15-
if (noteBlocks) { // the file does contain blocks. If not, return ""
16-
for (let eachBlock in noteBlocks) { // iterate through the blocks.
17-
console.log("Checking block ^" + eachBlock);
18-
let blockRegExp = new RegExp("(" + eachBlock + ")$", "gim");
19-
if (inputLine.match(blockRegExp)) { // if end of inputLine matches block, return it
20-
blockString = eachBlock;
21-
console.log("Found block ^" + blockString);
22-
return blockString;
23-
}
24-
}
25-
return blockString;
26-
}
27-
return blockString;
9+
// TODO: Rewrite
10+
getBlock(inputLine: string, noteFile: TFile): string {
11+
//Returns the string of a block ID if block is found, or "" if not.
12+
let noteBlocks = this.app.metadataCache.getFileCache(noteFile).blocks;
13+
console.log("Checking if line '" + inputLine + "' is a block.");
14+
let blockString = "";
15+
if (noteBlocks) {
16+
// the file does contain blocks. If not, return ""
17+
for (let eachBlock in noteBlocks) {
18+
// iterate through the blocks.
19+
console.log("Checking block ^" + eachBlock);
20+
let blockRegExp = new RegExp("(" + eachBlock + ")$", "gim");
21+
if (inputLine.match(blockRegExp)) {
22+
// if end of inputLine matches block, return it
23+
blockString = eachBlock;
24+
console.log("Found block ^" + blockString);
25+
return blockString;
26+
}
27+
}
28+
return blockString;
2829
}
30+
return blockString;
31+
}
2932

30-
createBlockHash(): string {
31-
// Credit to https://stackoverflow.com/a/1349426
32-
let result = '';
33-
var characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
34-
var charactersLength = characters.length;
35-
for ( var i = 0; i < 7; i++ ) {
36-
result += characters.charAt(Math.floor(Math.random() * charactersLength));
37-
}
38-
return result;
33+
createBlockHash(): string {
34+
// Credit to https://stackoverflow.com/a/1349426
35+
let result = "";
36+
var characters = "abcdefghijklmnopqrstuvwxyz0123456789";
37+
var charactersLength = characters.length;
38+
for (var i = 0; i < 7; i++) {
39+
result += characters.charAt(Math.floor(Math.random() * charactersLength));
3940
}
41+
return result;
42+
}
4043
}

Diff for: src/helpers/date-utils.ts

+21-24
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
export class DateUtils {
2-
static addDays(date: Date, days: number) {
3-
var result = new Date(date);
4-
result.setDate(result.getDate() + days);
5-
return result;
6-
}
2+
static addDays(date: Date, days: number) {
3+
var result = new Date(date);
4+
result.setDate(result.getDate() + days);
5+
return result;
6+
}
77

8-
static formatDate(d: Date) {
9-
10-
var month = '' + (d.getMonth() + 1);
11-
var day = '' + d.getDate();
12-
var year = d.getFullYear();
8+
static formatDate(d: Date) {
9+
var month = "" + (d.getMonth() + 1);
10+
var day = "" + d.getDate();
11+
var year = d.getFullYear();
1312

14-
if (month.length < 2)
15-
month = '0' + month;
16-
if (day.length < 2)
17-
day = '0' + day;
13+
if (month.length < 2) month = "0" + month;
14+
if (day.length < 2) day = "0" + day;
1815

19-
return [year, month, day].join('-');
20-
}
16+
return [year, month, day].join("-");
17+
}
2118

22-
static isValid(date: Date) {
23-
return (date instanceof Date && !isNaN(date.valueOf()))
24-
}
19+
static isValid(date: Date) {
20+
return date instanceof Date && !isNaN(date.valueOf());
21+
}
2522

26-
static dateDifference(date1: Date, date2: Date) {
27-
const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
28-
// @ts-ignore
29-
return Math.round(Math.abs((date1 - date2) / oneDay));
30-
}
23+
static dateDifference(date1: Date, date2: Date) {
24+
const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
25+
// @ts-ignore
26+
return Math.round(Math.abs((date1 - date2) / oneDay));
27+
}
3128
}

Diff for: src/helpers/file-utils.ts

+58-53
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,63 @@
1-
import { normalizePath, MarkdownView, App, TFile, TFolder } from "obsidian"
2-
import { ObsidianUtilsBase } from "./obsidian-utils-base"
1+
import { normalizePath, MarkdownView, App, TFile, TFolder } from "obsidian";
2+
import { ObsidianUtilsBase } from "./obsidian-utils-base";
33

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

66
export class FileUtils extends ObsidianUtilsBase {
7-
8-
constructor(app: App) {
9-
super(app);
10-
}
11-
12-
async createIfNotExists(file: string, data: string) {
13-
const normalizedPath = normalizePath(file);
14-
if (!await this.app.vault.adapter.exists(normalizedPath)) {
15-
let folderPath = this.getParentOfNormalized(normalizedPath);
16-
await this.createFolders(folderPath);
17-
await this.app.vault.create(normalizedPath, data);
18-
}
19-
}
20-
21-
toLinkText(file: TFile) {
22-
return this.app.metadataCache.fileToLinktext(file, file.path, true);
23-
}
24-
25-
getParentOfNormalized(normalizedPath: string) {
26-
let pathSplit = normalizedPath.split('/');
27-
return pathSplit.slice(0, pathSplit.length - 1).join('/');
28-
}
29-
30-
async createFolders(normalizedPath: string) {
31-
let current = normalizedPath;
32-
while (current && !await this.app.vault.adapter.exists(current)) {
33-
await this.app.vault.createFolder(current);
34-
current = this.getParentOfNormalized(current);
35-
}
36-
}
37-
38-
isDescendantOf(file: TFile, folder: TFolder): boolean {
39-
let ancestor = file.parent;
40-
while (ancestor && !ancestor.isRoot()) {
41-
if (ancestor === folder) {
42-
return true;
43-
}
44-
ancestor = ancestor.parent;
45-
}
46-
return false;
47-
}
48-
49-
async goTo(filePath: string, newLeaf: boolean) {
50-
let file = this.app.vault.getAbstractFileByPath(filePath) as TFile;
51-
let link = this.app.metadataCache.fileToLinktext(file, "");
52-
await this.app.workspace.openLinkText(link, "", newLeaf);
53-
}
54-
55-
getActiveNoteFile() {
56-
return (this.app.workspace.activeLeaf.view as MarkdownView).file;
57-
}
7+
constructor(app: App) {
8+
super(app);
9+
}
10+
11+
async createIfNotExists(file: string, data: string) {
12+
const normalizedPath = normalizePath(file);
13+
if (!(await this.app.vault.adapter.exists(normalizedPath))) {
14+
let folderPath = this.getParentOfNormalized(normalizedPath);
15+
await this.createFolders(folderPath);
16+
await this.app.vault.create(normalizedPath, data);
17+
}
18+
}
19+
20+
getTFile(filePath: string) {
21+
let file = this.app.vault.getAbstractFileByPath(filePath);
22+
if (file instanceof TFile) return file;
23+
return null;
24+
}
25+
26+
toLinkText(file: TFile) {
27+
return this.app.metadataCache.fileToLinktext(file, "", true);
28+
}
29+
30+
getParentOfNormalized(normalizedPath: string) {
31+
let pathSplit = normalizedPath.split("/");
32+
return pathSplit.slice(0, pathSplit.length - 1).join("/");
33+
}
34+
35+
async createFolders(normalizedPath: string) {
36+
let current = normalizedPath;
37+
while (current && !(await this.app.vault.adapter.exists(current))) {
38+
await this.app.vault.createFolder(current);
39+
current = this.getParentOfNormalized(current);
40+
}
41+
}
42+
43+
isDescendantOf(file: TFile, folder: TFolder): boolean {
44+
let ancestor = file.parent;
45+
while (ancestor && !ancestor.isRoot()) {
46+
if (ancestor === folder) {
47+
return true;
48+
}
49+
ancestor = ancestor.parent;
50+
}
51+
return false;
52+
}
53+
54+
async goTo(filePath: string, newLeaf: boolean) {
55+
let file = this.app.vault.getAbstractFileByPath(filePath) as TFile;
56+
let link = this.app.metadataCache.fileToLinktext(file, "");
57+
await this.app.workspace.openLinkText(link, "", newLeaf);
58+
}
59+
60+
getActiveNoteFile() {
61+
return this.app.workspace.getActiveViewOfType(MarkdownView)?.file;
62+
}
5863
}

0 commit comments

Comments
 (0)