-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
193 lines (185 loc) · 6.39 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// main.ts
var main_exports = {};
__export(main_exports, {
default: () => PopclipPlugin
});
module.exports = __toCommonJS(main_exports);
var import_obsidian3 = require("obsidian");
// settings.ts
var DEFAULT_SETTINGS = {
mySetting: "default",
actionHeading: "popclip",
action: "advanced-uri"
};
var CUSTOM_SETTINGS = {
useFrontmatter: true,
usePopclipHeading: true,
useHeader: true,
useDatetimeAsFileName: true,
useTable: true
};
var SETTINGS = {
...DEFAULT_SETTINGS,
...CUSTOM_SETTINGS
};
// src/modules/file-writer.ts
var import_obsidian = require("obsidian");
// src/utils/slugify.ts
function slugify(str) {
str = str.replace(/^\s+|\s+$/g, "");
str = str.toLowerCase();
var from = "\u0131\xFC\xF6\xE7\u011F\xE0\xE1\xE4\xE2\xE8\xE9\xEB\xEA\xEC\xED\xEF\xEE\xF2\xF3\xF6\xF4\xF9\xFA\xFC\xFB\xF1\xE7\xB7/_,:;";
var to = "iuocgaaaaeeeeiiiioooouuuunc------";
for (var i = 0, l = from.length; i < l; i++) {
str = str.replace(new RegExp(from.charAt(i), "g"), to.charAt(i));
}
str = str.replace(/[^a-z0-9 -]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-");
if (str.startsWith("-")) {
str = str.substring(1);
} else if (str.endsWith("-")) {
str = str.substring(0, str.length - 1);
}
return str;
}
// src/modules/file-writer.ts
var FileWriter = class {
constructor(app, plugin) {
this.app = app;
this.plugin = plugin;
}
async writeToFile(payload) {
const path = this.normalizePath(payload);
this.app.vault.adapter.write(
(0, import_obsidian.normalizePath)(path),
this.setContent(payload)
);
}
normalizePath(payload) {
let fileName = this.normalizedDate();
if (!this.plugin.settings.useDatetimeAsFileName) {
fileName = payload.title ? payload.title.slice(0, 20).trim() : slugify(payload.clipping.slice(0, 20).trim());
} else {
fileName = this.normalizedDate();
}
fileName = fileName + ".md";
let filePath = decodeURIComponent(payload.path);
const path = (0, import_obsidian.normalizePath)(filePath + "/" + fileName);
return path;
}
normalizedDate() {
const datetime = new Date().toISOString().split(".")[0];
return datetime.replaceAll("-", "").replaceAll(":", "").replace("T", "");
}
setFrontmatter(payload) {
const elements = [
"---",
`title: "${payload == null ? void 0 : payload.title}"`,
`source: ${(payload == null ? void 0 : payload.source) || ""}`,
`date: ${new Date().toISOString()}`,
"---"
];
return elements.join("\n");
}
setHeader(payload) {
return `# ${payload == null ? void 0 : payload.title}
`;
}
setTable(payload) {
const currentDate = new Date().toISOString();
const source = (payload == null ? void 0 : payload.source) || "";
}
setContent(payload) {
const elements = [];
if (this.plugin.settings.useFrontmatter) {
elements.push(this.setFrontmatter(payload));
}
if (this.plugin.settings.useHeader && (payload == null ? void 0 : payload.title)) {
elements.push(this.setHeader(payload));
}
elements.push((payload == null ? void 0 : payload.clipping) || "");
return elements.join("\n");
}
};
// src/modules/settings-tab.ts
var import_obsidian2 = require("obsidian");
var PopclipSettingsTab = class extends import_obsidian2.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
let { containerEl } = this;
containerEl.empty();
new import_obsidian2.Setting(containerEl).setName("Frontmatter").setDesc("Add frontmatter to the top of the file").addToggle((toggle) => {
toggle.setValue(this.plugin.settings.useFrontmatter).onChange(async (value) => {
this.plugin.settings.useFrontmatter = value;
console.log("useFrontmatter", value);
await this.plugin.saveSettings();
});
});
new import_obsidian2.Setting(containerEl).setName("Header").setDesc(
"Use the title of the source page if exists as header of note."
).addToggle((toggle) => {
toggle.setValue(this.plugin.settings.useHeader).onChange(async (value) => {
this.plugin.settings.useHeader = value;
await this.plugin.saveSettings();
});
});
new import_obsidian2.Setting(containerEl).setName("Popclip Heading").setDesc(
"IMPORTANT!!! Deactivate this if you got error when using popclip with this plugin. This settings can broke things. Thus, always take a backup before changing this setting."
).addToggle((toggle) => {
toggle.setValue(this.plugin.settings.usePopclipHeading).onChange(async (value) => {
console.log("setting usePopclipHeading", value);
this.plugin.settings.usePopclipHeading = value;
await this.plugin.saveSettings();
});
});
}
};
// main.ts
var PopclipPlugin = class extends import_obsidian3.Plugin {
async onload() {
await this.loadSettings();
this.addSettingTab(new PopclipSettingsTab(this.app, this));
this.registerObsidianProtocolHandler(SETTINGS.action, async (ev) => {
if (ev == null ? void 0 : ev.heading) {
if ((ev == null ? void 0 : ev.heading) === SETTINGS.actionHeading) {
new FileWriter(this.app, this).writeToFile(JSON.parse(ev.data));
}
} else if (!this.settings.usePopclipHeading) {
new FileWriter(this.app, this).writeToFile(JSON.parse(ev.data));
}
});
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async activateView() {
}
};