Skip to content

Commit 0a208ea

Browse files
authored
Merge pull request lukeleppan#79 from chrisgrieser/master
Feat: Option to add Citations / Footnote Counts
2 parents 437547f + d2f99e5 commit 0a208ea

File tree

7 files changed

+207
-5
lines changed

7 files changed

+207
-5
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ This plugin is the same as the built-in **Word Count** plugin, except when you s
1111
- Allows you to store statistics about your vault.
1212
- Works with all languages.
1313
- Can display a variety of different stats. Including:
14-
- Words, Characters and Sentences in current file.
15-
- Total Words, Characters and Sentences in vault.
16-
- Words, Characters and Sentences typed today.
17-
- Total Files in vault.
14+
- Words, Characters, Sentences, Footnotes, and Pandoc Citations in current file.
15+
- Total Words, Characters, Sentences, Footnotes, Pandoc Citations, and Files in vault.
16+
- Words, Characters, Sentences, Footnotes, and Pandoc Citations typed today.
1817
- Highly Customizable status bar that can be adapted to your needs.
1918

2019
## Contributors

src/settings/Settings.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ export enum MetricCounter {
22
words,
33
characters,
44
sentences,
5+
footnotes,
6+
citations,
57
pages,
68
files,
79
}

src/settings/StatusBarSettings.svelte

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
return "Chars in Note"
1919
case MetricCounter.sentences:
2020
return "Sentences in Note"
21+
case MetricCounter.footnotes:
22+
return "Footnotes in Note"
23+
case MetricCounter.citations:
24+
return "Citations in Note"
2125
case MetricCounter.pages:
2226
return "Pages in Note"
2327
case MetricCounter.files:
@@ -31,6 +35,10 @@
3135
return "Daily Chars"
3236
case MetricCounter.sentences:
3337
return "Daily Sentences"
38+
case MetricCounter.footnotes:
39+
return "Daily Footnotes"
40+
case MetricCounter.citations:
41+
return "Daily Citations"
3442
case MetricCounter.pages:
3543
return "Daily Pages"
3644
case MetricCounter.files:
@@ -44,6 +52,10 @@
4452
return "Total Chars"
4553
case MetricCounter.sentences:
4654
return "Total Sentences"
55+
case MetricCounter.footnotes:
56+
return "Total Footnotes"
57+
case MetricCounter.citations:
58+
return "Total Citations"
4759
case MetricCounter.pages:
4860
return "Total Pages"
4961
case MetricCounter.files:
@@ -187,6 +199,8 @@
187199
<option value={MetricCounter.words}>Words</option>
188200
<option value={MetricCounter.characters}>Characters</option>
189201
<option value={MetricCounter.sentences}>Sentences</option>
202+
<option value={MetricCounter.footnotes}>Footnotes</option>
203+
<option value={MetricCounter.citations}>Citations</option>
190204
<option value={MetricCounter.pages}>Pages</option>
191205
<option value={MetricCounter.files}>Files</option>
192206
</select>
@@ -355,6 +369,8 @@
355369
<option value={MetricCounter.words}>Words</option>
356370
<option value={MetricCounter.characters}>Characters</option>
357371
<option value={MetricCounter.sentences}>Sentences</option>
372+
<option value={MetricCounter.footnotes}>Footnotes</option>
373+
<option value={MetricCounter.citations}>Citations</option>
358374
<option value={MetricCounter.pages}>Pages</option>
359375
<option value={MetricCounter.files}>Files</option>
360376
</select>

src/stats/Stats.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@ export interface Day {
1111
sentences: number;
1212
pages: number;
1313
files: number;
14+
footnotes: number;
15+
citations: number;
1416
totalWords: number;
1517
totalCharacters: number;
1618
totalSentences: number;
19+
totalFootnotes: number;
20+
totalCitations: number;
1721
totalPages: number;
1822
}
1923

2024
export type ModifiedFiles = Record<string, FileStat>;
2125

2226
export interface FileStat {
27+
footnotes: CountDiff;
28+
citations: CountDiff;
2329
words: CountDiff;
2430
characters: CountDiff;
2531
sentences: CountDiff;

src/stats/StatsManager.ts

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
getSentenceCount,
99
getPageCount,
1010
getWordCount,
11+
getCitationCount,
12+
getFootnoteCount,
1113
} from "../utils/StatUtils";
1214

