Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
An assistant for markdown writers.

For now there is only one feature:
- Upload images from the clipboard automatically.
- Upload images or image files from the clipboard automatically.

## Upload images from the clipboard

Expand All @@ -29,3 +29,11 @@ How?
3. It's uploading now. Wait for secs.
4. See preview for the uploaded image and maybe add a title for it.
5. Press `enter` to insert the image.

### Uploaders

Now we have the following uploaders

* [qiniu-uploader](https://github.com/knightli/qiniu-uploader) for qiniu cloud
* [qcloud-uploader](https://github.com/tychenjiajun/qcloud-uploader) for tencent qcloud
* [oss-uploader](https://github.com/airclear/oss-uploader) for aliyun oss
27 changes: 25 additions & 2 deletions lib/main.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
insertImageViewModule = require "./insert-image-view"
electron = require 'electron'
fs = require 'fs'
isGif = require 'is-gif'

module.exports =
config:
Expand Down Expand Up @@ -32,7 +35,22 @@ module.exports =
if (e.metaKey && e.keyCode == 86 || e.ctrlKey && e.keyCode == 86)
clipboard = require('clipboard')
img = clipboard.readImage()
return if img.isEmpty()
ext = 'png'
if img.isEmpty()
potentialFilePath = clipboard.readText()
if not fs.existsSync(potentialFilePath)
return
ext = potentialFilePath.split('.').pop().toLowerCase()
img = electron.nativeImage.createFromPath(potentialFilePath)
if img.isEmpty()
try
img = fs.readFileSync(potentialFilePath) # read Buffer from file
catch error
return # omit it to avoid throw error in the console
if not isGif(img)
return # normaly return, paste whatever you want

e.preventDefault()

# insert loading text
uploaderName = atom.config.get('markdown-assistant.uploader')
Expand All @@ -54,7 +72,12 @@ module.exports =
uploaderIns = uploader.instance()

uploadFn = (callback)->
uploaderIns.upload(img.toPng(), 'png', callback)
if img instanceof Buffer
uploaderIns.upload(img, 'gif', callback) # only gif can be Buffer now
else if new Set(['jpg', 'jpeg', 'jpe', 'jif', 'jfif', 'jfi']).has(ext)
uploaderIns.upload(img.toJPEG(100), ext, callback)
else
uploaderIns.upload(img.toPNG(), ext, callback)

insertImageViewInstance = new insertImageViewModule()
insertImageViewInstance.display(uploadFn)
Expand Down
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@
"email": "knightli@foxmail.com",
"url": "https://github.com/knightli"
},
"contributors": [
{
"name": "Jiajun Chen",
"email": "tychenjiajun@live.cn",
"url": "https://deeloves.me/"
}
],
"repository": "https://github.com/knightli/markdown-assistant",
"license": "MIT",
"engines": {
"atom": ">=1.0.0 <2.0.0"
},
"dependencies": {
"atom-space-pen-views": "^2.0.3",
"qiniu": "^6.1.8"
"qiniu": "^6.1.8",
"is-gif": "^2.0.0"
}
}