Skip to content

Commit b072f4e

Browse files
authored
Merge pull request #6 from jordanhandy/transformations
Merge Transformations Features
2 parents 8c28a51 + 332800c commit b072f4e

File tree

5 files changed

+118
-6
lines changed

5 files changed

+118
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ This plugin allows you to automatically upload images pasted to Obsidian directl
2323
- Cloud Name
2424
- Upload Preset Name ([Set that here](https://cloudinary.com/documentation/upload_presets))
2525
- Set a Folder Name
26+
5. Optional configuration
27+
- Cloudinary default transformation parameters
2628

2729
## Unsigned vs. Signed Uploads to Cloudinary
2830
The uploads to Cloudinary are unsigned. [You can read more about that here](https://cloudinary.com/documentation/upload_images#unsigned_upload). A signed upload would require the use of an API key and secret, and I opted against asking for that in the plugin configuration, as a choice for security reasons.

main.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,7 +2669,28 @@ var CloudinaryUploaderSettingTab = class extends import_obsidian.PluginSettingTa
26692669
}
26702670
});
26712671
});
2672-
new import_obsidian.Setting(containerEl).setName("f_auto Option").setDesc("Enable f_auto option for image uploads").addToggle((toggle) => {
2672+
containerEl.createEl("h4", {text: "URL Manipulations / Transformation"});
2673+
let textFragment = document.createDocumentFragment();
2674+
let link = document.createElement("a");
2675+
const linkTransformation = document.createElement("a");
2676+
linkTransformation.text = "transformation limits ";
2677+
linkTransformation.href = "https://cloudinary.com/documentation/transformation_counts";
2678+
textFragment.append("The settings below are meant for default image transformations. As they only touch the resulting URL, this should not cause any upload errors, however, if syntax is incorrect, your images will not be referenced correctly (won't render). Be mindful of your Cloudinary ");
2679+
textFragment.append(linkTransformation);
2680+
textFragment.append(" and use the ");
2681+
link.href = "https://cloudinary.com/documentation/transformation_reference";
2682+
link.text = " Cloudinary documentation";
2683+
textFragment.append(link);
2684+
textFragment.append(" for guidance.");
2685+
containerEl.createEl("p", {text: textFragment});
2686+
textFragment = document.createDocumentFragment();
2687+
link = document.createElement("a");
2688+
link.href = "https://cloudinary.com/documentation/image_optimization#automatic_format_selection_f_auto";
2689+
link.text = "f_auto option";
2690+
textFragment.append("Enable the ");
2691+
textFragment.append(link);
2692+
textFragment.append(" for uploads");
2693+
new import_obsidian.Setting(containerEl).setName("f_auto Option").setDesc(textFragment).addToggle((toggle) => {
26732694
toggle.setValue(this.plugin.settings.f_auto).onChange(async (value) => {
26742695
try {
26752696
this.plugin.settings.f_auto = value;
@@ -2679,6 +2700,22 @@ var CloudinaryUploaderSettingTab = class extends import_obsidian.PluginSettingTa
26792700
}
26802701
});
26812702
});
2703+
textFragment = document.createDocumentFragment();
2704+
link = document.createElement("a");
2705+
link.href = "https://cloudinary.com/documentation/transformation_reference";
2706+
link.text = "View Cloudinary's transformation reference for guidance.";
2707+
textFragment.append("Add a comma-delimited default set of transformations to your uploads. You do NOT need to include f_auto here if already enabled above. ");
2708+
textFragment.append(link);
2709+
new import_obsidian.Setting(containerEl).setName("Default Transformation Parameters").setDesc(textFragment).addText((text) => {
2710+
text.setPlaceholder("w_150,h_150").setValue(this.plugin.settings.transformParams).onChange(async (value) => {
2711+
try {
2712+
this.plugin.settings.transformParams = value;
2713+
await this.plugin.saveSettings();
2714+
} catch (e) {
2715+
console.log(e);
2716+
}
2717+
});
2718+
});
26822719
}
26832720
};
26842721
var settings_tab_default = CloudinaryUploaderSettingTab;
@@ -2688,7 +2725,8 @@ var DEFAULT_SETTINGS = {
26882725
cloudName: null,
26892726
uploadPreset: null,
26902727
folder: null,
2691-
f_auto: false
2728+
f_auto: false,
2729+
transformParams: null
26922730
};
26932731
var CloudinaryUploader = class extends import_obsidian2.Plugin {
26942732
setupPasteHandler() {
@@ -2712,8 +2750,15 @@ var CloudinaryUploader = class extends import_obsidian2.Plugin {
27122750
data: formData
27132751
}).then((res) => {
27142752
console.log(res);
2715-
const url = import_object_path.default.get(res.data, "secure_url");
2753+
let url = import_object_path.default.get(res.data, "secure_url");
27162754
let imgMarkdownText = "";
2755+
if (this.settings.transformParams) {
2756+
const splitURL = url.split("/upload/", 2);
2757+
let modifiedURL = "";
2758+
modifiedURL = splitURL[0] += "/upload/" + this.settings.transformParams + "/" + splitURL[1];
2759+
imgMarkdownText = `![](${modifiedURL})`;
2760+
url = modifiedURL;
2761+
}
27172762
if (this.settings.f_auto) {
27182763
const splitURL = url.split("/upload/", 2);
27192764
let modifiedURL = "";

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.6"
8+
"version": "0.2.0"
99
}

src/main.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface CloudinarySettings {
1818
uploadPreset: string;
1919
folder: string;
2020
f_auto: boolean;
21+
transformParams: string;
2122
//maxWidth: number; TODO
2223
// enableResize: boolean; TODO
2324
}
@@ -28,6 +29,7 @@ const DEFAULT_SETTINGS: CloudinarySettings = {
2829
uploadPreset: null,
2930
folder: null,
3031
f_auto: false,
32+
transformParams: null,
3133
//maxWidth: 4096, TODO
3234
//enableResize: false, TODO later
3335
};
@@ -65,13 +67,24 @@ export default class CloudinaryUploader extends Plugin {
6567
}).then(res => {
6668
// Get response public URL of uploaded image
6769
console.log(res);
68-
const url = objectPath.get(res.data, 'secure_url')
70+
let url = objectPath.get(res.data, 'secure_url')
6971
let imgMarkdownText ="";
72+
73+
// Split URL to allow for appending transformations
74+
if(this.settings.transformParams){
75+
const splitURL = url.split("/upload/",2);
76+
let modifiedURL='';
77+
modifiedURL = splitURL[0]+="/upload/"+this.settings.transformParams+"/"+splitURL[1];
78+
imgMarkdownText = `![](${modifiedURL})`;
79+
url = modifiedURL
80+
}
7081
if(this.settings.f_auto){
7182
const splitURL = url.split("/upload/",2);
7283
let modifiedURL='';
7384
modifiedURL = splitURL[0]+="/upload/f_auto/"+splitURL[1];
7485
imgMarkdownText = `![](${modifiedURL})`;
86+
87+
// leave stamdard of no transformations added
7588
}else{
7689
imgMarkdownText = `![](${url})`;
7790
}

src/settings-tab.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,38 @@ export default class CloudinaryUploaderSettingTab extends PluginSettingTab {
6969
}
7070
})
7171
});
72+
containerEl.createEl("h4", { text: "URL Manipulations / Transformation" });
73+
74+
// Allow for inline hyperlinks with anchor tags
75+
let textFragment = document.createDocumentFragment();
76+
let link = document.createElement("a");
77+
78+
const linkTransformation = document.createElement("a");
79+
linkTransformation.text="transformation limits ";
80+
linkTransformation.href="https://cloudinary.com/documentation/transformation_counts";
81+
82+
textFragment.append("The settings below are meant for default image transformations. As they only touch the resulting URL, this should not cause any upload errors, however, if syntax is incorrect, your images will not be referenced correctly (won't render). Be mindful of your Cloudinary ");
83+
textFragment.append(linkTransformation);
84+
textFragment.append(" and use the ");
85+
86+
link.href = "https://cloudinary.com/documentation/transformation_reference";
87+
link.text = " Cloudinary documentation"
88+
textFragment.append(link);
89+
textFragment.append(" for guidance.");
90+
containerEl.createEl("p", { text: textFragment });
91+
92+
93+
textFragment = document.createDocumentFragment();
94+
link = document.createElement("a");
95+
link.href="https://cloudinary.com/documentation/image_optimization#automatic_format_selection_f_auto";
96+
link.text="f_auto option";
97+
textFragment.append("Enable the ");
98+
textFragment.append(link);
99+
textFragment.append(" for uploads");
100+
72101
new Setting(containerEl)
73102
.setName("f_auto Option")
74-
.setDesc("Enable f_auto option for image uploads")
103+
.setDesc(textFragment)
75104
.addToggle((toggle) => {
76105
toggle
77106
.setValue(this.plugin.settings.f_auto)
@@ -85,5 +114,28 @@ export default class CloudinaryUploaderSettingTab extends PluginSettingTab {
85114
}
86115
})
87116
});
117+
textFragment = document.createDocumentFragment();
118+
link = document.createElement("a");
119+
link.href="https://cloudinary.com/documentation/transformation_reference";
120+
link.text="View Cloudinary's transformation reference for guidance.";
121+
textFragment.append("Add a comma-delimited default set of transformations to your uploads. You do NOT need to include f_auto here if already enabled above. ");
122+
textFragment.append(link);
123+
new Setting(containerEl)
124+
.setName("Default Transformation Parameters")
125+
.setDesc(textFragment)
126+
.addText((text) => {
127+
text
128+
.setPlaceholder("w_150,h_150")
129+
.setValue(this.plugin.settings.transformParams)
130+
.onChange(async (value) => {
131+
try {
132+
this.plugin.settings.transformParams = value;
133+
await this.plugin.saveSettings();
134+
}
135+
catch (e) {
136+
console.log(e)
137+
}
138+
})
139+
});
88140
}
89141
}

0 commit comments

Comments
 (0)