-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
124 lines (110 loc) · 2.87 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
// 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 JSON5 = require('json5');
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
function getType(p) {
let str = Object.prototype.toString.call(p);
let arr = str.substring(1, str.length - 1).split(" ");
let type = arr[1].toLowerCase();
return type;
}
function isObjectOrArray(type) {
return type == "object" || type == "array";
}
function getAllFileds(obj) {
if (!obj) {
return [];
}
let res = [];
let type = getType(obj)
if (type == "object") {
let keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
let value = obj[key];
if (!obj.hasOwnProperty(key)) {
continue;
}
let type = getType(value);
res.push({
key,
type
});
if (isObjectOrArray(type)) {
res.push(...getAllFileds(value));
}
}
} else if (type == "array") {
let keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
let value = obj[key];
if (!obj.hasOwnProperty(key)) {
continue;
}
let type = getType(value);
if (isObjectOrArray(type)) {
res.push(...getAllFileds(value));
}
}
}
return res;
}
/**
* @param {string} json
*/
function jsonToMarkdownTable(json) {
let arr = getAllFileds(json);
let res = "";
res += "|字段|类型|必填|名称|描述|\n";
res += "|:----|:---|:----- |:----- |:----- |\n";
for (const item of arr) {
res += `|${item.key} | ${item.type} | | | | \n`;
}
return res;
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let disposable = vscode.commands.registerCommand('editor.jsonToMarkdownTable', function () {
const global = vscode.window;
const activeTextEditor = global.activeTextEditor;
if (!activeTextEditor) {
return;
}
let activeText = activeTextEditor.document.getText(activeTextEditor.selection);
if (!activeText) {
vscode.window.showWarningMessage('未选中任何json文本');
return;
}
let json = "";
try {
let text = activeText.replace(/[\r\n\t\s]/g, "");
console.log(text);
json = JSON5.parse(text);
} catch (error) {
vscode.window.showErrorMessage("选中文本非JSON文本, 请重新选择");
return;
}
try {
let markdownTable = jsonToMarkdownTable(json);
vscode.env.clipboard.writeText(markdownTable)
.then(() => {
vscode.window.showInformationMessage('转换结果已存至粘贴板');
})
} catch (error) {
vscode.window.showErrorMessage("将转换结果至粘贴板失败:" + error);
return;
}
});
context.subscriptions.push(disposable);
}
// This method is called when your extension is deactivated
function deactivate() { }
module.exports = {
activate,
deactivate
}