Skip to content

Commit 66c00b2

Browse files
authored
Merge branch 'master' into master
2 parents 4a7ed47 + 437547f commit 66c00b2

File tree

10 files changed

+141
-16
lines changed

10 files changed

+141
-16
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# Better Word Count
22

3-
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/lukeleppan/better-word-count/Build%20Release?logo=github&style=for-the-badge) ![GitHub release (latest by date)](https://img.shields.io/github/v/release/lukeleppan/better-word-count?style=for-the-badge) ![Obsidian Downloads](https://img.shields.io/badge/dynamic/json?logo=obsidian&color=%23483699&label=downloads&query=%24%5B%22better-word-count%22%5D.downloads&url=https%3A%2F%2Fraw.githubusercontent.com%2Fobsidianmd%2Fobsidian-releases%2Fmaster%2Fcommunity-plugin-stats.json&style=for-the-badge)
3+
![GitHub manifest version](https://img.shields.io/github/manifest-json/v/lukeleppan/better-word-count?color=magenta&label=version&style=for-the-badge) ![GitHub Release Date](https://img.shields.io/github/release-date/lukeleppan/better-word-count?style=for-the-badge) ![Lines of code](https://img.shields.io/tokei/lines/github/lukeleppan/better-word-count?style=for-the-badge) ![Obsidian Downloads](https://img.shields.io/badge/dynamic/json?logo=obsidian&color=%23483699&label=downloads&query=%24%5B%22better-word-count%22%5D.downloads&url=https%3A%2F%2Fraw.githubusercontent.com%2Fobsidianmd%2Fobsidian-releases%2Fmaster%2Fcommunity-plugin-stats.json&style=for-the-badge)
44

5-
**IMPORTANT NOTICE:** Due to the introduction of the new Live Preview feature, this plugin needed to be almost entirely rewritten. It will be unstable for a while so please report bugs on github.
5+
![Better Count Word](https://raw.githubusercontent.com/lukeleppan/better-word-count/master/assets/better-word-count.gif)
66

77
This plugin is the same as the built-in **Word Count** plugin, except when you select text, it will count the selected word instead of the whole document. I recommend turning off the built-in **Word Count** because this plugin is designed to replace that. This plugin also has the ability to store statistics about your vault.
88

9-
![Better Count Word](https://raw.githubusercontent.com/lukeleppan/better-word-count/master/assets/better-word-count.gif)
10-
119
## Features
1210

1311
- Allows you to store statistics about your vault.

src/editor/EditorPlugin.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Transaction } from "@codemirror/state";
12
import {
23
ViewUpdate,
34
PluginValue,
@@ -22,9 +23,19 @@ class EditorPlugin implements PluginValue {
2223
}
2324

2425
const tr = update.transactions[0];
25-
if (!tr) return;
26+
27+
if (!tr) {
28+
return;
29+
}
30+
31+
// When selecting text with Shift+Home the userEventType is undefined.
32+
// This is probably a bug in codemirror, for the time being doing an explict check
33+
// for the type allows us to update the stats for the selection.
34+
const userEventTypeUndefined =
35+
tr.annotation(Transaction.userEvent) === undefined;
36+
2637
if (
27-
tr.isUserEvent("select") &&
38+
(tr.isUserEvent("select") || userEventTypeUndefined) &&
2839
tr.newSelection.ranges[0].from !== tr.newSelection.ranges[0].to
2940
) {
3041
let text = "";

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default class BetterWordCount extends Plugin {
3333

3434
// Handle Statistics
3535
if (this.settings.collectStats) {
36-
this.statsManager = new StatsManager(this.app.vault, this.app.workspace);
36+
this.statsManager = new StatsManager(this.app.vault, this.app.workspace, this);
3737
}
3838

3939
// Handle Status Bar

src/settings/Settings.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export enum MetricCounter {
44
sentences,
55
footnotes,
66
citations,
7+
pages,
78
files,
89
}
910

@@ -40,6 +41,7 @@ export interface BetterWordCountSettings {
4041
altBar: StatusBarItem[];
4142
countComments: boolean;
4243
collectStats: boolean;
44+
pageWords: number;
4345
}
4446

4547
export const DEFAULT_SETTINGS: BetterWordCountSettings = {
@@ -73,4 +75,5 @@ export const DEFAULT_SETTINGS: BetterWordCountSettings = {
7375
],
7476
countComments: false,
7577
collectStats: false,
78+
pageWords: 300,
7679
};

src/settings/SettingsTab.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { App, PluginSettingTab, Setting, ToggleComponent } from "obsidian";
1+
import { App, PluginSettingTab, Setting, ToggleComponent, TextComponent } from "obsidian";
22
import type BetterWordCount from "src/main";
33
import { addStatusBarSettings } from "./StatusBarSettings";
44

@@ -37,6 +37,18 @@ export default class BetterWordCountSettingsTab extends PluginSettingTab {
3737
await this.plugin.saveSettings();
3838
});
3939
});
40+
new Setting(containerEl)
41+
.setName("Page Word Count")
42+
.setDesc("Set how many words count as one \"page\"")
43+
.addText((text: TextComponent) => {
44+
text.inputEl.type = "number";
45+
text.setPlaceholder("300");
46+
text.setValue(this.plugin.settings.pageWords.toString());
47+
text.onChange(async (value: string) => {
48+
this.plugin.settings.pageWords = parseInt(value);
49+
await this.plugin.saveSettings();
50+
});
51+
});
4052

4153
// Status Bar Settings
4254
addStatusBarSettings(this.plugin, containerEl);

src/settings/StatusBarSettings.svelte

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
return "Footnotes in Note"
2323
case MetricCounter.citations:
2424
return "Citations in Note"
25+
case MetricCounter.pages:
26+
return "Pages in Note"
2527
case MetricCounter.files:
2628
return "Total Notes"
2729
}
@@ -37,6 +39,8 @@
3739
return "Daily Footnotes"
3840
case MetricCounter.citations:
3941
return "Daily Citations"
42+
case MetricCounter.pages:
43+
return "Daily Pages"
4044
case MetricCounter.files:
4145
return "Total Notes"
4246
}
@@ -52,6 +56,8 @@
5256
return "Total Footnotes"
5357
case MetricCounter.citations:
5458
return "Total Citations"
59+
case MetricCounter.pages:
60+
return "Total Pages"
5561
case MetricCounter.files:
5662
return "Total Notes"
5763
}
@@ -195,6 +201,7 @@
195201
<option value={MetricCounter.sentences}>Sentences</option>
196202
<option value={MetricCounter.footnotes}>Footnotes</option>
197203
<option value={MetricCounter.citations}>Citations</option>
204+
<option value={MetricCounter.pages}>Pages</option>
198205
<option value={MetricCounter.files}>Files</option>
199206
</select>
200207
</div>
@@ -364,6 +371,7 @@
364371
<option value={MetricCounter.sentences}>Sentences</option>
365372
<option value={MetricCounter.footnotes}>Footnotes</option>
366373
<option value={MetricCounter.citations}>Citations</option>
374+
<option value={MetricCounter.pages}>Pages</option>
367375
<option value={MetricCounter.files}>Files</option>
368376
</select>
369377
</div>

