-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
63 lines (51 loc) · 2.02 KB
/
utils.js
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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-brown; icon-glyph: toolbox;
module.exports.getRandomItem = (arr) => {
return arr[Math.floor(Math.random() * arr.length)];
};
module.exports.getRandomItemWithIndex = (arr) => {
const randomIndex = Math.floor(Math.random() * arr.length);
return { item: arr[randomIndex], index: randomIndex };
};
module.exports.buildObsidianOpenFileURI = (filePath, lineNumber=1) => {
return (
`obsidian://adv-uri?` +
`filepath=${encodeURIComponent(filePath)}&` +
`viewmode=live&` +
`openmode=true&` +
`line=${lineNumber}&` +
`commandid=${encodeURIComponent("editor:unfold-all")}`
);
};
module.exports.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(module.exports.getAllFilesByExtension(fullPath, fileExtension));
} else if (file.endsWith(fileExtension)) {
matchedFiles.push(fullPath);
}
}
});
return matchedFiles;
};
module.exports.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();
};