-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
118 lines (100 loc) · 3.22 KB
/
extension.js
File metadata and controls
118 lines (100 loc) · 3.22 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
const { commands, window, Uri } = require("vscode");
const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
const slugify = require("slugify");
const createUrl = (title, product) => {
// TODO: Remove product from title slug
// @ts-ignore
const titleSlug = slugify(title.trim(), {
lower: true
});
// @ts-ignore
const productSlug = slugify(product.trim(), {
lower: true
});
return "/" + [productSlug, titleSlug].join("/") + "/";
};
async function activate(context) {
// const createDraft = async (uri) => {
// }
let disposable = commands.registerCommand(
"extension.createDraft",
async uri => {
// TODO: Default to a user-defined directory
if (!uri) {
uri["path"] = "...";
return;
}
// Ask the user for a title
const title = await window.showInputBox({
placeHolder: "Title",
prompt: "Choose a title for the topic."
});
// Return error if a title hasn't been defined
if (!title) {
window.showErrorMessage("You need to choose a title for the topic.");
return;
}
// Ask the user for a product
const product = await window.showQuickPick(
["Analytics", "Exchange", "Maestro", "Manager", "Workspaces"],
{ placeHolder: "Product" }
);
// Ask the user for a persona
const persona = await window.showQuickPick(
["Form Builder", "Platform Developer", "Template Designer"],
{ placeHolder: "Persona" }
);
// If uri.path is a file path, get its parent directory
const directoryPath = fs.statSync(uri.path).isFile()
? path.dirname(uri.path)
: uri.path;
// Define the directory and file paths
// EXAMPLE:
// title/
// images/
// .keep
// index.md
const topicDirectoryPath = path.join(directoryPath, title);
const topicImagesDirectoryPath = path.join(topicDirectoryPath, "images");
const topicImagesKeepPath = path.join(topicImagesDirectoryPath, ".keep");
const topicPath = path.join(topicDirectoryPath, "index.md");
// Return error if the topic already exists
if (fs.existsSync(topicDirectoryPath)) {
window.showErrorMessage("A topic by that name already exists.");
return;
}
// Create the document
// const document = new Document({ title, product, persona }).markdown();
const document =
"---\n" +
yaml.safeDump({
title: title,
product: product,
persona: persona,
version_available: "",
version_updated: "",
version_deprecated: "",
version_removed: "",
url: createUrl(title, product),
type: "topic"
}) +
"---\n";
// Create the relevant directories and files
fs.mkdirSync(topicDirectoryPath);
fs.mkdirSync(topicImagesDirectoryPath);
fs.writeFileSync(topicImagesKeepPath, "");
fs.writeFileSync(topicPath, document);
// Show the file in the editor
window.showTextDocument(Uri.parse(topicPath));
}
);
context.subscriptions.push(disposable);
}
exports.activate = activate;
function deactivate() {}
module.exports = {
activate,
deactivate
};