forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetImageInfo.js
More file actions
35 lines (33 loc) · 1003 Bytes
/
getImageInfo.js
File metadata and controls
35 lines (33 loc) · 1003 Bytes
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
import { isWin } from '../config'
import { findNearestParagraph, getOffsetOfParagraph } from '../selection/dom'
import { tokenizer } from '../parser'
export const getImageInfo = image => {
const paragraph = findNearestParagraph(image)
const raw = image.getAttribute('data-raw')
const offset = getOffsetOfParagraph(image, paragraph)
const tokens = tokenizer(raw)
const token = tokens[0]
token.range = {
start: offset,
end: offset + raw.length
}
return {
key: paragraph.id,
token,
imageId: image.id
}
}
export const correctImageSrc = src => {
if (src) {
// Fix ASCII and UNC paths on Windows (#1997).
if (isWin && /^(?:[a-zA-Z]:\\|[a-zA-Z]:\/).+/.test(src)) {
src = 'file:///' + src.replace(/\\/g, '/')
} else if (isWin && /^\\\\\?\\.+/.test(src)) {
src = 'file:///' + src.substring(4).replace(/\\/g, '/')
} else if (/^\/.+/.test(src)) {
// Also adding file protocol on UNIX.
src = 'file://' + src
}
}
return src
}