Repository navigation
Expand file tree
/
Copy pathUtils.js
More file actions
57 lines (46 loc) · 1.72 KB
/
Copy pathUtils.js
File metadata and controls
57 lines (46 loc) · 1.72 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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: teal; icon-glyph: magic;
function getRandomItem(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
function getRandomIndex(max) {
return Math.floor(Math.random() * max);
}
function convertMarkdownToPlainText(markdown) {
// Convert headings (e.g., "# Heading" to "Heading")
markdown = markdown.replace(/(^|\n)#+\s*(.+)/g, "$2");
// Convert links [text](url) to "text"
markdown = markdown.replace(/\[([^\]]+)\]\([^\)]+\)/g, "$1");
// Remove bold (**text** or __text__)
markdown = markdown.replace(/(\*\*|__)(.*?)\1/g, "$2");
// Remove italic (*text* or _text_)
markdown = markdown.replace(/(\*|_)(.*?)\1/g, "$2");
// Remove inline code `code`
markdown = markdown.replace(/`([^`]+)`/g, "$1");
// Remove code blocks ```code```
markdown = markdown.replace(/```[^`]*```/g, "");
return markdown.trim();
}
// https://docs.github.com/en/rest/git/trees?apiVersion=2022-11-28#get-a-tree
async function getRepoTree(repoOwner, repoName) {
const url = `https://api.github.com/repos/${repoOwner}/${repoName}/git/trees/main?recursive=true`;
let req = new Request(url);
req.headers = {
accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
};
return (await req.loadJSON()).tree;
}
async function getFileContent(repoOwner, repoName, filePath) {
let url = `https://raw.githubusercontent.com/${repoOwner}/${repoName}/main/${filePath}`;
let req = new Request(url);
return await req.loadString();
}
module.exports = {
getRandomItem,
getRandomIndex,
convertMarkdownToPlainText,
getRepoTree,
getFileContent
};