-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextension.js
130 lines (116 loc) · 4.95 KB
/
extension.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
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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const { MarkdownBookPreviewServer } = require('./lib/markdown-book-preview-server');
const { MarkdownBookPreviewConvert } = require('./lib/markdown-book-preview-convert');
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
const markdownBookPreviewServer = new MarkdownBookPreviewServer();
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "mdbp-vscode" is now active!');
context.subscriptions.push(vscode.commands.registerCommand('mdbp-vscode.startServer', startServer));
context.subscriptions.push(vscode.commands.registerCommand('mdbp-vscode.stopServer', stopServer));
context.subscriptions.push(vscode.commands.registerCommand('mdbp-vscode.toggle', toggle));
context.subscriptions.push(vscode.commands.registerCommand('mdbp-vscode.toggleVS', toggleVS));
context.subscriptions.push(vscode.commands.registerCommand('mdbp-vscode.exportXML', exportXML));
context.subscriptions.push(
vscode.commands.registerCommand('mdbp-vscode.previewThisCLI', () => {
const htmlfilepath = convertMD2HTML();
if (htmlfilepath) {
callShell(`vivliostyle preview "${htmlfilepath}"`);
}
})
);
context.subscriptions.push(
vscode.commands.registerCommand('mdbp-vscode.buildThisCLI', () => {
const htmlfilepath = convertMD2HTML();
if (htmlfilepath) {
// callShell(`vivliostyle build "${htmlfilepath}"`);
const outputpath = htmlfilepath.replace(/.html$/, '.pdf');
callShell(`vivliostyle build "${htmlfilepath}" -o "${outputpath}"`);
}
})
);
// 自動更新設定(WorkSpace内のファイルが更新され、それがMarkdownであればHTMLを書き出す)
vscode.workspace.onDidSaveTextDocument((event) => {
convertMD2HTML();
});
// サーバーの起動終了
function startServer() {
markdownBookPreviewServer.startWebServer();
}
function stopServer() {
markdownBookPreviewServer.stopWebServer();
}
// 生のHTMLで開く
function toggle(vsmode = false) {
console.log('MarkdownBookPreview was toggled!');
const editor = vscode.window.activeTextEditor;
if (checkEditorPath(editor) === false) return;
// プレビューしたいパスやVSmodeを設定
const mdpath = editor.document.fileName;
console.log(mdpath);
markdownBookPreviewServer.setTargetMarkdownFile(mdpath, vsmode);
}
// Vivliostyleで開く
function toggleVS() {
console.log('Vivliostyle Mode');
toggle(true);
}
// InDesign用のXMLを書き出す
function exportXML() {
console.log('MarkdownBookPreview export XML');
const editor = vscode.window.activeTextEditor;
if (checkEditorPath(editor) === false) return;
// プレビューしたいパスやVSmodeを設定
const mdpath = editor.document.fileName;
markdownBookPreviewServer.exportInDesignXML(mdpath);
}
// Markdownファイルの変換
function convertMD2HTML() {
const editor = vscode.window.activeTextEditor;
if (checkEditorPath(editor) === false) return null;
// プレビューしたいパスやVSmodeを設定
const mdpath = editor.document.fileName.replace(/^[a-z]:/, (d) => d.toUpperCase());
console.log(mdpath);
const homePath = MarkdownBookPreviewServer.searchHomepath(mdpath);
const htmlfilepath = MarkdownBookPreviewConvert.convertMarkdown(mdpath, homePath);
return htmlfilepath;
}
// ターミナルにコマンドを発行する
function callShell(shellcommand) {
const term = vscode.window.activeTerminal?.name === 'vivliostyle-cli-helper' ? vscode.window.activeTerminal : vscode.window.createTerminal('vivliostyle-cli-helper');
term.show();
// PowerShellかつvivliostyleスクリプトの実行時のみ許可が必要
if (vscode.env.shell.includes('powershell') && shellcommand.indexOf('vivliostyle') === 0) {
term.sendText(`PowerShell -ExecutionPolicy RemoteSigned ${shellcommand}`);
} else {
term.sendText(shellcommand);
}
}
// チェック
function checkEditorPath(editor) {
if (editor === null || editor === undefined) return false;
const path = editor.document.fileName;
if (!path) {
vscode.window.showWarningMessage('ファイルを保存してから実行してください');
return false;
}
if (!path.endsWith('.md')) {
vscode.window.showWarningMessage('Mardownファイルではありません');
return false;
}
return true;
}
}
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate,
};