generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.ts
145 lines (125 loc) · 4.19 KB
/
main.ts
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
140
141
142
143
144
145
import { Editor, EditorPosition, MarkdownView, Plugin, Vault, Workspace, App } from 'obsidian';
function getCurrentLine(editor: Editor, view: MarkdownView) {
const lineNumber = editor.getCursor().line
const lineText = editor.getLine(lineNumber)
return lineText
}
function prepareTask(line: string) {
line = line.trim()
//remove all leading non-alphanumeric characters
line = line.replace(/^\W+|\W+$/, '')
line = urlEncode(line)
return line
}
function urlEncode(line: string) {
line = encodeURIComponent(line)
return line
}
function createProject(title: string, deepLink: string) {
const project = `things:///add-project?title=${title}¬es=${deepLink}&x-success=obsidian://project-id`
window.open(project);
}
function createTask(line: string, deepLink: string) {
const task = `things:///add?title=${line}¬es=${deepLink}&x-success=obsidian://task-id`
window.open(task);
}
export default class ThingsLink extends Plugin {
async onload() {
this.registerObsidianProtocolHandler("project-id", async (id) => {
const projectID = id['x-things-id'];
const workspace = this.app.workspace;
const view = workspace.getActiveViewOfType(MarkdownView);
if (view == null) {
return;
} else {
const editor = view.editor
const thingsDeepLink = `things:///show?id=${projectID}`;
let fileText = editor.getValue()
const lines = fileText.split('\n');
const h1Index = lines.findIndex(line => line.startsWith('#'));
if (h1Index !== -1) {
let startRange: EditorPosition = {
line: h1Index,
ch:lines[h1Index].length
}
let endRange: EditorPosition = {
line: h1Index,
ch:lines[h1Index].length
}
editor.replaceRange(`\n\n[Things](${thingsDeepLink})`, startRange, endRange);
} else {
let startRange: EditorPosition = {
line: 0,
ch:0
}
let endRange: EditorPosition = {
line: 0,
ch:0
}
editor.replaceRange(`[Things](${thingsDeepLink})\n\n`, startRange, endRange);
}
}
});
this.addCommand({
id: 'create-things-project',
name: 'Create Things Project',
editorCallback: (editor: Editor, view: MarkdownView) => {
const workspace = this.app.workspace;
const fileTitle = workspace.getActiveFile()
if (fileTitle == null) {
return;
} else {
let fileName = urlEncode(fileTitle.name)
fileName = fileName.replace(/\.md$/, '')
const obsidianDeepLink = (this.app as any).getObsidianUrl(fileTitle)
const encodedLink = urlEncode(obsidianDeepLink)
createProject(fileName, encodedLink);
}
}
});
this.registerObsidianProtocolHandler("task-id", async (id) => {
const taskID = id['x-things-id'];
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (view == null) {
return;
} else {
const editor = view.editor
const currentLine = getCurrentLine(editor, view)
const firstLetterIndex = currentLine.search(/[a-zA-Z]|[0-9]/);
const line = currentLine.substring(firstLetterIndex, currentLine.length)
let editorPosition = view.editor.getCursor()
const lineLength = view.editor.getLine(editorPosition.line).length
let startRange: EditorPosition = {
line: editorPosition.line,
ch: firstLetterIndex
}
let endRange: EditorPosition = {
line: editorPosition.line,
ch: lineLength
}
view.editor.replaceRange(`[${line}](things:///show?id=${taskID})`, startRange, endRange);
}
});
this.addCommand({
id: 'create-things-task',
name: 'Create Things Task',
editorCallback: (editor: Editor, view: MarkdownView) => {
const workspace = this.app.workspace;
const fileTitle = workspace.getActiveFile()
if (fileTitle == null) {
return;
} else {
let fileName = urlEncode(fileTitle.name)
fileName = fileName.replace(/\.md$/, '')
const obsidianDeepLink = (this.app as any).getObsidianUrl(fileTitle)
const encodedLink = urlEncode(obsidianDeepLink)
const line = getCurrentLine(editor, view)
const task = prepareTask(line)
createTask(task, encodedLink)
}
}
});
}
onunload() {
}
}