Repository navigation
Expand file tree
/
Copy pathsentencestoplottertime.ts
More file actions
129 lines (102 loc) · 3.58 KB
/
Copy pathsentencestoplottertime.ts
File metadata and controls
129 lines (102 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { editor, system } from "@silverbulletmd/silverbullet/syscalls";
const defaultConfig = {
seconds_per_sentence: 20,
milliseconds_per_word: 2000,
ignore_headings: true,
ignore_lists: true,
start_regex: '',
end_regex: ''
};
export async function showPlotterTime() {
let content = await getContent();
let countSentences = await countSentencesInParagraphs(content);
let countWords = await countWordsInParagraphs(content);
const config = await getConfig();
let seconds_sentences = countSentences * config.seconds_per_sentence;
let seconds_words = countWords * config.milliseconds_per_word;
await editor.flashNotification("Plotter runtime: " + formatTime(seconds_sentences, false) + " min [by sentence] or " + formatTime(seconds_words, true) + " min [by words].", "info");
}
async function getContent() {
const config = await getConfig();
const selection = await editor.getSelection();
let content = await editor.getText();
// there is a selection
if (selection.from != selection.to) {
content = content.slice(selection.from, selection.to);
// there is no selection
} else {
content = getStringBetweenRegexes(content, config.start_regex, config.end_regex);
};
return content;
}
function getStringBetweenRegexes(content, start_regex_str, end_regex_str) {
let start = 0;
let start_regex = new RegExp(start_regex_str, 'g');
let start_match = start_regex.exec(content);
let start_found_str = '';
if (start_match) {
start = start_match.index;
start_found_str = start_match[0];
}
let end = content.length;
let end_regex = new RegExp(end_regex_str, 'gm');
let end_match = end_regex.exec(content.slice(start + start_found_str.length));
if (end_match) end = start + start_found_str.length + end_match.index;
let new_content = content.slice(start, end);
return new_content;
}
async function getConfig() {
return {...defaultConfig, ...await system.getSpaceConfig("sentencesToPlotterTime", defaultConfig)};
}
async function countSentencesInParagraphs(content: string): number {
let totalSentences = 0;
let sentencesRegex = /[^.!?]+[.!?]*/g;
let ignore_character_group = '';
const config = await getConfig();
if (config.ignore_headings) {
ignore_character_group += '#';
}
if (config.ignore_lists) {
ignore_character_group += '*-';
}
let ignore_regex = new RegExp(`^\s*[${ignore_character_group}]`);
content.split("\n").forEach(line => {
let trimmedLine = line.trim();
if (!ignore_regex.test(trimmedLine)) {
let sentences = trimmedLine.match(sentencesRegex) || [];
totalSentences += sentences.length;
}
});
return totalSentences;
}
async function countWordsInParagraphs(content: string): number {
let totalWords = 0;
let wordRegex = /\b\w+\b/g;
let ignore_character_group = '';
const config = await getConfig();
if (config.ignore_headings) {
ignore_character_group += '#';
}
if (config.ignore_lists) {
ignore_character_group += '*-';
}
let ignore_regex = new RegExp(`^\s*[${ignore_character_group}]`);
content.split("\n").forEach(line => {
let trimmedLine = line.trim();
if (!ignore_regex.test(trimmedLine)) {
let words = trimmedLine.match(wordRegex) || [];
totalWords += words.length;
}
});
return totalWords;
}
function formatTime(seconds, milliseconds) {
if (milliseconds) {
seconds = seconds / 1000;
}
let minutes = Math.floor(seconds / 60);
let remainingSeconds = seconds % 60;
let formattedMinutes = String(minutes);
let formattedSeconds = String(remainingSeconds).padStart(2, "0");
return `${formattedMinutes}:${formattedSeconds}`;
}