Skip to content

Commit 49ed5c1

Browse files
authored
Merge pull request lukeleppan#100 from davisriedel/feature/api
Expose API with counting and stats to other plugins
2 parents e6e332b + 88a3f4f commit 49ed5c1

File tree

6 files changed

+157
-6
lines changed

6 files changed

+157
-6
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
*.iml
33
.idea
44

5+
# VSCode
6+
.vscode
7+
58
# npm
69
node_modules
710
package-lock.json
@@ -12,4 +15,4 @@ main.js
1215
test-vault/
1316
*.zip
1417
.DS_Store
15-
dist/
18+
dist/

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"version": "0.10.0",
44
"description": "Counts the words of selected text in the editor.",
55
"main": "main.js",
6+
"types": "src/api.d.ts",
7+
"files": ["src/api.d.ts"],
68
"scripts": {
79
"lint": "svelte-check && eslint . --ext .ts",
810
"dev": "rollup --config rollup.config.js -w",

src/api/api.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import "obsidian";
2+
import type BetterWordCountApi from "./api";
3+
4+
declare module "obsidian" {
5+
interface App {
6+
plugins: {
7+
enabledPlugins: Set<string>;
8+
plugins: {
9+
["better-word-count"]?: {
10+
api: BetterWordCountApi;
11+
};
12+
};
13+
};
14+
}
15+
}

src/api/api.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import { TFile, normalizePath } from "obsidian";
2+
import type BetterWordCount from "src/main";
3+
import { getCharacterCount, getCitationCount, getFootnoteCount, getPageCount, getSentenceCount, getWordCount } from "src/utils/StatUtils";
4+
5+
export default class BetterWordCountApi {
6+
private plugin: BetterWordCount;
7+
8+
constructor(plugin: BetterWordCount) {
9+
this.plugin = plugin;
10+
}
11+
12+
// plain utility functions
13+
public getWordCount(text: string): number {
14+
return getWordCount(text);
15+
}
16+
public getCharacterCount(text: string): number {
17+
return getCharacterCount(text);
18+
}
19+
public getFootnoteCount(text: string): number {
20+
return getFootnoteCount(text);
21+
}
22+
public getCitationCount(text: string): number {
23+
return getCitationCount(text);
24+
}
25+
public getSentenceCount(text: string): number {
26+
return getSentenceCount(text);
27+
}
28+
public getPageCount(text: string, pageWords: number = this.plugin.settings.pageWords): number {
29+
return getPageCount(text, pageWords);
30+
}
31+
32+
// Functions using page paths e.g. for use with dataviewjs
33+
private async countPagePath(path: string, countFunc: (text: string) => number): Promise<number | null> {
34+
const normalizedPath = normalizePath(path);
35+
const file = this.plugin.app.vault.getAbstractFileByPath(normalizedPath);
36+
37+
// Check if it exists and is of the correct type
38+
if (file instanceof TFile) {
39+
const text = await this.plugin.app.vault.cachedRead(file);
40+
return countFunc(text);
41+
}
42+
43+
return null;
44+
}
45+
46+
public async getWordCountPagePath(path: string): Promise<number | null> {
47+
return this.countPagePath(path, getWordCount);
48+
}
49+
public getCharacterCountPagePath(path: string): Promise<number | null> {
50+
return this.countPagePath(path, getCharacterCount);
51+
}
52+
public getFootnoteCountPagePath(path: string): Promise<number | null> {
53+
return this.countPagePath(path, getFootnoteCount);
54+
}
55+
public getCitationCountPagePath(path: string): Promise<number | null> {
56+
return this.countPagePath(path, getCitationCount);
57+
}
58+
public getSentenceCountPagePath(path: string): Promise<number | null> {
59+
return this.countPagePath(path, getSentenceCount);
60+
}
61+
public getPageCountPagePath(path: string, pageWords: number = this.plugin.settings.pageWords): Promise<number | null> {
62+
return this.countPagePath(path, (text: string) => getPageCount(text, pageWords));
63+
}
64+
65+
// Functions for accessing stats
66+
public getDailyWords(): number | null {
67+
if (!this.plugin.statsManager) return null;
68+
return this.plugin.statsManager.getDailyWords();
69+
}
70+
71+
public getDailyCharacters(): number | null {
72+
if (!this.plugin.statsManager) return null;
73+
return this.plugin.statsManager.getDailyCharacters();
74+
}
75+
76+
public getDailySentences(): number | null {
77+
if (!this.plugin.statsManager) return null;
78+
return this.plugin.statsManager.getDailySentences();
79+
}
80+
81+
public getDailyFootnotes(): number | null {
82+
if (!this.plugin.statsManager) return null;
83+
return this.plugin.statsManager.getDailyFootnotes();
84+
}
85+
86+
public getDailyCitations(): number | null {
87+
if (!this.plugin.statsManager) return null;
88+
return this.plugin.statsManager.getDailyCitations();
89+
}
90+
91+
public getDailyPages(): number | null {
92+
if (!this.plugin.statsManager) return null;
93+
return this.plugin.statsManager.getDailyPages();
94+
}
95+
96+
public getTotalFiles(): number | null {
97+
if (!this.plugin.statsManager) return null;
98+
return this.plugin.statsManager.getTotalFiles();
99+
}
100+
101+
public async getTotalWords(): Promise<number | null> {
102+
if (!this.plugin.statsManager) return null;
103+
return this.plugin.statsManager.getTotalWords();
104+
}
105+
106+
public async getTotalCharacters(): Promise<number | null> {
107+
if (!this.plugin.statsManager) return null;
108+
return this.plugin.statsManager.getTotalCharacters();
109+
}
110+
111+
public async getTotalSentences(): Promise<number | null> {
112+
if (!this.plugin.statsManager) return null;
113+
return this.plugin.statsManager.getTotalSentences();
114+
}
115+
116+
public async getTotalFootnotes(): Promise<number | null> {
117+
if (!this.plugin.statsManager) return null;
118+
return this.plugin.statsManager.getTotalFootnotes();
119+
}
120+
121+
public async getTotalCitations(): Promise<number | null> {
122+
if (!this.plugin.statsManager) return null;
123+
return this.plugin.statsManager.getTotalCitations();
124+
}
125+
126+
public async getTotalPages(): Promise<number | null> {
127+
if (!this.plugin.statsManager) return null;
128+
return this.plugin.statsManager.getTotalPages();
129+
}
130+
}

src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import {
1111
} from "./editor/EditorPlugin";
1212
import { BetterWordCountSettings, DEFAULT_SETTINGS } from "src/settings/Settings";
1313
import { settingsStore } from "./utils/SvelteStores";
14+
import BetterWordCountApi from "src/api/api";
1415
import { handleFileMenu } from "./utils/FileMenu";
1516

1617
export default class BetterWordCount extends Plugin {
1718
public settings: BetterWordCountSettings;
1819
public statusBar: StatusBar;
1920
public statsManager: StatsManager;
21+
public api: BetterWordCountApi = new BetterWordCountApi(this);
2022

2123
async onunload(): Promise<void> {
2224
this.statsManager = null;

src/stats/StatsManager.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export default class StatsManager {
119119
const currentCitations = getCitationCount(text);
120120
const currentFootnotes = getFootnoteCount(text);
121121
const currentPages = getPageCount(text, this.plugin.settings.pageWords);
122-
122+
123123
if (
124124
this.vaultStats.history.hasOwnProperty(this.today) &&
125125
this.today === moment().format("YYYY-MM-DD")
@@ -139,7 +139,7 @@ export default class StatsManager {
139139
currentSentences - modFiles[fileName].citations.current;
140140
this.vaultStats.history[this.today].totalPages +=
141141
currentPages - modFiles[fileName].pages.current;
142-
142+
143143
modFiles[fileName].words.current = currentWords;
144144
modFiles[fileName].characters.current = currentCharacters;
145145
modFiles[fileName].sentences.current = currentSentences;
@@ -276,7 +276,7 @@ export default class StatsManager {
276276
}
277277
return sentence;
278278
}
279-
279+
280280
private async calcTotalPages(): Promise<number> {
281281
let pages = 0;
282282

@@ -327,7 +327,6 @@ export default class StatsManager {
327327
return this.vaultStats.history[this.today].sentences;
328328
}
329329

330-
331330
public getDailyFootnotes(): number {
332331
return this.vaultStats.history[this.today].footnotes;
333332
}
@@ -357,7 +356,7 @@ export default class StatsManager {
357356
if (!this.vaultStats) return await this.calcTotalSentences();
358357
return this.vaultStats.history[this.today].totalSentences;
359358
}
360-
359+
361360
public async getTotalFootnotes(): Promise<number> {
362361
if (!this.vaultStats) return await this.calcTotalFootnotes();
363362
return this.vaultStats.history[this.today].totalFootnotes;

0 commit comments

Comments
 (0)