forked from almariah/embed-code-file
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
77 lines (63 loc) · 1.71 KB
/
utils.ts
File metadata and controls
77 lines (63 loc) · 1.71 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
import path from "path";
export function pathJoin(dir: string, subpath: string): string {
const result = path.join(dir, subpath);
// it seems that obsidian do not understand paths with backslashes in Windows, so turn them into forward slashes
return result.replace(/\\/g, "/");
}
export function analyseSrcLines(str: string): number[] {
str = str.replace(/\s*/g, "")
const result: number[] = []
let strs = str.split(",")
strs.forEach(it => {
if(/\w+-\w+/.test(it)) {
let left = Number(it.split('-')[0])
let right = Number(it.split('-')[1])
for(let i = left; i <= right; i++) {
result.push(i)
}
result.push(0) // three dots
} else {
result.push(Number(it))
result.push(0) // three dots
}
})
return result
}
export function extractSrcLines(fullSrc: string, srcLinesNum: number[]): string {
let src = ""
const fullSrcLines = fullSrc.split("\n")
const fullSrcLinesLen = fullSrcLines.length
srcLinesNum.forEach((lineNum, index, arr) => {
if (lineNum > fullSrcLinesLen) {
arr.splice(index, 1);
}
});
srcLinesNum.forEach((lineNum, index, arr) => {
if (lineNum == 0 && arr[index-1] == 0) {
arr.splice(index, 1);
}
});
srcLinesNum.forEach((lineNum, index) => {
if (lineNum > fullSrcLinesLen) {
return
}
if (index == srcLinesNum.length-1 && lineNum == 0 && srcLinesNum[index-1] == fullSrcLinesLen) {
return
}
if (index == 0 && lineNum != 1) {
src = '...' + '\n' + fullSrcLines[lineNum-1]
return
}
// zeros is dots (analyseSrcLines)
if (lineNum == 0 ) {
src = src + '\n' + '...'
return
}
if (index == 0) {
src = fullSrcLines[lineNum-1]
} else {
src = src + '\n' + fullSrcLines[lineNum-1]
}
});
return src
}