generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.ts
More file actions
62 lines (58 loc) · 1.24 KB
/
main.ts
File metadata and controls
62 lines (58 loc) · 1.24 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
import {Editor, MarkdownView, Notice, Plugin} from 'obsidian';
const prettier = require("prettier");
const plugins = [
require("prettier/parser-babel"),
require("prettier/parser-html"),
require("prettier/parser-yaml"),
require("prettier/parser-graphql"),
require("prettier/parser-typescript")
];
interface Formatter {
name: string;
parser: string;
}
export default class FormatCodePlugin extends Plugin {
async onload() {
let supportedFormats: Formatter[] = [
{
name: "JSON",
parser: "json"
},
{
name: "YAML",
parser: "yaml"
},
{
name: "HTML",
parser: "html"
},
{
name: "GraphQL",
parser: "graphql"
},
{
name: "TypeScript",
parser: "typescript"
}
];
supportedFormats.forEach(x => {
this.addCommand({
id: 'format-prettier-' + x.name,
name: x.name,
editorCallback: (editor: Editor, view: MarkdownView) => {
try {
const formatted = prettier.format(editor.getSelection(), {
semi: false,
parser: x.parser,
plugins: plugins
});
editor.replaceSelection(formatted);
} catch (e) {
console.log(e);
new Notice("Format: " + e);
}
}
});
});
}
}