1315
export default class StatsManager {
@@ -80,6 +82,8 @@ export default class StatsManager {
8082
const totalWords = await this.calcTotalWords();
8183
const totalCharacters = await this.calcTotalCharacters();
8284
const totalSentences = await this.calcTotalSentences();
85+
const totalFootnotes = await this.calcTotalFootnotes();
86+
const totalCitations = await this.calcTotalCitations();
8387
const totalPages = await this.calcTotalPages();
8488

8589
const newDay: Day = {
@@ -88,9 +92,13 @@ export default class StatsManager {
8892
sentences: 0,
8993
pages: 0,
9094
files: 0,
95+
footnotes: 0,
96+
citations: 0,
9197
totalWords: totalWords,
9298
totalCharacters: totalCharacters,
9399
totalSentences: totalSentences,
100+
totalFootnotes: totalFootnotes,
101+
totalCitations: totalCitations,
94102
totalPages: totalPages,
95103
};
96104

@@ -104,6 +112,8 @@ export default class StatsManager {
104112
const currentWords = getWordCount(text);
105113
const currentCharacters = getCharacterCount(text);
106114
const currentSentences = getSentenceCount(text);
115+
const currentCitations = getCitationCount(text);
116+
const currentFootnotes = getFootnoteCount(text);
107117
const currentPages = getPageCount(text, this.plugin.settings.pageWords);
108118

109119
if (
@@ -119,11 +129,18 @@ export default class StatsManager {
119129
currentCharacters - modFiles[fileName].characters.current;
120130
this.vaultStats.history[this.today].totalSentences +=
121131
currentSentences - modFiles[fileName].sentences.current;
132+
this.vaultStats.history[this.today].totalFootnotes +=
133+
currentSentences - modFiles[fileName].footnotes.current;
134+
this.vaultStats.history[this.today].totalCitations +=
135+
currentSentences - modFiles[fileName].citations.current;
122136
this.vaultStats.history[this.today].totalPages +=
123137
currentPages - modFiles[fileName].pages.current;
138+
124139
modFiles[fileName].words.current = currentWords;
125140
modFiles[fileName].characters.current = currentCharacters;
126141
modFiles[fileName].sentences.current = currentSentences;
142+
modFiles[fileName].footnotes.current = currentFootnotes;
143+
modFiles[fileName].citations.current = currentCitations;
127144
modFiles[fileName].pages.current = currentPages;
128145
} else {
129146
modFiles[fileName] = {
@@ -139,6 +156,14 @@ export default class StatsManager {
139156
initial: currentSentences,
140157
current: currentSentences,
141158
},
159+
footnotes: {
160+
initial: currentFootnotes,
161+
current: currentFootnotes,
162+
},
163+
citations: {
164+
initial: currentCitations,
165+
current: currentCitations,
166+
},
142167
pages: {
143168
initial: currentPages,
144169
current: currentPages,
@@ -161,6 +186,16 @@ export default class StatsManager {
161186
Math.max(0, counts.sentences.current - counts.sentences.initial)
162187
)
163188
.reduce((a, b) => a + b, 0);
189+
190+
const footnotes = Object.values(modFiles)
191+
.map((counts) =>
192+
Math.max(0, counts.footnotes.current - counts.footnotes.initial)
193+
)
194+
.reduce((a, b) => a + b, 0);
195+
const citations = Object.values(modFiles)
196+
.map((counts) =>
197+
Math.max(0, counts.citations.current - counts.citations.initial)
198+
).reduce((a, b) => a + b, 0);
164199
const pages = Object.values(modFiles)
165200
.map((counts) =>
166201
Math.max(0, counts.pages.current - counts.pages.initial)
@@ -170,6 +205,8 @@ export default class StatsManager {
170205
this.vaultStats.history[this.today].words = words;
171206
this.vaultStats.history[this.today].characters = characters;
172207
this.vaultStats.history[this.today].sentences = sentences;
208+
this.vaultStats.history[this.today].footnotes = footnotes;
209+
this.vaultStats.history[this.today].citations = citations;
173210
this.vaultStats.history[this.today].pages = pages;
174211
this.vaultStats.history[this.today].files = this.getTotalFiles();
175212

@@ -189,6 +226,8 @@ export default class StatsManager {
189226
todayHist.totalWords = await this.calcTotalWords();
190227
todayHist.totalCharacters = await this.calcTotalCharacters();
191228
todayHist.totalSentences = await this.calcTotalSentences();
229+
todayHist.totalFootnotes = await this.calcTotalFootnotes();
230+
todayHist.totalCitations = await this.calcTotalCitations();
192231
todayHist.totalPages = await this.calcTotalPages();
193232
this.update();
194233
} else {
@@ -231,7 +270,6 @@ export default class StatsManager {
231270
sentence += getSentenceCount(await this.vault.cachedRead(file));
232271
}
233272
}
234-
235273
return sentence;
236274
}
237275

@@ -249,6 +287,30 @@ export default class StatsManager {
249287
return pages;
250288
}
251289

290+
private async calcTotalFootnotes(): Promise<number> {
291+
let footnotes = 0;
292+
const files = this.vault.getFiles();
293+
for (const i in files) {
294+
const file = files[i];
295+
if (file.extension === "md") {
296+
footnotes += getFootnoteCount(await this.vault.cachedRead(file));
297+
}
298+
}
299+
return footnotes;
300+
}
301+
302+
private async calcTotalCitations(): Promise<number> {
303+
let citations = 0;
304+
const files = this.vault.getFiles();
305+
for (const i in files) {
306+
const file = files[i];
307+
if (file.extension === "md") {
308+
citations += getCitationCount(await this.vault.cachedRead(file));
309+
}
310+
}
311+
return citations;
312+
}
313+
252314
public getDailyWords(): number {
253315
return this.vaultStats.history[this.today].words;
254316
}
@@ -261,6 +323,14 @@ export default class StatsManager {
261323
return this.vaultStats.history[this.today].sentences;
262324
}
263325

326+
327+
public getDailyFootnotes(): number {
328+
return this.vaultStats.history[this.today].footnotes;
329+
}
330+
331+
public getDailyCitations(): number {
332+
return this.vaultStats.history[this.today].citations;
333+
}
264334
public getDailyPages(): number {
265335
return this.vaultStats.history[this.today].pages;
266336
}
@@ -283,6 +353,16 @@ export default class StatsManager {
283353
if (!this.vaultStats) return await this.calcTotalSentences();
284354
return this.vaultStats.history[this.today].totalSentences;
285355
}
356+
357+
public async getTotalFootnotes(): Promise<number> {
358+
if (!this.vaultStats) return await this.calcTotalFootnotes();
359+
return this.vaultStats.history[this.today].totalFootnotes;
360+
}
361+
362+
public async getTotalCitations(): Promise<number> {
363+
if (!this.vaultStats) return await this.calcTotalCitations();
364+
return this.vaultStats.history[this.today].totalCitations;
365+
}
286366

287367
public async getTotalPages(): Promise<number> {
288368
if (!this.vaultStats) return await this.calcTotalPages();

src/status/StatusBar.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
getWordCount,
55
getCharacterCount,
66
getSentenceCount,
7+
getCitationCount,
8+
getFootnoteCount,
79
getPageCount,
810
} from "src/utils/StatUtils";
911
import { debounce } from "obsidian";
@@ -108,6 +110,46 @@ export default class StatusBar {
108110
: 0));
109111
break;
110112
}
113+
} else if (metric.counter === MetricCounter.footnotes) {
114+
switch (metric.type) {
115+
case MetricType.file:
116+
display = display + getFootnoteCount(text);
117+
break;
118+
case MetricType.daily:
119+
display =
120+
display +
121+
(this.plugin.settings.collectStats
122+
? this.plugin.statsManager.getDailyFootnotes()
123+
: 0);
124+
break;
125+
case MetricType.total:
126+
display =
127+
display +
128+
(await (this.plugin.settings.collectStats
129+
? this.plugin.statsManager.getTotalFootnotes()
130+
: 0));
131+
break;
132+
}
133+
} else if (metric.counter === MetricCounter.citations) {
134+
switch (metric.type) {
135+
case MetricType.file:
136+
display = display + getCitationCount(text);
137+
break;
138+
case MetricType.daily:
139+
display =
140+
display +
141+
(this.plugin.settings.collectStats
142+
? this.plugin.statsManager.getDailyCitations()
143+
: 0);
144+
break;
145+
case MetricType.total:
146+
display =
147+
display +
148+
(await (this.plugin.settings.collectStats
149+
? this.plugin.statsManager.getTotalCitations()
150+
: 0));
151+
break;
152+
}
111153
} else if (metric.counter === MetricCounter.pages) {
112154
switch (metric.type) {
113155
case MetricType.file:
@@ -230,6 +272,46 @@ export default class StatusBar {
230272
: 0));
231273
break;
232274
}
275+
} else if (metric.counter === MetricCounter.footnotes) {
276+
switch (metric.type) {
277+
case MetricType.file:
278+
display = display + 0;
279+
break;
280+
case MetricType.daily:
281+
display =
282+
display +
283+
(this.plugin.settings.collectStats
284+
? this.plugin.statsManager.getDailyFootnotes()
285+
: 0);
286+
break;
287+
case MetricType.total:
288+
display =
289+
display +
290+
(await (this.plugin.settings.collectStats
291+
? this.plugin.statsManager.getTotalFootnotes()
292+
: 0));
293+
break;
294+
}
295+
} else if (metric.counter === MetricCounter.citations) {
296+
switch (metric.type) {
297+
case MetricType.file:
298+
display = display + 0;
299+
break;
300+
case MetricType.daily:
301+
display =
302+
display +
303+
(this.plugin.settings.collectStats
304+
? this.plugin.statsManager.getDailyCitations()
305+
: 0);
306+
break;
307+
case MetricType.total:
308+
display =
309+
display +
310+
(await (this.plugin.settings.collectStats
311+
? this.plugin.statsManager.getTotalCitations()
312+
: 0));
313+
break;
314+
}
233315
} else if (metric.counter === MetricCounter.pages) {
234316
switch (metric.type) {
235317
case MetricType.file:

0 commit comments

Comments
 (0)