Skip to content

Commit

Permalink
upload images
Browse files Browse the repository at this point in the history
  • Loading branch information
zhugy08 committed Jan 25, 2021
1 parent e946c9e commit 1d6bdbd
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
- Initial release
- 完成目录更新、显示、编辑和保存功能
- 完成markdown右键菜单功能
- 未完成:图片上传和拖动编辑
- 未完成:图片上传和拖动编辑

## [1.1.0]

- 完成图片上传功能
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gitbook编辑器总是存在这样或那样的问题,使得编辑过程不是
* 插入: 引用、公式、图片
* 显示markdown基本语法帮助
* 显示预览
* 图片上传(待完成)
* 图片上传

## 安装方法

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "gitbook-explorer",
"displayName": "gitbook-explorer",
"description": "vscode plugin for gitbook",
"version": "1.0.0",
"version": "1.1.0",
"publisher": "weixingmayi",
"license": "BSD license",
"repository": "https://github.com/zhugy08/gitbook-explorer",
Expand Down Expand Up @@ -178,7 +178,7 @@
"properties": {
"gitbook-explorer.uploadDirectory":{
"type":"string",
"default": "images",
"default": "",
"description": "Set the directory for uploaded images. The name will be used as a relative path from the editing file except when it is a absolute path."
}
}
Expand Down
59 changes: 54 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import { fstat } from 'fs';
import {mkdirSync, copyFileSync, fstat, existsSync} from 'fs';
import * as vscode from 'vscode';
import { Item } from './summary';
import * as summary from './summary_list';
Expand Down Expand Up @@ -145,10 +145,59 @@ export function activate(context: vscode.ExtensionContext) {
if(uri){
//如果不再在当前目录就上传
//path.relative()
let relpath = "";
insertSnippet(new vscode.SnippetString(
"![${1:alt-text}]("+relpath+")"
));
let imagePath = "";
let imagePathConfig = vscode.workspace.getConfiguration().get('gitbook-explorer.uploadDirectory');
if(imagePathConfig){
imagePath = imagePathConfig as string;
imagePath = imagePath.trim();
console.log("Image path setting: " + imagePath);
}

let filePathName = vscode.window.activeTextEditor?.document.uri.fsPath;
if (filePathName) {
let filename = path.basename(filePathName);
let pathDir = filePathName.substring(0, filePathName.length - filename.length);
let ext = filename?.lastIndexOf('.');
if (ext) {
filename = filename?.substr(0, ext);
}
console.log("path: " + pathDir);

if (!path.isAbsolute(imagePath)) {

if (pathDir && filename) {
if (imagePath.length === 0) {
imagePath = path.join(pathDir, filename);
}
else {
imagePath = path.join(pathDir, imagePath);
}
}
}
else {
console.log("Error file path name");
}

console.log("Upload image path: " + imagePath);

let fileName = uri[0].fsPath;
let sFileName = path.basename(fileName);
let targetFileName = path.join(imagePath, sFileName);
if (uri[0].fsPath !== targetFileName) {
if(!existsSync(imagePath)){
mkdirSync(imagePath);
}
copyFileSync(fileName, targetFileName);
}

if (pathDir) {
let relpath = path.relative(pathDir, targetFileName);
insertSnippet(new vscode.SnippetString(
"![${1:alt-text}](" + relpath + ")"
));
}
}

}
});
break;
Expand Down

0 comments on commit 1d6bdbd

Please sign in to comment.