src/stats/Stats.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface Day {
99
words: number;
1010
characters: number;
1111
sentences: number;
12+
pages: number;
1213
files: number;
1314
footnotes: number;
1415
citations: number;
@@ -17,6 +18,7 @@ export interface Day {
1718
totalSentences: number;
1819
totalFootnotes: number;
1920
totalCitations: number;
21+
totalPages: number;
2022
}
2123

2224
export type ModifiedFiles = Record<string, FileStat>;
@@ -27,6 +29,7 @@ export interface FileStat {
2729
words: CountDiff;
2830
characters: CountDiff;
2931
sentences: CountDiff;
32+
pages: CountDiff;
3033
}
3134

3235
export interface CountDiff {

src/stats/StatsManager.ts

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { debounce, Debouncer, TFile, Vault, Workspace } from "obsidian";
2+
import type BetterWordCount from "../main";
23
import { STATS_FILE } from "../constants";
34
import type { Day, VaultStatistics } from "./Stats";
45
import moment from "moment";
56
import {
67
getCharacterCount,
78
getSentenceCount,
9+
getPageCount,
810
getWordCount,
911
getCitationCount,
1012
getFootnoteCount,
@@ -13,13 +15,15 @@ import {
1315
export default class StatsManager {
1416
private vault: Vault;
1517
private workspace: Workspace;
18+
private plugin: BetterWordCount;
1619
private vaultStats: VaultStatistics;
1720
private today: string;
1821
public debounceChange;
1922

20-
constructor(vault: Vault, workspace: Workspace) {
23+
constructor(vault: Vault, workspace: Workspace, plugin: BetterWordCount) {
2124
this.vault = vault;
2225
this.workspace = workspace;
26+
this.plugin = plugin;
2327
this.debounceChange = debounce(
2428
(text: string) => this.change(text),
2529
50,
@@ -80,11 +84,13 @@ export default class StatsManager {
8084
const totalSentences = await this.calcTotalSentences();
8185
const totalFootnotes = await this.calcTotalFootnotes();
8286
const totalCitations = await this.calcTotalCitations();
87+
const totalPages = await this.calcTotalPages();
8388

8489
const newDay: Day = {
8590
words: 0,
8691
characters: 0,
8792
sentences: 0,
93+
pages: 0,
8894
files: 0,
8995
footnotes: 0,
9096
citations: 0,
@@ -93,6 +99,7 @@ export default class StatsManager {
9399
totalSentences: totalSentences,
94100
totalFootnotes: totalFootnotes,
95101
totalCitations: totalCitations,
102+
totalPages: totalPages,
96103
};
97104

98105
this.vaultStats.modifiedFiles = {};
@@ -107,6 +114,8 @@ export default class StatsManager {
107114
const currentSentences = getSentenceCount(text);
108115
const currentCitations = getCitationCount(text);
109116
const currentFootnotes = getFootnoteCount(text);
117+
const currentPages = getPageCount(text, this.plugin.settings.pageWords);
118+
110119
if (
111120
this.vaultStats.history.hasOwnProperty(this.today) &&
112121
this.today === moment().format("YYYY-MM-DD")
@@ -124,11 +133,15 @@ export default class StatsManager {
124133
currentSentences - modFiles[fileName].footnotes.current;
125134
this.vaultStats.history[this.today].totalCitations +=
126135
currentSentences - modFiles[fileName].citations.current;
136+
this.vaultStats.history[this.today].totalPages +=
137+
currentPages - modFiles[fileName].pages.current;
138+
127139
modFiles[fileName].words.current = currentWords;
128140
modFiles[fileName].characters.current = currentCharacters;
129141
modFiles[fileName].sentences.current = currentSentences;
130-
modFiles[fileName].footnotes.current = currentSentences;
131-
modFiles[fileName].citations.current = currentSentences;
142+
modFiles[fileName].footnotes.current = currentFootnotes;
143+
modFiles[fileName].citations.current = currentCitations;
144+
modFiles[fileName].pages.current = currentPages;
132145
} else {
133146
modFiles[fileName] = {
134147
words: {
@@ -150,6 +163,9 @@ export default class StatsManager {
150163
citations: {
151164
initial: currentCitations,
152165
current: currentCitations,
166+
pages: {
167+
initial: currentPages,
168+
current: currentPages,
153169
},
154170
};
155171
}
@@ -169,6 +185,7 @@ export default class StatsManager {
169185
Math.max(0, counts.sentences.current - counts.sentences.initial)
170186
)
171187
.reduce((a, b) => a + b, 0);
188+
172189
const footnotes = Object.values(modFiles)
173190
.map((counts) =>
174191
Math.max(0, counts.footnotes.current - counts.footnotes.initial)
@@ -178,13 +195,18 @@ export default class StatsManager {
178195
.map((counts) =>
179196
Math.max(0, counts.citations.current - counts.citations.initial)
180197
)
198+
const pages = Object.values(modFiles)
199+
.map((counts) =>
200+
Math.max(0, counts.pages.current - counts.pages.initial)
201+
)
181202
.reduce((a, b) => a + b, 0);
182203

183204
this.vaultStats.history[this.today].words = words;
184205
this.vaultStats.history[this.today].characters = characters;
185206
this.vaultStats.history[this.today].sentences = sentences;
186207
this.vaultStats.history[this.today].footnotes = footnotes;
187208
this.vaultStats.history[this.today].citations = citations;
209+
this.vaultStats.history[this.today].pages = pages;
188210
this.vaultStats.history[this.today].files = this.getTotalFiles();
189211

190212
await this.update();
@@ -205,6 +227,7 @@ export default class StatsManager {
205227
todayHist.totalSentences = await this.calcTotalSentences();
206228
todayHist.totalFootnotes = await this.calcTotalFootnotes();
207229
todayHist.totalCitations = await this.calcTotalCitations();
230+
todayHist.totalPages = await this.calcTotalPages();
208231
this.update();
209232
} else {
210233
this.updateToday();
@@ -248,6 +271,20 @@ export default class StatsManager {
248271
}
249272
return sentence;
250273
}
274+
275+
private async calcTotalPages(): Promise<number> {
276+
let pages = 0;
277+
278+
const files = this.vault.getFiles();
279+
for (const i in files) {
280+
const file = files[i];
281+
if (file.extension === "md") {
282+
pages += getPageCount(await this.vault.cachedRead(file), this.plugin.settings.pageWords);
283+
}
284+
}
285+
286+
return pages;
287+
}
251288

252289
private async calcTotalFootnotes(): Promise<number> {
253290
let footnotes = 0;
@@ -285,13 +322,17 @@ export default class StatsManager {
285322
return this.vaultStats.history[this.today].sentences;
286323
}
287324

325+
288326
public getDailyFootnotes(): number {
289327
return this.vaultStats.history[this.today].footnotes;
290328
}
291329

292330
public getDailyCitations(): number {
293331
return this.vaultStats.history[this.today].citations;
294332
}
333+
public getDailyPages(): number {
334+
return this.vaultStats.history[this.today].pages;
335+
}
295336

296337
public getTotalFiles(): number {
297338
return this.vault.getMarkdownFiles().length;
@@ -311,7 +352,7 @@ export default class StatsManager {
311352
if (!this.vaultStats) return await this.calcTotalSentences();
312353
return this.vaultStats.history[this.today].totalSentences;
313354
}
314-
355+
315356
public async getTotalFootnotes(): Promise<number> {
316357
if (!this.vaultStats) return await this.calcTotalFootnotes();
317358
return this.vaultStats.history[this.today].totalFootnotes;
@@ -322,4 +363,8 @@ export default class StatsManager {
322363
return this.vaultStats.history[this.today].totalCitations;
323364
}
324365

366+
public async getTotalPages(): Promise<number> {
367+
if (!this.vaultStats) return await this.calcTotalPages();
368+
return this.vaultStats.history[this.today].totalPages;
369+
}
325370
}

0 commit comments

Comments
 (0)