feat: add footnote & pandoc citation count

This commit is contained in:
pseudometa 2023-02-17 11:40:51 +01:00
parent 566f8531b5
commit b2ef959464
6 changed files with 125 additions and 5 deletions

View file

@ -13,10 +13,9 @@ This plugin is the same as the built-in **Word Count** plugin, except when you s
- Allows you to store statistics about your vault. - Allows you to store statistics about your vault.
- Works with all languages. - Works with all languages.
- Can display a variety of different stats. Including: - Can display a variety of different stats. Including:
- Words, Characters and Sentences in current file. - Words, Characters, Sentences, Footnotes, and Pandoc Citations in current file.
- Total Words, Characters and Sentences in vault. - Total Words, Characters, Sentences, Footnotes, Pandoc Citations, and Files in vault.
- Words, Characters and Sentences typed today. - Words, Characters, Sentences, Footnotes, and Pandoc Citations typed today.
- Total Files in vault.
- Highly Customizable status bar that can be adapted to your needs. - Highly Customizable status bar that can be adapted to your needs.
## Contributors ## Contributors

View file

@ -18,6 +18,10 @@
return "Chars in Note" return "Chars in Note"
case MetricCounter.sentences: case MetricCounter.sentences:
return "Sentences in Note" return "Sentences in Note"
case MetricCounter.footnotes:
return "Footnotes in Note"
case MetricCounter.citations:
return "Pandoc Citations in Note"
case MetricCounter.files: case MetricCounter.files:
return "Total Notes" return "Total Notes"
} }
@ -29,6 +33,10 @@
return "Daily Chars" return "Daily Chars"
case MetricCounter.sentences: case MetricCounter.sentences:
return "Daily Sentences" return "Daily Sentences"
case MetricCounter.footnotes:
return "Daily Footnotes"
case MetricCounter.citations:
return "Daily Citations"
case MetricCounter.files: case MetricCounter.files:
return "Total Notes" return "Total Notes"
} }
@ -40,6 +48,10 @@
return "Total Chars" return "Total Chars"
case MetricCounter.sentences: case MetricCounter.sentences:
return "Total Sentences" return "Total Sentences"
case MetricCounter.citations:
return "Total Pandoc Citations"
case MetricCounter.footnotes:
return "Total Footnotes"
case MetricCounter.files: case MetricCounter.files:
return "Total Notes" return "Total Notes"
} }
@ -181,6 +193,8 @@
<option value={MetricCounter.words}>Words</option> <option value={MetricCounter.words}>Words</option>
<option value={MetricCounter.characters}>Characters</option> <option value={MetricCounter.characters}>Characters</option>
<option value={MetricCounter.sentences}>Sentences</option> <option value={MetricCounter.sentences}>Sentences</option>
<option value={MetricCounter.footnotes}>Footnotes</option>
<option value={MetricCounter.citations}>Pandoc Citations</option>
<option value={MetricCounter.files}>Files</option> <option value={MetricCounter.files}>Files</option>
</select> </select>
</div> </div>
@ -348,6 +362,8 @@
<option value={MetricCounter.words}>Words</option> <option value={MetricCounter.words}>Words</option>
<option value={MetricCounter.characters}>Characters</option> <option value={MetricCounter.characters}>Characters</option>
<option value={MetricCounter.sentences}>Sentences</option> <option value={MetricCounter.sentences}>Sentences</option>
<option value={MetricCounter.footnotes}>Footnotes</option>
<option value={MetricCounter.citations}>Pandoc Citations</option>
<option value={MetricCounter.files}>Files</option> <option value={MetricCounter.files}>Files</option>
</select> </select>
</div> </div>

View file

