-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
139 lines (126 loc) · 4.61 KB
/
extension.js
File metadata and controls
139 lines (126 loc) · 4.61 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const vscode = require('vscode');
const path = require('path');
const fs = require('fs');
const fetch = require("node-fetch");
const { v4: uuidv4 } = require('uuid');
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
console.log('Aspose Cloud for VSCode is now active!');
let commandSet = [{
'commandId': 'extension.aspose-cloud.pdf',
'callback': async function() {
await convertMarkdown('pdf')
}
},
{
'commandId': 'extension.aspose-cloud.html',
'callback': async function() {
await convertMarkdown('html')
}
},
{
'commandId': 'extension.aspose-cloud.jpg',
'callback': async function() {
await convertMarkdown('jpg')
}
},
{
'commandId': 'extension.aspose-cloud.exportSettings',
'callback': exportSettings
},
];
commandSet.forEach((cmd) => {
context.subscriptions.push(vscode.commands.registerCommand(cmd.commandId, cmd.callback));
});
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
}
const apiURL = 'https://vscode-markdown-converter-750605.conholdate.cloud/api/markdown';
/**
* @param {string} conversionType
*/
async function convertMarkdown(conversionType) {
// check active window
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showWarningMessage('No active editor!');
return;
}
// check markdown mode
if (editor.document.languageId !== 'markdown') {
vscode.window.showWarningMessage("It's not a markdown mode!");
return;
}
let mdfilename = editor.document.uri.fsPath;
let ext = path.extname(mdfilename);
if (!fs.existsSync(mdfilename)) {
if (editor.document.isUntitled) {
vscode.window.showWarningMessage('File not saved. Please, save before converting!');
return;
}
vscode.window.showWarningMessage('Can\'t get a filename!');
return;
};
let data = {
machineId: vscode.env.machineId,
content: editor.document.getText(),
to: conversionType,
paper: vscode.workspace.getConfiguration('aspose-cloud')['paper']
};
// convert and export markdown to pdf, html
try {
let response = await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: "Aspose Cloud for VSCode",
cancellable: false
}, (progress) => {
progress.report({ message: "Conversion in progress..." });
return fetch(apiURL, {
method: 'POST',
cache: 'no-cache',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
});
let outputDirectory = vscode.workspace.getConfiguration('aspose-cloud')['outputDirectory'] || '.';
let outputFileName = mdfilename.replace(ext, '.' + conversionType);
let outputFullPath = (outputDirectory !== '.') ? path.join(outputDirectory, path.basename(outputFileName)) : outputFileName;
let blob = await response.blob();
let readableStream = blob.stream().on('end', () => {
vscode.window.showInformationMessage("File saved: " + outputFullPath);
});
let writableStream = fs.createWriteStream(outputFullPath);
readableStream.pipe(writableStream);
} catch (err) {
vscode.window.showErrorMessage(`Aspose Cloud for VSCode: ${err.message}`);
return;
}
}
/**
* @description Export config to external file
*/
function exportSettings() {
let outputDirectory = vscode.workspace.getConfiguration('aspose-cloud')['outputDirectory'] || '.';
if (outputDirectory === '.') {
outputDirectory = (vscode.workspace.workspaceFolders !== undefined) ?
vscode.workspace.workspaceFolders[0].uri.fsPath :
path.dirname(vscode.window.activeTextEditor.document.uri.fsPath);
}
let configFileName = path.join(outputDirectory, "aspose-html-converter-settings.json");
let jsonContent = JSON.stringify(vscode.workspace.getConfiguration('aspose-cloud'));
fs.writeFile(configFileName, jsonContent, 'utf8', function(err) {
if (err) {
vscode.window.showErrorMessage(`Aspose Cloud for VSCode: ${err.message}`);
return;
}
vscode.window.showInformationMessage(`Saved to: ${configFileName}`);
});
}
module.exports = {
activate,
deactivate
}