Compare commits
37 commits
Author | SHA1 | Date | |
---|---|---|---|
|
3f7ec6561e | ||
|
8e7860d510 | ||
|
141c39dbb7 | ||
|
0a208ea8ef | ||
|
d2f99e548a | ||
|
66c00b24c2 | ||
|
437547f02e | ||
|
0712d9f61a | ||
|
d3b6938e48 | ||
|
b2a43d9ca3 | ||
|
9bbeeff386 | ||
|
4a7ed4730c | ||
|
3412745204 | ||
|
fee3421508 | ||
|
b2ef959464 | ||
|
86ca1199cc | ||
|
d865d61e23 | ||
|
ec21413d55 | ||
|
fbf7977188 | ||
|
2b847ddcc9 | ||
|
aabd175d5d | ||
|
f43895645f | ||
|
5e970cad74 | ||
|
de3172ee95 | ||
|
759b4d949c | ||
|
9885035ac8 | ||
|
2cc592c13e | ||
|
88a3bd1a2a | ||
|
547fb32f95 | ||
|
9cdab1d1d7 | ||
|
44dfcb9fe3 | ||
|
566f8531b5 | ||
|
496d708117 | ||
|
3871f71aae | ||
|
909e954c3e | ||
|
76ac1fbc86 | ||
|
da44785003 |
12 changed files with 376 additions and 26 deletions
28
README.md
28
README.md
|
@ -1,34 +1,36 @@
|
||||||
# Better Word Count
|
# Better Word Count
|
||||||
|
|
||||||
  
|
   
