Skip to content

Commit ecc9b8b

Browse files
committed
Fix problem with error message on standard text paste, and line counting for replacing text on Cloudinary upload
1 parent c9f0be5 commit ecc9b8b

File tree

4 files changed

+91
-87
lines changed

4 files changed

+91
-87
lines changed

main.js

Lines changed: 78 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ var require_common = __commonJS((exports2, module2) => {
844844
}
845845
namespaces = split[i].replace(/\*/g, ".*?");
846846
if (namespaces[0] === "-") {
847-
createDebug.skips.push(new RegExp("^" + namespaces.substr(1) + "$"));
847+
createDebug.skips.push(new RegExp("^" + namespaces.slice(1) + "$"));
848848
} else {
849849
createDebug.names.push(new RegExp("^" + namespaces + "$"));
850850
}
@@ -1304,56 +1304,56 @@ var require_follow_redirects = __commonJS((exports2, module2) => {
13041304
});
13051305
}
13061306
var location = response.headers.location;
1307-
if (location && this._options.followRedirects !== false && statusCode >= 300 && statusCode < 400) {
1308-
abortRequest(this._currentRequest);
1309-
response.destroy();
1310-
if (++this._redirectCount > this._options.maxRedirects) {
1311-
this.emit("error", new TooManyRedirectsError());
1312-
return;
1313-
}
1314-
if ((statusCode === 301 || statusCode === 302) && this._options.method === "POST" || statusCode === 303 && !/^(?:GET|HEAD)$/.test(this._options.method)) {
1315-
this._options.method = "GET";
1316-
this._requestBodyBuffers = [];
1317-
removeMatchingHeaders(/^content-/i, this._options.headers);
1318-
}
1319-
var currentHostHeader = removeMatchingHeaders(/^host$/i, this._options.headers);
1320-
var currentUrlParts = url.parse(this._currentUrl);
1321-
var currentHost = currentHostHeader || currentUrlParts.host;
1322-
var currentUrl = /^\w+:/.test(location) ? this._currentUrl : url.format(Object.assign(currentUrlParts, {host: currentHost}));
1323-
var redirectUrl;
1324-
try {
1325-
redirectUrl = url.resolve(currentUrl, location);
1326-
} catch (cause) {
1327-
this.emit("error", new RedirectionError(cause));
1328-
return;
1329-
}
1330-
debug("redirecting to", redirectUrl);
1331-
this._isRedirect = true;
1332-
var redirectUrlParts = url.parse(redirectUrl);
1333-
Object.assign(this._options, redirectUrlParts);
1334-
if (!(redirectUrlParts.host === currentHost || isSubdomainOf(redirectUrlParts.host, currentHost))) {
1335-
removeMatchingHeaders(/^authorization$/i, this._options.headers);
1336-
}
1337-
if (typeof this._options.beforeRedirect === "function") {
1338-
var responseDetails = {headers: response.headers};
1339-
try {
1340-
this._options.beforeRedirect.call(null, this._options, responseDetails);
1341-
} catch (err) {
1342-
this.emit("error", err);
1343-
return;
1344-
}
1345-
this._sanitizeOptions(this._options);
1346-
}
1347-
try {
1348-
this._performRequest();
1349-
} catch (cause) {
1350-
this.emit("error", new RedirectionError(cause));
1351-
}
1352-
} else {
1307+
if (!location || this._options.followRedirects === false || statusCode < 300 || statusCode >= 400) {
13531308
response.responseUrl = this._currentUrl;
13541309
response.redirects = this._redirects;
13551310
this.emit("response", response);
13561311
this._requestBodyBuffers = [];
1312+
return;
1313+
}
1314+
abortRequest(this._currentRequest);
1315+
response.destroy();
1316+
if (++this._redirectCount > this._options.maxRedirects) {
1317+
this.emit("error", new TooManyRedirectsError());
1318+
return;
1319+
}
1320+
if ((statusCode === 301 || statusCode === 302) && this._options.method === "POST" || statusCode === 303 && !/^(?:GET|HEAD)$/.test(this._options.method)) {
1321+
this._options.method = "GET";
1322+
this._requestBodyBuffers = [];
1323+
removeMatchingHeaders(/^content-/i, this._options.headers);
1324+
}
1325+
var currentHostHeader = removeMatchingHeaders(/^host$/i, this._options.headers);
1326+
var currentUrlParts = url.parse(this._currentUrl);
1327+
var currentHost = currentHostHeader || currentUrlParts.host;
1328+
var currentUrl = /^\w+:/.test(location) ? this._currentUrl : url.format(Object.assign(currentUrlParts, {host: currentHost}));
1329+
var redirectUrl;
1330+
try {
1331+
redirectUrl = url.resolve(currentUrl, location);
1332+
} catch (cause) {
1333+
this.emit("error", new RedirectionError(cause));
1334+
return;
1335+
}
1336+
debug("redirecting to", redirectUrl);
1337+
this._isRedirect = true;
1338+
var redirectUrlParts = url.parse(redirectUrl);
1339+
Object.assign(this._options, redirectUrlParts);
1340+
if (redirectUrlParts.protocol !== currentUrlParts.protocol && redirectUrlParts.protocol !== "https:" || redirectUrlParts.host !== currentHost && !isSubdomain(redirectUrlParts.host, currentHost)) {
1341+
removeMatchingHeaders(/^(?:authorization|cookie)$/i, this._options.headers);
1342+
}
1343+
if (typeof this._options.beforeRedirect === "function") {
1344+
var responseDetails = {headers: response.headers};
1345+
try {
1346+
this._options.beforeRedirect.call(null, this._options, responseDetails);
1347+
} catch (err) {
1348+
this.emit("error", err);
1349+
return;
1350+
}
1351+
this._sanitizeOptions(this._options);
1352+
}
1353+
try {
1354+
this._performRequest();
1355+
} catch (cause) {
1356+
this.emit("error", new RedirectionError(cause));
13571357
}
13581358
};
13591359
function wrap(protocols) {
@@ -1427,11 +1427,11 @@ var require_follow_redirects = __commonJS((exports2, module2) => {
14271427
var lastValue;
14281428
for (var header in headers) {
14291429
if (regex.test(header)) {
1430-
lastValue = headers[header].toString().trim();
1430+
lastValue = headers[header];
14311431
delete headers[header];
14321432
}
14331433
}
1434-
return lastValue;
1434+
return lastValue === null || typeof lastValue === "undefined" ? void 0 : String(lastValue).trim();
14351435
}
14361436
function createErrorType(code, defaultMessage) {
14371437
function CustomError(cause) {
@@ -1456,7 +1456,7 @@ var require_follow_redirects = __commonJS((exports2, module2) => {
14561456
request.on("error", noop);
14571457
request.abort();
14581458
}
1459-
function isSubdomainOf(subdomain, domain) {
1459+
function isSubdomain(subdomain, domain) {
14601460
const dot = subdomain.length - domain.length - 1;
14611461
return dot > 0 && subdomain[dot] === "." && subdomain.endsWith(domain);
14621462
}
@@ -2653,41 +2653,42 @@ var CloudinaryUploader = class extends import_obsidian2.Plugin {
26532653
setupPasteHandler() {
26542654
this.registerEvent(this.app.workspace.on("editor-paste", async (evt, editor) => {
26552655
const {files} = evt.clipboardData;
2656-
if (files.length == 0 && !files[0].type.startsWith("text")) {
2657-
editor.replaceSelection("Clipboard data is not an image\n");
2658-
} else if (this.settings.cloudName && this.settings.uploadPreset && files[0].type.startsWith("image")) {
2659-
for (let file of files) {
2656+
if (files.length > 0) {
2657+
if (this.settings.cloudName && this.settings.uploadPreset && files[0].type.startsWith("image")) {
26602658
evt.preventDefault();
2661-
const randomString = (Math.random() * 10086).toString(36).substr(0, 8);
2662-
const pastePlaceText = `![uploading...](${randomString})
2659+
for (let file of files) {
2660+
const randomString = (Math.random() * 10086).toString(36).substr(0, 8);
2661+
const pastePlaceText = `![uploading...](${randomString})
26632662
`;
2664-
editor.replaceSelection(pastePlaceText);
2665-
const formData = new FormData();
2666-
formData.append("file", file);
2667-
formData.append("upload_preset", this.settings.uploadPreset);
2668-
formData.append("folder", this.settings.folder);
2669-
(0, import_axios.default)({
2670-
url: `https://api.cloudinary.com/v1_1/${this.settings.cloudName}/upload`,
2671-
method: "POST",
2672-
data: formData
2673-
}).then((res) => {
2674-
const url = import_object_path.default.get(res.data, "secure_url");
2675-
const imgMarkdownText = `![](${url})`;
2676-
this.replaceText(editor, pastePlaceText, imgMarkdownText);
2677-
}, (err) => {
2678-
new import_obsidian2.Notice(err, 5e3);
2679-
console.log(err);
2680-
});
2663+
editor.replaceSelection(pastePlaceText);
2664+
const formData = new FormData();
2665+
formData.append("file", file);
2666+
formData.append("upload_preset", this.settings.uploadPreset);
2667+
formData.append("folder", this.settings.folder);
2668+
(0, import_axios.default)({
2669+
url: `https://api.cloudinary.com/v1_1/${this.settings.cloudName}/auto/upload`,
2670+
method: "POST",
2671+
data: formData
2672+
}).then((res) => {
2673+
console.log(res);
2674+
const url = import_object_path.default.get(res.data, "secure_url");
2675+
const imgMarkdownText = `![](${url})`;
2676+
this.replaceText(editor, pastePlaceText, imgMarkdownText);
2677+
}, (err) => {
2678+
new import_obsidian2.Notice(err, 5e3);
2679+
console.log(err);
2680+
});
2681+
}
26812682
}
2682-
} else {
2683-
new import_obsidian2.Notice("Cloudinary Image Uploader: Please check the image hosting settings.");
2684-
editor.replaceSelection("Please check settings for upload\n This will also appear if file is not of image type");
26852683
}
26862684
}));
26872685
}
26882686
replaceText(editor, target, replacement) {
26892687
target = target.trim();
2690-
const lines = editor.getValue().split("\n");
2688+
let lines = [];
2689+
for (let i = 0; i < editor.lineCount(); i++) {
2690+
lines.push(editor.getLine(i));
2691+
}
26912692
for (let i = 0; i < lines.length; i++) {
26922693
const ch = lines[i].indexOf(target);
26932694
if (ch !== -1) {

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"author": "Jordan Handy",
66
"isDesktopOnly": true,
77
"minAppVersion": "0.11.0",
8-
"version": "0.1.4"
8+
"version": "0.1.5"
99
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-cloudinary-uploader",
3-
"version": "0.1.3",
3+
"version": "0.1.5",
44
"description": "This plugin uploads the images in your clipboard to Cloudinary as unsigned uploads",
55
"main": "lib/main.js",
66
"license": "MIT",

src/main.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,13 @@ export default class CloudinaryUploader extends Plugin {
3434

3535

3636
setupPasteHandler(): void {
37-
// On past event, get "files" from clipbaord data
37+
// On paste event, get "files" from clipbaord data
3838
// If files contain image, move to API call
3939
// if Files empty or does not contain image, throw error
4040
this.registerEvent(this.app.workspace.on('editor-paste',async (evt: ClipboardEvent, editor: Editor)=>{
4141
const { files } = evt.clipboardData;
42-
if (files.length == 0 || !files[0].type.startsWith("text")) {
43-
editor.replaceSelection("Clipboard data is not an image\n");
44-
}
45-
else if (this.settings.cloudName && this.settings.uploadPreset && files[0].type.startsWith("image")) {
42+
if(files.length > 0){
43+
if (this.settings.cloudName && this.settings.uploadPreset && files[0].type.startsWith("image")) {
4644
evt.preventDefault(); // Prevent default paste behaviour
4745
for (let file of files) {
4846
const randomString = (Math.random() * 10086).toString(36).substr(0, 8)
@@ -59,11 +57,12 @@ export default class CloudinaryUploader extends Plugin {
5957

6058
// Make API call
6159
axios({
62-
url: `https://api.cloudinary.com/v1_1/${this.settings.cloudName}/upload`,
60+
url: `https://api.cloudinary.com/v1_1/${this.settings.cloudName}/auto/upload`,
6361
method: 'POST',
6462
data: formData
6563
}).then(res => {
6664
// Get response public URL of uploaded image
65+
console.log(res);
6766
const url = objectPath.get(res.data, 'secure_url')
6867
const imgMarkdownText = `![](${url})`
6968
// Show MD syntax using uploaded image URL, in Obsidian Editor
@@ -81,12 +80,16 @@ export default class CloudinaryUploader extends Plugin {
8180
// editor.replaceSelection("Please check settings for upload\n This will also appear if file is not of image type");
8281
// }
8382

84-
}))
83+
}}))
8584
}
8685
// Function to replace text
8786
private replaceText(editor: Editor, target: string, replacement: string): void {
8887
target = target.trim();
89-
const lines = editor.lineCount;
88+
let lines = [];
89+
for (let i = 0; i < editor.lineCount(); i++){
90+
lines.push(editor.getLine(i));
91+
}
92+
//const tlines = editor.getValue().split("\n");
9093
for (let i = 0; i < lines.length; i++) {
9194
const ch = lines[i].indexOf(target)
9295
if (ch !== -1) {

0 commit comments

Comments
 (0)