forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.js
More file actions
121 lines (110 loc) · 3.16 KB
/
paths.js
File metadata and controls
121 lines (110 loc) · 3.16 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import fs from 'fs'
import path from 'path'
import { isFile, isFile2, isSymbolicLink } from './index'
const isOsx = process.platform === 'darwin'
export const MARKDOWN_EXTENSIONS = Object.freeze([
'markdown',
'mdown',
'mkdn',
'md',
'mkd',
'mdwn',
'mdtxt',
'mdtext',
'text',
'txt'
])
export const MARKDOWN_INCLUSIONS = Object.freeze(MARKDOWN_EXTENSIONS.map(x => '*.' + x))
export const IMAGE_EXTENSIONS = Object.freeze([
'jpeg',
'jpg',
'png',
'gif',
'svg',
'webp'
])
/**
* Returns true if the filename matches one of the markdown extensions.
*
* @param {string} filename Path or filename
*/
export const hasMarkdownExtension = filename => {
if (!filename || typeof filename !== 'string') return false
return MARKDOWN_EXTENSIONS.some(ext => filename.toLowerCase().endsWith(`.${ext}`))
}
/**
* Returns true if the path is an image file.
*
* @param {string} filepath The path
*/
export const isImageFile = filepath => {
const extname = path.extname(filepath)
return isFile(filepath) && IMAGE_EXTENSIONS.some(ext => {
const EXT_REG = new RegExp(ext, 'i')
return EXT_REG.test(extname)
})
}
/**
* Returns true if the path is a markdown file or symbolic link to a markdown file.
*
* @param {string} filepath The path or link path.
*/
export const isMarkdownFile = filepath => {
if (!isFile2(filepath)) return false
// Check symbolic link.
if (isSymbolicLink(filepath)) {
const targetPath = path.resolve(path.dirname(filepath), fs.readlinkSync(filepath))
return isFile(targetPath) && hasMarkdownExtension(targetPath)
}
return hasMarkdownExtension(filepath)
}
/**
* Check if the both paths point to the same file.
*
* @param {string} pathA The first path.
* @param {string} pathB The second path.
* @param {boolean} [isNormalized] Are both paths already normalized.
*/
export const isSamePathSync = (pathA, pathB, isNormalized = false) => {
if (!pathA || !pathB) return false
const a = isNormalized ? pathA : path.normalize(pathA)
const b = isNormalized ? pathB : path.normalize(pathB)
if (a.length !== b.length) {
return false
} else if (a === b) {
return true
} else if (a.toLowerCase() === b.toLowerCase()) {
try {
const fiA = fs.statSync(a)
const fiB = fs.statSync(b)
return fiA.ino === fiB.ino
} catch (_) {
// Ignore error
}
}
return false
}
/**
* Check whether a file or directory is a child of the given directory.
*
* @param {string} dir The parent directory.
* @param {string} child The file or directory path to check.
*/
export const isChildOfDirectory = (dir, child) => {
if (!dir || !child) return false
const relative = path.relative(dir, child)
return relative && !relative.startsWith('..') && !path.isAbsolute(relative)
}
export const getResourcesPath = () => {
let resPath = process.resourcesPath
if (process.env.NODE_ENV === 'development') {
// Default locations:
// Linux/Windows: node_modules/electron/dist/resources/
// macOS: node_modules/electron/dist/Electron.app/Contents/Resources
if (isOsx) {
resPath = path.join(resPath, '../..')
}
resPath = path.join(resPath, '../../../../resources')
}
return resPath
}