generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
120 lines (109 loc) · 3.37 KB
/
utils.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { exec } from 'child_process';
import { Platform } from 'obsidian';
import * as path from 'path';
import { marked } from 'marked';
export function customEncodeURI(uri: string) {
return uri.replace(/[ #&%?]/g, function (c) {
return encodeURIComponent(c);
});
}
export function openFileWithDefaultProgram(filePath: string, onError: (error: Error) => void) {
let command = "";
if (Platform.isWin) {
command = `start "" "${filePath}"`;
} else if (Platform.isMacOS) {
command = `open "${filePath}"`;
} else if (Platform.isLinux) {
command = `xdg-open "${filePath}"`;
}else{
onError(new Error("Unsupported platform to open file"));
}
exec(command, onError);
}
export function getRelativePath(from: string, to: string) {
from = path.normalize(from);
to = path.normalize(to);
const relativePath = path.relative(from, to);
return relativePath.replace(/\\/g, '/');
}
export function getContentType(extname: string) {
extname = extname.toLowerCase();
const _map: { [key: string]: string } = {
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.bmp': 'image/bmp',
'.webp': 'image/webp',
'.pdf': 'application/pdf',
'.mp3': 'audio/mpeg',
'.mp4': 'video/mp4',
'.webm': 'video/webm',
'.ogg': 'audio/ogg',
};
if (_map[extname]) {
return _map[extname];
}
return 'application/octet-stream';
}
export const ImageExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp', '.svg', '.avif'];
export const VideoExtensions = ['.mp4', '.webm', '.mkv', '.mov', '.ogv'];
export const AudioExtensions = ['.mp3', '.ogg', '.wav', '.flac', '.m4a', '.webm'];
export const MarkdownExtensions = ['.md', '.markdown', '.txt'];
export function isImage(fullpath: string) {
const extname = path.extname(fullpath).toLowerCase();
return ImageExtensions.includes(extname);
}
export function isVideo(fullpath: string) {
const extname = path.extname(fullpath).toLowerCase();
return VideoExtensions.includes(extname);
}
export function isAudio(fullpath: string) {
const extname = path.extname(fullpath).toLowerCase();
return AudioExtensions.includes(extname);
}
export function isMarkdown(fullpath: string) {
const extname = path.extname(fullpath).toLowerCase();
return MarkdownExtensions.includes(extname);
}
export function isPDF(fullpath: string) {
const extname = path.extname(fullpath).toLowerCase();
return extname === '.pdf';
}
export function parseUrlParams(params: string|undefined): { [key: string]: string } {
if(!params){
return {};
}
const paramDict: { [key: string]: string } = {};
const paramPairs = params.split('&');
for (const pair of paramPairs) {
const [key, value] = pair.split('=');
if (key && value) {
paramDict[decodeURIComponent(key)] = decodeURIComponent(value);
}
}
return paramDict;
}
export async function extractHeaderSection(markdown: string, header: string) {
if(header === ''){
return marked(markdown);
}
const tokens = marked.lexer(markdown);
let capture = false;
let result = '';
// console.log("header", header);
tokens.forEach((token) => {
// console.log("token", token);
if (token.type === 'heading' && token.text === header) {
capture = true;
} else if (capture && token.type === 'heading') {
capture = false;
} else if (capture) {
result += marked.parser([token]);
}
});
if(result === ''){
return `<p>failed to find header in markdown file: "${header}"</p>`;
}
return result;
}