@ -10,14 +10,20 @@ export interface Day {
characters: number; characters: number;
sentences: number; sentences: number;
files: number; files: number;
footnotes: number;
citations: number;
totalWords: number; totalWords: number;
totalCharacters: number; totalCharacters: number;
totalSentences: number; totalSentences: number;
totalFootnotes: number;
totalCitations: number;
} }
export type ModifiedFiles = Record<string, FileStat>; export type ModifiedFiles = Record<string, FileStat>;
export interface FileStat { export interface FileStat {
footnotes: CountDiff;
citations: CountDiff;
words: CountDiff; words: CountDiff;
characters: CountDiff; characters: CountDiff;
sentences: CountDiff; sentences: CountDiff;

View file

@ -6,6 +6,8 @@ import {
getCharacterCount, getCharacterCount,
getSentenceCount, getSentenceCount,
getWordCount, getWordCount,
getCitationCount,
getFootnoteCount,
} from "../utils/StatUtils"; } from "../utils/StatUtils";
export default class StatsManager { export default class StatsManager {
@ -76,15 +78,21 @@ export default class StatsManager {
const totalWords = await this.calcTotalWords(); const totalWords = await this.calcTotalWords();
const totalCharacters = await this.calcTotalCharacters(); const totalCharacters = await this.calcTotalCharacters();
const totalSentences = await this.calcTotalSentences(); const totalSentences = await this.calcTotalSentences();
const totalFootnotes = await this.calcTotalFootnotes();
const totalCitations = await this.calcTotalCitations();
const newDay: Day = { const newDay: Day = {
words: 0, words: 0,
characters: 0, characters: 0,
sentences: 0, sentences: 0,
files: 0, files: 0,
footnotes: 0,
citations: 0,
totalWords: totalWords, totalWords: totalWords,
totalCharacters: totalCharacters, totalCharacters: totalCharacters,
totalSentences: totalSentences, totalSentences: totalSentences,
totalFootnotes: totalFootnotes,
totalCitations: totalCitations,
}; };
this.vaultStats.modifiedFiles = {}; this.vaultStats.modifiedFiles = {};
@ -97,6 +105,8 @@ export default class StatsManager {
const currentWords = getWordCount(text); const currentWords = getWordCount(text);
const currentCharacters = getCharacterCount(text); const currentCharacters = getCharacterCount(text);
const currentSentences = getSentenceCount(text); const currentSentences = getSentenceCount(text);
const currentCitations = getCitationCount(text);
const currentFootnotes = getFootnoteCount(text);
if ( if (
this.vaultStats.history.hasOwnProperty(this.today) && this.vaultStats.history.hasOwnProperty(this.today) &&
this.today === moment().format("YYYY-MM-DD") this.today === moment().format("YYYY-MM-DD")
@ -110,9 +120,15 @@ export default class StatsManager {
currentCharacters - modFiles[fileName].characters.current; currentCharacters - modFiles[fileName].characters.current;
this.vaultStats.history[this.today].totalSentences += this.vaultStats.history[this.today].totalSentences +=
currentSentences - modFiles[fileName].sentences.current; currentSentences - modFiles[fileName].sentences.current;
this.vaultStats.history[this.today].totalFootnotes +=
currentSentences - modFiles[fileName].footnotes.current;
this.vaultStats.history[this.today].totalCitations +=
currentSentences - modFiles[fileName].citations.current;
modFiles[fileName].words.current = currentWords; modFiles[fileName].words.current = currentWords;
modFiles[fileName].characters.current = currentCharacters; modFiles[fileName].characters.current = currentCharacters;
modFiles[fileName].sentences.current = currentSentences; modFiles[fileName].sentences.current = currentSentences;
modFiles[fileName].footnotes.current = currentSentences;
modFiles[fileName].citations.current = currentSentences;
} else { } else {
modFiles[fileName] = { modFiles[fileName] = {
words: { words: {
@ -127,6 +143,14 @@ export default class StatsManager {
initial: currentSentences, initial: currentSentences,
current: currentSentences, current: currentSentences,
}, },
footnotes: {
initial: currentFootnotes,
current: currentFootnotes,
},
citations: {
initial: currentCitations,
current: currentCitations,
},
}; };
} }
@ -145,10 +169,22 @@ export default class StatsManager {
Math.max(0, counts.sentences.current - counts.sentences.initial) Math.max(0, counts.sentences.current - counts.sentences.initial)
) )
.reduce((a, b) => a + b, 0); .reduce((a, b) => a + b, 0);
const footnotes = Object.values(modFiles)
.map((counts) =>
Math.max(0, counts.footnotes.current - counts.footnotes.initial)
)
.reduce((a, b) => a + b, 0);
const citations = Object.values(modFiles)
.map((counts) =>
Math.max(0, counts.citations.current - counts.citations.initial)
)
.reduce((a, b) => a + b, 0);
this.vaultStats.history[this.today].words = words; this.vaultStats.history[this.today].words = words;
this.vaultStats.history[this.today].characters = characters; this.vaultStats.history[this.today].characters = characters;
this.vaultStats.history[this.today].sentences = sentences; this.vaultStats.history[this.today].sentences = sentences;
this.vaultStats.history[this.today].footnotes = footnotes;
this.vaultStats.history[this.today].citations = citations;
this.vaultStats.history[this.today].files = this.getTotalFiles(); this.vaultStats.history[this.today].files = this.getTotalFiles();
await this.update(); await this.update();
@ -167,6 +203,8 @@ export default class StatsManager {
todayHist.totalWords = await this.calcTotalWords(); todayHist.totalWords = await this.calcTotalWords();
todayHist.totalCharacters = await this.calcTotalCharacters(); todayHist.totalCharacters = await this.calcTotalCharacters();
todayHist.totalSentences = await this.calcTotalSentences(); todayHist.totalSentences = await this.calcTotalSentences();
todayHist.totalFootnotes = await this.calcTotalFootnotes();
todayHist.totalCitations = await this.calcTotalCitations();
this.update(); this.update();
} else { } else {
this.updateToday(); this.updateToday();
@ -208,10 +246,33 @@ export default class StatsManager {
sentence += getSentenceCount(await this.vault.cachedRead(file)); sentence += getSentenceCount(await this.vault.cachedRead(file));
} }
} }
return sentence; return sentence;
} }
private async calcTotalFootnotes(): Promise<number> {
let footnotes = 0;
const files = this.vault.getFiles();
for (const i in files) {
const file = files[i];
if (file.extension === "md") {
footnotes += getFootnoteCount(await this.vault.cachedRead(file));
}
}
return footnotes;
}
private async calcTotalCitations(): Promise<number> {
let citations = 0;
const files = this.vault.getFiles();
for (const i in files) {
const file = files[i];
if (file.extension === "md") {
citations += getCitationCount(await this.vault.cachedRead(file));
}
}
return citations;
}
public getDailyWords(): number { public getDailyWords(): number {
return this.vaultStats.history[this.today].words; return this.vaultStats.history[this.today].words;
} }
@ -224,6 +285,14 @@ export default class StatsManager {
return this.vaultStats.history[this.today].sentences; return this.vaultStats.history[this.today].sentences;
} }
public getDailyFootnotes(): number {
return this.vaultStats.history[this.today].footnotes;
}
public getDailyCitations(): number {
return this.vaultStats.history[this.today].citations;
}
public getTotalFiles(): number { public getTotalFiles(): number {
return this.vault.getMarkdownFiles().length; return this.vault.getMarkdownFiles().length;
} }
@ -242,4 +311,15 @@ export default class StatsManager {
if (!this.vaultStats) return await this.calcTotalSentences(); if (!this.vaultStats) return await this.calcTotalSentences();
return this.vaultStats.history[this.today].totalSentences; return this.vaultStats.history[this.today].totalSentences;
} }
public async getTotalFootnotes(): Promise<number> {
if (!this.vaultStats) return await this.calcTotalFootnotes();
return this.vaultStats.history[this.today].totalFootnotes;
}
public async getTotalCitations(): Promise<number> {
if (!this.vaultStats) return await this.calcTotalCitations();
return this.vaultStats.history[this.today].totalCitations;
}
} }

View file

@ -4,6 +4,8 @@ import {
getWordCount, getWordCount,
getCharacterCount, getCharacterCount,
getSentenceCount, getSentenceCount,
getCitationCount,
getFootnoteCount,
} from "src/utils/StatUtils"; } from "src/utils/StatUtils";
import { debounce } from "obsidian"; import { debounce } from "obsidian";

View file

@ -27,6 +27,23 @@ export function getCharacterCount(text: string): number {
return text.length; return text.length;
} }
export function getFootnoteCount(text: string): number {
const regularFn = text.match(/\[\^\S+](?!:)/g);
const inlineFn = text.match(/\^\[[^^].+?]/g);
let overallFn = 0;
if (regularFn) overallFn += regularFn.length;
if (inlineFn) overallFn += inlineFn.length;
return overallFn;
}
export function getCitationCount(text: string): number {
const pandocCitations = text.match(/@[A-Za-z0-9-]+[,;\]](?!\()/gi);
if (!pandocCitations) return 0;
const uniqueCitations = [...new Set(pandocCitations)].length;
return uniqueCitations;
}
export function getSentenceCount(text: string): number { export function getSentenceCount(text: string): number {
const sentences: number = ( const sentences: number = (
(text || "").match( (text || "").match(