-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget_dear-today-me.js
More file actions
59 lines (44 loc) · 1.62 KB
/
widget_dear-today-me.js
File metadata and controls
59 lines (44 loc) · 1.62 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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: gray; icon-glyph: smile-wink;
// 📁 https://github.com/huaminghuangtw/Dear-Today-Me
const CONFIG = {
FONT: { NAME: "IowanOldStyle-BoldItalic", SIZE: 18 },
MINIMUM_SCALE_FACTOR: 0.1,
TEXT_OPACITY: 1,
TEXT_COLOR: Color.white()
};
const Utils = importModule("Utils");
const Cache = importModule("Cache");
let cache = new Cache(Script.name());
let randomParagraph = await cache.getOrFetch(fetchRandomParagraph);
let widget = await createWidget(randomParagraph);
config.runsInWidget ? Script.setWidget(widget) : widget.presentMedium();
Script.complete();
// ================
// Helper functions
// ================
async function fetchRandomParagraph() {
let fileContent = await Utils.getFileContent(
"huaminghuangtw",
"Dear-Today-Me",
"index.md"
);
let allParagraphs = fileContent.split("\n\n");
// Skip salutation and closing lines
let selectedParagraphs = allParagraphs.slice(1, allParagraphs.length - 2);
let randomParagraph = Utils.convertMarkdownToPlainText(
Utils.getRandomItem(selectedParagraphs)
);
return randomParagraph;
}
async function createWidget(plainTextFromMarkdown) {
let widget = new ListWidget();
let text = widget.addText(Utils.truncateText(plainTextFromMarkdown));
text.centerAlignText();
text.font = new Font(CONFIG.FONT.NAME, CONFIG.FONT.SIZE);
text.minimumScaleFactor = CONFIG.MINIMUM_SCALE_FACTOR;
text.textOpacity = CONFIG.TEXT_OPACITY;
text.textColor = CONFIG.TEXT_COLOR;
return widget;
}