|
||||||
|
|
||||||
**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.
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- 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
|
||||||
|
|
||||||
- @leoccyao
|
- @leoccyao
|
||||||
- Added all word, char, sentence count when not viewing a markdown file.
|
- Added all word, char, sentence count when not viewing a markdown file.
|
||||||
- @lishid
|
- @chrisgrieser
|
||||||
- Helped solve the performace issue.
|
- Added Footnotes and Citation Counting.
|
||||||
- @THeK3nger
|
|
||||||
- Fixed issue that occured when renaming files.
|
|
||||||
- @bakuzan
|
- @bakuzan
|
||||||
|
- Added page counts.
|
||||||
- Fixed issue that caused errors at start up.
|
- Fixed issue that caused errors at start up.
|
||||||
|
- @THeK3nger
|
||||||
|
- Fixed issue that occurred when renaming files.
|
||||||
|
- @lishid
|
||||||
|
- Helped solve the performance issue.
|
||||||
|
- @Noxellar
|
||||||
|
- Fixed alt bar spacing issue.
|
||||||
|
|
||||||
### Special Thanks
|
### Special Thanks
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"id": "better-word-count",
|
"id": "better-word-count",
|
||||||
"name": "Better Word Count",
|
"name": "Better Word Count",
|
||||||
"version": "0.9.2",
|
"version": "0.9.6",
|
||||||
"description": "Counts the words of selected text in the editor.",
|
"description": "Counts the words of selected text in the editor.",
|
||||||
"author": "Luke Leppan",
|
"author": "Luke Leppan",
|
||||||
"authorUrl": "https://lukeleppan.com",
|
"authorUrl": "https://lukeleppan.com",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "better-word-count",
|
"name": "better-word-count",
|
||||||
"version": "0.9.2",
|
"version": "0.9.6",
|
||||||
"description": "Counts the words of selected text in the editor.",
|
"description": "Counts the words of selected text in the editor.",
|
||||||
"main": "main.js",
|
"main": "main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { Transaction } from "@codemirror/state";
|
||||||
import {
|
import {
|
||||||
ViewUpdate,
|
ViewUpdate,
|
||||||
PluginValue,
|
PluginValue,
|
||||||
|
@ -22,9 +23,19 @@ class EditorPlugin implements PluginValue {
|
||||||
}
|
}
|
||||||
|
|
||||||
const tr = update.transactions[0];
|
const tr = update.transactions[0];
|
||||||
if (!tr) return;
|
|
||||||
|
if (!tr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// When selecting text with Shift+Home the userEventType is undefined.
|
||||||
|
// This is probably a bug in codemirror, for the time being doing an explict check
|
||||||
|
// for the type allows us to update the stats for the selection.
|
||||||
|
const userEventTypeUndefined =
|
||||||
|
tr.annotation(Transaction.userEvent) === undefined;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
tr.isUserEvent("select") &&
|
(tr.isUserEvent("select") || userEventTypeUndefined) &&
|
||||||
tr.newSelection.ranges[0].from !== tr.newSelection.ranges[0].to
|
tr.newSelection.ranges[0].from !== tr.newSelection.ranges[0].to
|
||||||
) {
|
) {
|
||||||
let text = "";
|
let text = "";
|
||||||
|
|
|
@ -33,7 +33,7 @@ export default class BetterWordCount extends Plugin {
|
||||||
|
|
||||||
// Handle Statistics
|
// Handle Statistics
|
||||||
if (this.settings.collectStats) {
|
if (this.settings.collectStats) {
|
||||||
this.statsManager = new StatsManager(this.app.vault, this.app.workspace);
|
this.statsManager = new StatsManager(this.app.vault, this.app.workspace, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle Status Bar
|
// Handle Status Bar
|
||||||
|
|
|
@ -2,6 +2,9 @@ export enum MetricCounter {
|
||||||
words,
|
words,
|
||||||
characters,
|
characters,
|
||||||
sentences,
|
sentences,
|
||||||
|
footnotes,
|
||||||
|
citations,
|
||||||
|
pages,
|
||||||
files,
|
files,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,6 +41,7 @@ export interface BetterWordCountSettings {
|
||||||
altBar: StatusBarItem[];
|
altBar: StatusBarItem[];
|
||||||
countComments: boolean;
|
countComments: boolean;
|
||||||
collectStats: boolean;
|
collectStats: boolean;
|
||||||
|
pageWords: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DEFAULT_SETTINGS: BetterWordCountSettings = {
|
export const DEFAULT_SETTINGS: BetterWordCountSettings = {
|
||||||
|
@ -62,7 +66,7 @@ export const DEFAULT_SETTINGS: BetterWordCountSettings = {
|
||||||
altBar: [
|
altBar: [
|
||||||
{
|
{
|
||||||
prefix: "",
|
prefix: "",
|
||||||
suffix: "files",
|
suffix: " files",
|
||||||
metric: {
|
metric: {
|
||||||
type: MetricType.total,
|
type: MetricType.total,
|
||||||
counter: MetricCounter.files,
|
counter: MetricCounter.files,
|
||||||
|
@ -71,4 +75,5 @@ export const DEFAULT_SETTINGS: BetterWordCountSettings = {
|
||||||
],
|
],
|
||||||
countComments: false,
|
countComments: false,
|
||||||
collectStats: false,
|
collectStats: false,
|
||||||
|
pageWords: 300,
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { App, PluginSettingTab, Setting, ToggleComponent } from "obsidian";
|
import { App, PluginSettingTab, Setting, ToggleComponent, TextComponent } from "obsidian";
|
||||||
import type BetterWordCount from "src/main";
|
import type BetterWordCount from "src/main";
|
||||||
import { addStatusBarSettings } from "./StatusBarSettings";
|
import { addStatusBarSettings } from "./StatusBarSettings";
|
||||||
|
|
||||||
|
@ -37,6 +37,18 @@ export default class BetterWordCountSettingsTab extends PluginSettingTab {
|
||||||
await this.plugin.saveSettings();
|
await this.plugin.saveSettings();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
new Setting(containerEl)
|
||||||
|
.setName("Page Word Count")
|
||||||
|
.setDesc("Set how many words count as one \"page\"")
|
||||||
|
.addText((text: TextComponent) => {
|
||||||
|
text.inputEl.type = "number";
|
||||||
|
text.setPlaceholder("300");
|
||||||
|
text.setValue(this.plugin.settings.pageWords.toString());
|
||||||
|
text.onChange(async (value: string) => {
|
||||||
|
this.plugin.settings.pageWords = parseInt(value);
|
||||||
|
await this.plugin.saveSettings();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// Status Bar Settings
|
// Status Bar Settings
|
||||||
addStatusBarSettings(this.plugin, containerEl);
|
addStatusBarSettings(this.plugin, containerEl);
|
||||||
|
|
|
@ -18,6 +18,12 @@
|
||||||
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 "Citations in Note"
|
||||||
|
case MetricCounter.pages:
|
||||||
|
return "Pages in Note"
|
||||||
case MetricCounter.files:
|
case MetricCounter.files:
|
||||||
return "Total Notes"
|
return "Total Notes"
|
||||||
}
|
}
|
||||||
|
@ -29,6 +35,12 @@
|
||||||
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.pages:
|
||||||
|
return "Daily Pages"
|
||||||
case MetricCounter.files:
|
case MetricCounter.files:
|
||||||
return "Total Notes"
|
return "Total Notes"
|
||||||
}
|
}
|
||||||
|
@ -40,6 +52,12 @@
|
||||||
return "Total Chars"
|
return "Total Chars"
|
||||||
case MetricCounter.sentences:
|
case MetricCounter.sentences:
|
||||||
return "Total Sentences"
|
return "Total Sentences"
|
||||||
|
case MetricCounter.footnotes:
|
||||||
|
return "Total Footnotes"
|
||||||
|
case MetricCounter.citations:
|
||||||
|
return "Total Citations"
|
||||||
|
case MetricCounter.pages:
|
||||||
|
return "Total Pages"
|
||||||
case MetricCounter.files:
|
case MetricCounter.files:
|
||||||
return "Total Notes"
|
return "Total Notes"
|
||||||
}
|
}
|
||||||
|
@ -181,6 +199,9 @@
|
||||||
<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}>Citations</option>
|
||||||
|
<option value={MetricCounter.pages}>Pages</option>
|
||||||
<option value={MetricCounter.files}>Files</option>
|
<option value={MetricCounter.files}>Files</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
@ -348,6 +369,9 @@
|
||||||
<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}>Citations</option>
|
||||||
|
<option value={MetricCounter.pages}>Pages</option>
|
||||||
<option value={MetricCounter.files}>Files</option>
|
<option value={MetricCounter.files}>Files</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -9,18 +9,27 @@ export interface Day {
|
||||||
words: number;
|
words: number;
|
||||||
characters: number;
|
characters: number;
|
||||||
sentences: number;
|
sentences: number;
|
||||||
|
pages: 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;
|
||||||
|
totalPages: 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;
|
||||||
|
pages: CountDiff;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CountDiff {
|
export interface CountDiff {
|
||||||
|
|
|
@ -1,23 +1,30 @@
|
||||||
import { debounce, Debouncer, TFile, Vault, Workspace } from "obsidian";
|
import { debounce, Debouncer, TFile, Vault, Workspace } from "obsidian";
|
||||||
|
import type BetterWordCount from "../main";
|
||||||
import { STATS_FILE } from "../constants";
|
import { STATS_FILE } from "../constants";
|
||||||
import type { Day, VaultStatistics } from "./Stats";
|
import type { Day, VaultStatistics } from "./Stats";
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import {
|
import {
|
||||||
getCharacterCount,
|
getCharacterCount,
|
||||||
getSentenceCount,
|
getSentenceCount,
|
||||||
|
getPageCount,
|
||||||
getWordCount,
|
getWordCount,
|
||||||
|
getCitationCount,
|
||||||
|
getFootnoteCount,
|
||||||
|
cleanComments,
|
||||||
} from "../utils/StatUtils";
|
} from "../utils/StatUtils";
|
||||||
|
|
||||||
export default class StatsManager {
|
export default class StatsManager {
|
||||||
private vault: Vault;
|
private vault: Vault;
|
||||||
private workspace: Workspace;
|
private workspace: Workspace;
|
||||||
|
private plugin: BetterWordCount;
|
||||||
private vaultStats: VaultStatistics;
|
private vaultStats: VaultStatistics;
|
||||||
private today: string;
|
private today: string;
|
||||||
public debounceChange;
|
public debounceChange;
|
||||||
|
|
||||||
constructor(vault: Vault, workspace: Workspace) {
|
constructor(vault: Vault, workspace: Workspace, plugin: BetterWordCount) {
|
||||||
this.vault = vault;
|
this.vault = vault;
|
||||||
this.workspace = workspace;
|
this.workspace = workspace;
|
||||||
|
this.plugin = plugin;
|
||||||
this.debounceChange = debounce(
|
this.debounceChange = debounce(
|
||||||
(text: string) => this.change(text),
|
(text: string) => this.change(text),
|
||||||
50,
|
50,
|
||||||
|
@ -25,9 +32,17 @@ export default class StatsManager {
|
||||||
);
|
);
|
||||||
|
|
||||||
this.vault.on("rename", (new_name, old_path) => {
|
this.vault.on("rename", (new_name, old_path) => {
|
||||||
const content = this.vaultStats.modifiedFiles[old_path];
|
if (this.vaultStats.modifiedFiles.hasOwnProperty(old_path)) {
|
||||||
delete this.vaultStats.modifiedFiles[old_path];
|
const content = this.vaultStats.modifiedFiles[old_path];
|
||||||
this.vaultStats.modifiedFiles[new_name.path] = content;
|
delete this.vaultStats.modifiedFiles[old_path];
|
||||||
|
this.vaultStats.modifiedFiles[new_name.path] = content;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.vault.on("delete", (deleted_file) => {
|
||||||
|
if (this.vaultStats.modifiedFiles.hasOwnProperty(deleted_file.path)) {
|
||||||
|
delete this.vaultStats.modifiedFiles[deleted_file.path];
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
this.vault.adapter.exists(STATS_FILE).then(async (exists) => {
|
this.vault.adapter.exists(STATS_FILE).then(async (exists) => {
|
||||||
|
@ -68,15 +83,24 @@ 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 totalPages = await this.calcTotalPages();
|
||||||
|
|
||||||
const newDay: Day = {
|
const newDay: Day = {
|
||||||
words: 0,
|
words: 0,
|
||||||
characters: 0,
|
characters: 0,
|
||||||
sentences: 0,
|
sentences: 0,
|
||||||
|
pages: 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,
|
||||||
|
totalPages: totalPages,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.vaultStats.modifiedFiles = {};
|
this.vaultStats.modifiedFiles = {};
|
||||||
|
@ -85,10 +109,17 @@ export default class StatsManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async change(text: string) {
|
public async change(text: string) {
|
||||||
|
if (this.plugin.settings.countComments) {
|
||||||
|
text = cleanComments(text);
|
||||||
|
}
|
||||||
const fileName = this.workspace.getActiveFile().path;
|
const fileName = this.workspace.getActiveFile().path;
|
||||||
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);
|
||||||
|
const currentPages = getPageCount(text, this.plugin.settings.pageWords);
|
||||||
|
|
||||||
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")
|
||||||
|
@ -102,9 +133,19 @@ 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;
|
||||||
|
this.vaultStats.history[this.today].totalPages +=
|
||||||
|
currentPages - modFiles[fileName].pages.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 = currentFootnotes;
|
||||||
|
modFiles[fileName].citations.current = currentCitations;
|
||||||
|
modFiles[fileName].pages.current = currentPages;
|
||||||
} else {
|
} else {
|
||||||
modFiles[fileName] = {
|
modFiles[fileName] = {
|
||||||
words: {
|
words: {
|
||||||
|
@ -119,6 +160,18 @@ export default class StatsManager {
|
||||||
initial: currentSentences,
|
initial: currentSentences,
|
||||||
current: currentSentences,
|
current: currentSentences,
|
||||||
},
|
},
|
||||||
|
footnotes: {
|
||||||
|
initial: currentFootnotes,
|
||||||
|
current: currentFootnotes,
|
||||||
|
},
|
||||||
|
citations: {
|
||||||
|
initial: currentCitations,
|
||||||
|
current: currentCitations,
|
||||||
|
},
|
||||||
|
pages: {
|
||||||
|
initial: currentPages,
|
||||||
|
current: currentPages,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,9 +191,27 @@ export default class StatsManager {
|
||||||
)
|
)
|
||||||
.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);
|
||||||
|
const pages = Object.values(modFiles)
|
||||||
|
.map((counts) =>
|
||||||
|
Math.max(0, counts.pages.current - counts.pages.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].pages = pages;
|
||||||
this.vaultStats.history[this.today].files = this.getTotalFiles();
|
this.vaultStats.history[this.today].files = this.getTotalFiles();
|
||||||
|
|
||||||
await this.update();
|
await this.update();
|
||||||
|
@ -159,6 +230,9 @@ 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();
|
||||||
|
todayHist.totalPages = await this.calcTotalPages();
|
||||||
this.update();
|
this.update();
|
||||||
} else {
|
} else {
|
||||||
this.updateToday();
|
this.updateToday();
|
||||||
|
@ -200,9 +274,46 @@ export default class StatsManager {
|
||||||
sentence += getSentenceCount(await this.vault.cachedRead(file));
|
sentence += getSentenceCount(await this.vault.cachedRead(file));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return sentence;
|
return sentence;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async calcTotalPages(): Promise<number> {
|
||||||
|
let pages = 0;
|
||||||
|
|
||||||
|
const files = this.vault.getFiles();
|
||||||
|
for (const i in files) {
|
||||||
|
const file = files[i];
|
||||||
|
if (file.extension === "md") {
|
||||||
|
pages += getPageCount(await this.vault.cachedRead(file), this.plugin.settings.pageWords);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pages;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
@ -216,6 +327,18 @@ 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 getDailyPages(): number {
|
||||||
|
return this.vaultStats.history[this.today].pages;
|
||||||
|
}
|
||||||
|
|
||||||
public getTotalFiles(): number {
|
public getTotalFiles(): number {
|
||||||
return this.vault.getMarkdownFiles().length;
|
return this.vault.getMarkdownFiles().length;
|
||||||
}
|
}
|
||||||
|
@ -234,4 +357,19 @@ 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getTotalPages(): Promise<number> {
|
||||||
|
if (!this.vaultStats) return await this.calcTotalPages();
|
||||||
|
return this.vaultStats.history[this.today].totalPages;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,10 @@ import {
|
||||||
getWordCount,
|
getWordCount,
|
||||||
getCharacterCount,
|
getCharacterCount,
|
||||||
getSentenceCount,
|
getSentenceCount,
|
||||||
|
getCitationCount,
|
||||||
|
getFootnoteCount,
|
||||||
|
getPageCount,
|
||||||
|
cleanComments,
|
||||||
} from "src/utils/StatUtils";
|
} from "src/utils/StatUtils";
|
||||||
import { debounce } from "obsidian";
|
import { debounce } from "obsidian";
|
||||||
|
|
||||||
|
@ -22,7 +26,7 @@ export default class StatusBar {
|
||||||
);
|
);
|
||||||
|
|
||||||
this.statusBarEl.classList.add("mod-clickable");
|
this.statusBarEl.classList.add("mod-clickable");
|
||||||
this.statusBarEl.setAttribute("aria-label", "Coming Soon");
|
this.statusBarEl.setAttribute("aria-label", "!!!");
|
||||||
this.statusBarEl.setAttribute("aria-label-position", "top");
|
this.statusBarEl.setAttribute("aria-label-position", "top");
|
||||||
this.statusBarEl.addEventListener("click", (ev: MouseEvent) =>
|
this.statusBarEl.addEventListener("click", (ev: MouseEvent) =>
|
||||||
this.onClick(ev)
|
this.onClick(ev)
|
||||||
|
@ -41,6 +45,10 @@ export default class StatusBar {
|
||||||
const sb = this.plugin.settings.statusBar;
|
const sb = this.plugin.settings.statusBar;
|
||||||
let display = "";
|
let display = "";
|
||||||
|
|
||||||
|
if (!this.plugin.settings.countComments) {
|
||||||
|
text = cleanComments(text);
|
||||||
|
}
|
||||||
|
|
||||||
for (let i = 0; i < sb.length; i++) {
|
for (let i = 0; i < sb.length; i++) {
|
||||||
const sbItem = sb[i];
|
const sbItem = sb[i];
|
||||||
|
|
||||||
|
@ -107,6 +115,66 @@ export default class StatusBar {
|
||||||
: 0));
|
: 0));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} else if (metric.counter === MetricCounter.footnotes) {
|
||||||
|
switch (metric.type) {
|
||||||
|
case MetricType.file:
|
||||||
|
display = display + getFootnoteCount(text);
|
||||||
|
break;
|
||||||
|
case MetricType.daily:
|
||||||
|
display =
|
||||||
|
display +
|
||||||
|
(this.plugin.settings.collectStats
|
||||||
|
? this.plugin.statsManager.getDailyFootnotes()
|
||||||
|
: 0);
|
||||||
|
break;
|
||||||
|
case MetricType.total:
|
||||||
|
display =
|
||||||
|
display +
|
||||||
|
(await (this.plugin.settings.collectStats
|
||||||
|
? this.plugin.statsManager.getTotalFootnotes()
|
||||||
|
: 0));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (metric.counter === MetricCounter.citations) {
|
||||||
|
switch (metric.type) {
|
||||||
|
case MetricType.file:
|
||||||
|
display = display + getCitationCount(text);
|
||||||
|
break;
|
||||||
|
case MetricType.daily:
|
||||||
|
display =
|
||||||
|
display +
|
||||||
|
(this.plugin.settings.collectStats
|
||||||
|
? this.plugin.statsManager.getDailyCitations()
|
||||||
|
: 0);
|
||||||
|
break;
|
||||||
|
case MetricType.total:
|
||||||
|
display =
|
||||||
|
display +
|
||||||
|
(await (this.plugin.settings.collectStats
|
||||||
|
? this.plugin.statsManager.getTotalCitations()
|
||||||
|
: 0));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (metric.counter === MetricCounter.pages) {
|
||||||
|
switch (metric.type) {
|
||||||
|
case MetricType.file:
|
||||||
|
display = display + getPageCount(text, this.plugin.settings.pageWords);
|
||||||
|
break;
|
||||||
|
case MetricType.daily:
|
||||||
|
display =
|
||||||
|
display +
|
||||||
|
(this.plugin.settings.collectStats
|
||||||
|
? this.plugin.statsManager.getDailyPages()
|
||||||
|
: 0);
|
||||||
|
break;
|
||||||
|
case MetricType.total:
|
||||||
|
display =
|
||||||
|
display +
|
||||||
|
(await (this.plugin.settings.collectStats
|
||||||
|
? this.plugin.statsManager.getTotalPages()
|
||||||
|
: 0));
|
||||||
|
break;
|
||||||
|
}
|
||||||
} else if (metric.counter === MetricCounter.files) {
|
} else if (metric.counter === MetricCounter.files) {
|
||||||
switch (metric.type) {
|
switch (metric.type) {
|
||||||
case MetricType.file:
|
case MetricType.file:
|
||||||
|
@ -209,6 +277,66 @@ export default class StatusBar {
|
||||||
: 0));
|
: 0));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} else if (metric.counter === MetricCounter.footnotes) {
|
||||||
|
switch (metric.type) {
|
||||||
|
case MetricType.file:
|
||||||
|
display = display + 0;
|
||||||
|
break;
|
||||||
|
case MetricType.daily:
|
||||||
|
display =
|
||||||
|
display +
|
||||||
|
(this.plugin.settings.collectStats
|
||||||
|
? this.plugin.statsManager.getDailyFootnotes()
|
||||||
|
: 0);
|
||||||
|
break;
|
||||||
|
case MetricType.total:
|
||||||
|
display =
|
||||||
|
display +
|
||||||
|
(await (this.plugin.settings.collectStats
|
||||||
|
? this.plugin.statsManager.getTotalFootnotes()
|
||||||
|
: 0));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (metric.counter === MetricCounter.citations) {
|
||||||
|
switch (metric.type) {
|
||||||
|
case MetricType.file:
|
||||||
|
display = display + 0;
|
||||||
|
break;
|
||||||
|
case MetricType.daily:
|
||||||
|
display =
|
||||||
|
display +
|
||||||
|
(this.plugin.settings.collectStats
|
||||||
|
? this.plugin.statsManager.getDailyCitations()
|
||||||
|
: 0);
|
||||||
|
break;
|
||||||
|
case MetricType.total:
|
||||||
|
display =
|
||||||
|
display +
|
||||||
|
(await (this.plugin.settings.collectStats
|
||||||
|
? this.plugin.statsManager.getTotalCitations()
|
||||||
|
: 0));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (metric.counter === MetricCounter.pages) {
|
||||||
|
switch (metric.type) {
|
||||||
|
case MetricType.file:
|
||||||
|
display = display + 0;
|
||||||
|
break;
|
||||||
|
case MetricType.daily:
|
||||||
|
display =
|
||||||
|
display +
|
||||||
|
(this.plugin.settings.collectStats
|
||||||
|
? this.plugin.statsManager.getDailyPages()
|
||||||
|
: 0);
|
||||||
|
break;
|
||||||
|
case MetricType.total:
|
||||||
|
display =
|
||||||
|
display +
|
||||||
|
(await (this.plugin.settings.collectStats
|
||||||
|
? this.plugin.statsManager.getTotalPages()
|
||||||
|
: 0));
|
||||||
|
break;
|
||||||
|
}
|
||||||
} else if (metric.counter === MetricCounter.files) {
|
} else if (metric.counter === MetricCounter.files) {
|
||||||
switch (metric.type) {
|
switch (metric.type) {
|
||||||
case MetricType.file:
|
case MetricType.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(
|
||||||
|
@ -37,6 +54,10 @@ export function getSentenceCount(text: string): number {
|
||||||
return sentences;
|
return sentences;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getPageCount(text: string, pageWords: number): number {
|
||||||
|
return parseFloat((getWordCount(text) / pageWords).toFixed(1));
|
||||||
|
}
|
||||||
|
|
||||||
export function getTotalFileCount(vault: Vault): number {
|
export function getTotalFileCount(vault: Vault): number {
|
||||||
return vault.getMarkdownFiles().length;
|
return vault.getMarkdownFiles().length;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue