-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.js
More file actions
101 lines (85 loc) · 3.02 KB
/
Utils.js
File metadata and controls
101 lines (85 loc) · 3.02 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
// 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 getRandomItemWithIndex(arr) {
const randomIndex = Math.floor(Math.random() * arr.length);
return { item: arr[randomIndex], index: randomIndex };
}
function buildObsidianOpenFileURI(filePath, lineNumber = 1) {
return (
`obsidian://adv-uri?` +
`filepath=${encodeURIComponent(filePath)}&` +
`viewmode=live&` +
`openmode=true&` +
`line=${lineNumber}&` +
`commandid=${encodeURIComponent("editor:unfold-all")}`
);
}
function getAllFilesByExtension(folderPath, fileExtension) {
let fm = FileManager.iCloud();
let files = fm.listContents(folderPath);
let matchedFiles = [];
files.forEach((file) => {
let fullPath = fm.joinPath(folderPath, file);
if (fm.fileExists(fullPath)) {
if (fm.isDirectory(fullPath)) {
matchedFiles = matchedFiles.concat(
getAllFilesByExtension(fullPath, fileExtension)
);
} else if (file.endsWith(fileExtension)) {
matchedFiles.push(fullPath);
}
}
});
return matchedFiles;
}
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();
}
function truncateText(text, maxLength = 180) {
if (typeof text !== "string") return text;
if (text.length > maxLength) {
return text.slice(0, maxLength) + "…";
}
return text;
}
// 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,
getRandomItemWithIndex,
buildObsidianOpenFileURI,
getAllFilesByExtension,
convertMarkdownToPlainText,
truncateText,
getRepoTree,
getFileContent
};