-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (66 loc) · 2.5 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use babel'
import path from 'path'
const imageStyle = `max-width: 100%;`
const imageContainerStyle = `text-align: center; padding: 5px; display: none;`
const allMarkdownImagesRegExp = /^!\[[^\]\n]*\]\([^)\n]+\)$/g
const markdownImageLinkCaptureGroupRegExp = /^!\[[^\]\n]*\]\(([^)\n]+)\)$/
const networkPathRegExp = /^(?:[a-z]+:)?\/\//i
const strInside = 'inside'
const strBefore = 'before'
const strBlock = 'block'
const strDiv = 'div'
const strImg = 'img'
const arrGithubMarkdown = ['GitHub Markdown','Markdown']
const objIsImiMarker = {isImiMarker: true}
const objInvalidate = {invalidate: strInside}
export default {
processTextBuffer(editor) {
const currentMarkers = editor.getMarkers()
const currentValidMarkers = []
currentMarkers.forEach((marker) => {
if (isImiMarker(marker)) {
if (!marker.isValid()) {
marker.destroy()
} else {
currentValidMarkers.push(marker)
}
}
})
return editor.scan(allMarkdownImagesRegExp, (hit) => {
const hitIsMarked = !!currentValidMarkers.find((marker) => {
return hit.computedRange.start.row == marker.getBufferRange().start.row
})
if (!hitIsMarked) {
const link = hit.matchText.match(markdownImageLinkCaptureGroupRegExp)[1]
const finalLink = (isNetworkPath(link) || path.isAbsolute(link)) ?
link :
path.join(path.dirname(editor.buffer.file.path), link)
const imageContainer = document.createElement(strDiv)
const image = document.createElement(strImg)
const marker = editor.markBufferRange(hit.computedRange, objInvalidate)
imageContainer.appendChild(image)
imageContainer.style = imageContainerStyle
image.style = imageStyle
image.src = finalLink
image.onload = () => imageContainer.style.display = strBlock
marker.bufferMarker.setProperties(objIsImiMarker)
return editor.decorateMarker(marker, {type: strBlock, item: imageContainer, position: strBefore})
}
})
},
activate() {
atom.workspace.observeTextEditors((editor) => {
// if (editor.getGrammar().name === arrGithubMarkdown) {
if (arrGithubMarkdown.includes(editor.getGrammar().name)) {
this.processTextBuffer(editor)
editor.onDidStopChanging(this.processTextBuffer.bind(null, editor))
}
})
}
}
function isImiMarker(marker) {
return marker.bufferMarker && marker.bufferMarker.properties.isImiMarker
}
function isNetworkPath(path) {
return networkPathRegExp.test(path)
}