Skip to content

Commit

Permalink
Fix problem with error message on standard text paste, and line count…
Browse files Browse the repository at this point in the history
…ing for replacing text on Cloudinary upload
  • Loading branch information
jordanhandy committed Apr 5, 2022
1 parent c9f0be5 commit ecc9b8b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 87 deletions.
155 changes: 78 additions & 77 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ var require_common = __commonJS((exports2, module2) => {
}
namespaces = split[i].replace(/\*/g, ".*?");
if (namespaces[0] === "-") {
createDebug.skips.push(new RegExp("^" + namespaces.substr(1) + "$"));
createDebug.skips.push(new RegExp("^" + namespaces.slice(1) + "$"));
} else {
createDebug.names.push(new RegExp("^" + namespaces + "$"));
}
Expand Down Expand Up @@ -1304,56 +1304,56 @@ var require_follow_redirects = __commonJS((exports2, module2) => {
});
}
var location = response.headers.location;
if (location && this._options.followRedirects !== false && statusCode >= 300 && statusCode < 400) {
abortRequest(this._currentRequest);
response.destroy();
if (++this._redirectCount > this._options.maxRedirects) {
this.emit("error", new TooManyRedirectsError());
return;
}
if ((statusCode === 301 || statusCode === 302) && this._options.method === "POST" || statusCode === 303 && !/^(?:GET|HEAD)$/.test(this._options.method)) {
this._options.method = "GET";
this._requestBodyBuffers = [];
removeMatchingHeaders(/^content-/i, this._options.headers);
}
var currentHostHeader = removeMatchingHeaders(/^host$/i, this._options.headers);
var currentUrlParts = url.parse(this._currentUrl);
var currentHost = currentHostHeader || currentUrlParts.host;
var currentUrl = /^\w+:/.test(location) ? this._currentUrl : url.format(Object.assign(currentUrlParts, {host: currentHost}));
var redirectUrl;
try {
redirectUrl = url.resolve(currentUrl, location);
} catch (cause) {
this.emit("error", new RedirectionError(cause));
return;
}
debug("redirecting to", redirectUrl);
this._isRedirect = true;
var redirectUrlParts = url.parse(redirectUrl);
Object.assign(this._options, redirectUrlParts);
if (!(redirectUrlParts.host === currentHost || isSubdomainOf(redirectUrlParts.host, currentHost))) {
removeMatchingHeaders(/^authorization$/i, this._options.headers);
}
if (typeof this._options.beforeRedirect === "function") {
var responseDetails = {headers: response.headers};
try {
this._options.beforeRedirect.call(null, this._options, responseDetails);
} catch (err) {
this.emit("error", err);
return;
}
this._sanitizeOptions(this._options);
}
try {
this._performRequest();
} catch (cause) {
this.emit("error", new RedirectionError(cause));
}
} else {
if (!location || this._options.followRedirects === false || statusCode < 300 || statusCode >= 400) {
response.responseUrl = this._currentUrl;
response.redirects = this._redirects;
this.emit("response", response);
this._requestBodyBuffers = [];
return;
}
abortRequest(this._currentRequest);
response.destroy();
if (++this._redirectCount > this._options.maxRedirects) {
this.emit("error", new TooManyRedirectsError());
return;
}
if ((statusCode === 301 || statusCode === 302) && this._options.method === "POST" || statusCode === 303 && !/^(?:GET|HEAD)$/.test(this._options.method)) {
this._options.method = "GET";
this._requestBodyBuffers = [];
removeMatchingHeaders(/^content-/i, this._options.headers);
}
var currentHostHeader = removeMatchingHeaders(/^host$/i, this._options.headers);
var currentUrlParts = url.parse(this._currentUrl);
var currentHost = currentHostHeader || currentUrlParts.host;
var currentUrl = /^\w+:/.test(location) ? this._currentUrl : url.format(Object.assign(currentUrlParts, {host: currentHost}));
var redirectUrl;
try {
redirectUrl = url.resolve(currentUrl, location);
} catch (cause) {
this.emit("error", new RedirectionError(cause));
return;
}
debug("redirecting to", redirectUrl);
this._isRedirect = true;
var redirectUrlParts = url.parse(redirectUrl);
Object.assign(this._options, redirectUrlParts);
if (redirectUrlParts.protocol !== currentUrlParts.protocol && redirectUrlParts.protocol !== "https:" || redirectUrlParts.host !== currentHost && !isSubdomain(redirectUrlParts.host, currentHost)) {
removeMatchingHeaders(/^(?:authorization|cookie)$/i, this._options.headers);
}
if (typeof this._options.beforeRedirect === "function") {
var responseDetails = {headers: response.headers};
try {
this._options.beforeRedirect.call(null, this._options, responseDetails);
} catch (err) {
this.emit("error", err);
return;
}
this._sanitizeOptions(this._options);
}
try {
this._performRequest();
} catch (cause) {
this.emit("error", new RedirectionError(cause));
}
};
function wrap(protocols) {
Expand Down Expand Up @@ -1427,11 +1427,11 @@ var require_follow_redirects = __commonJS((exports2, module2) => {
var lastValue;
for (var header in headers) {
if (regex.test(header)) {
lastValue = headers[header].toString().trim();
lastValue = headers[header];
delete headers[header];
}
}
return lastValue;
return lastValue === null || typeof lastValue === "undefined" ? void 0 : String(lastValue).trim();
}
function createErrorType(code, defaultMessage) {
function CustomError(cause) {
Expand All @@ -1456,7 +1456,7 @@ var require_follow_redirects = __commonJS((exports2, module2) => {
request.on("error", noop);
request.abort();
}
function isSubdomainOf(subdomain, domain) {
function isSubdomain(subdomain, domain) {
const dot = subdomain.length - domain.length - 1;
return dot > 0 && subdomain[dot] === "." && subdomain.endsWith(domain);
}
Expand Down Expand Up @@ -2653,41 +2653,42 @@ var CloudinaryUploader = class extends import_obsidian2.Plugin {
setupPasteHandler() {
this.registerEvent(this.app.workspace.on("editor-paste", async (evt, editor) => {
const {files} = evt.clipboardData;
if (files.length == 0 && !files[0].type.startsWith("text")) {
editor.replaceSelection("Clipboard data is not an image\n");
} else if (this.settings.cloudName && this.settings.uploadPreset && files[0].type.startsWith("image")) {
for (let file of files) {
if (files.length > 0) {
if (this.settings.cloudName && this.settings.uploadPreset && files[0].type.startsWith("image")) {
evt.preventDefault();
const randomString = (Math.random() * 10086).toString(36).substr(0, 8);
const pastePlaceText = `![uploading...](${randomString})
for (let file of files) {
const randomString = (Math.random() * 10086).toString(36).substr(0, 8);
const pastePlaceText = `![uploading...](${randomString})
`;
editor.replaceSelection(pastePlaceText);
const formData = new FormData();
formData.append("file", file);
formData.append("upload_preset", this.settings.uploadPreset);
formData.append("folder", this.settings.folder);
(0, import_axios.default)({
url: `https://api.cloudinary.com/v1_1/${this.settings.cloudName}/upload`,
method: "POST",
data: formData
}).then((res) => {
const url = import_object_path.default.get(res.data, "secure_url");
const imgMarkdownText = `![](${url})`;
this.replaceText(editor, pastePlaceText, imgMarkdownText);
}, (err) => {
new import_obsidian2.Notice(err, 5e3);
console.log(err);
});
editor.replaceSelection(pastePlaceText);
const formData = new FormData();
formData.append("file", file);
formData.append("upload_preset", this.settings.uploadPreset);
formData.append("folder", this.settings.folder);
(0, import_axios.default)({
url: `https://api.cloudinary.com/v1_1/${this.settings.cloudName}/auto/upload`,
method: "POST",
data: formData
}).then((res) => {
console.log(res);
const url = import_object_path.default.get(res.data, "secure_url");
const imgMarkdownText = `![](${url})`;
this.replaceText(editor, pastePlaceText, imgMarkdownText);
}, (err) => {
new import_obsidian2.Notice(err, 5e3);
console.log(err);
});
}
}
} else {
new import_obsidian2.Notice("Cloudinary Image Uploader: Please check the image hosting settings.");
editor.replaceSelection("Please check settings for upload\n This will also appear if file is not of image type");
}
}));
}
replaceText(editor, target, replacement) {
target = target.trim();
const lines = editor.getValue().split("\n");
let lines = [];
for (let i = 0; i < editor.lineCount(); i++) {
lines.push(editor.getLine(i));
}
for (let i = 0; i < lines.length; i++) {
const ch = lines[i].indexOf(target);
if (ch !== -1) {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"author": "Jordan Handy",
"isDesktopOnly": true,
"minAppVersion": "0.11.0",
"version": "0.1.4"
"version": "0.1.5"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-cloudinary-uploader",
"version": "0.1.3",
"version": "0.1.5",
"description": "This plugin uploads the images in your clipboard to Cloudinary as unsigned uploads",
"main": "lib/main.js",
"license": "MIT",
Expand Down
19 changes: 11 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ export default class CloudinaryUploader extends Plugin {


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

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

}))
}}))
}
// Function to replace text
private replaceText(editor: Editor, target: string, replacement: string): void {
target = target.trim();
const lines = editor.lineCount;
let lines = [];
for (let i = 0; i < editor.lineCount(); i++){
lines.push(editor.getLine(i));
}
//const tlines = editor.getValue().split("\n");
for (let i = 0; i < lines.length; i++) {
const ch = lines[i].indexOf(target)
if (ch !== -1) {
Expand Down

0 comments on commit ecc9b8b

Please sign in to comment.