-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget_quote-from-Weekly-Mindware-Update.js
More file actions
83 lines (65 loc) · 2.05 KB
/
Copy pathwidget_quote-from-Weekly-Mindware-Update.js
File metadata and controls
83 lines (65 loc) · 2.05 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
// 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/Weekly-Mindware-Update
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 randomQuote = await cache.getOrFetch(fetchRandomQuote);
let widget = await createWidget(randomQuote);
config.runsInWidget ? Script.setWidget(widget) : widget.presentMedium();
Script.complete();
// ================
// Helper functions
// ================
async function fetchRandomQuote() {
let files = await Utils.getRepoTree(
"huaminghuangtw",
"Weekly-Mindware-Update",
);
let today = new Date();
let df = new DateFormatter();
df.dateFormat = "w";
let weekNumber = df.string(today);
let filePath = Utils.getRandomItem(
files.filter((f) => f.path.includes(`w${weekNumber}`) && f.path.endsWith(".md")),
).path;
let fileContent = await Utils.getFileContent(
"huaminghuangtw",
"Weekly-Mindware-Update",
filePath,
);
let sectionContent = fileContent
.split("\n")
.filter((line) => line.startsWith("*"))
.map((line) => line.slice(1).trim())
.slice(0, 5);
let randomQuote = Utils.convertMarkdownToPlainText(
Utils.getRandomItem(sectionContent),
);
return randomQuote;
}
async function createWidget(t) {
let widget = new ListWidget();
let text = widget.addText(t);
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;
// 👉 Download this shortcut: https://shortcutomation.com/add-to-inbox
widget.url =
`shortcuts://run-shortcut?` +
`name=${encodeURIComponent("📥 Add to Inbox")}&` +
`input=${encodeURIComponent(t)}`;
return widget;
}