main.js
1 /* 2 THIS IS A GENERATED/BUNDLED FILE BY ESBUILD 3 if you want to view the source, please visit the github repository of this plugin 4 */ 5 6 var __defProp = Object.defineProperty; 7 var __getOwnPropDesc = Object.getOwnPropertyDescriptor; 8 var __getOwnPropNames = Object.getOwnPropertyNames; 9 var __hasOwnProp = Object.prototype.hasOwnProperty; 10 var __export = (target, all) => { 11 for (var name in all) 12 __defProp(target, name, { get: all[name], enumerable: true }); 13 }; 14 var __copyProps = (to, from, except, desc) => { 15 if (from && typeof from === "object" || typeof from === "function") { 16 for (let key of __getOwnPropNames(from)) 17 if (!__hasOwnProp.call(to, key) && key !== except) 18 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); 19 } 20 return to; 21 }; 22 var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); 23 24 // main.ts 25 var main_exports = {}; 26 __export(main_exports, { 27 default: () => FontPlugin 28 }); 29 module.exports = __toCommonJS(main_exports); 30 var import_obsidian = require("obsidian"); 31 var DEFAULT_SETTINGS = { 32 font: "None", 33 force_mode: false, 34 custom_css_mode: false, 35 custom_css: "" 36 }; 37 function get_default_css(font_family_name, css_class = ":root *") { 38 return `${css_class} { 39 --font-default: ${font_family_name}; 40 --default-font: ${font_family_name}; 41 --font-family-editor: ${font_family_name}; 42 --font-monospace-default: ${font_family_name}, 43 --font-interface-override: ${font_family_name}, 44 --font-text-override: ${font_family_name}, 45 --font-monospace-override: ${font_family_name}, 46 } 47 `; 48 } 49 function get_custom_css(font_family_name, css_class = ":root *") { 50 return `${css_class} * { 51 font-family: ${font_family_name} !important; 52 }`; 53 } 54 function arrayBufferToBase64(buffer) { 55 let binary = ""; 56 const bytes = new Uint8Array(buffer); 57 for (let i = 0; i < bytes.byteLength; i++) { 58 binary += String.fromCharCode(bytes[i]); 59 } 60 return btoa(binary); 61 } 62 function applyCss(css, css_id, appendMode = false) { 63 const existingStyle = document.getElementById(css_id); 64 if (existingStyle && appendMode) { 65 existingStyle.innerHTML += css; 66 } else { 67 const style = document.createElement("style"); 68 style.innerHTML = css; 69 document.head.appendChild(style); 70 if (existingStyle) { 71 existingStyle.remove(); 72 } 73 style.id = css_id; 74 } 75 } 76 var FontPlugin = class extends import_obsidian.Plugin { 77 constructor() { 78 super(...arguments); 79 this.config_dir = this.app.vault.configDir; 80 this.plugin_folder_path = `${this.config_dir}/plugins/custom-font-loader`; 81 this.font_folder_path = `${this.app.vault.configDir}/fonts`; 82 } 83 async load_plugin() { 84 await this.loadSettings(); 85 try { 86 const font_file_name = this.settings.font; 87 if (font_file_name && font_file_name.toLowerCase() != "none") { 88 if (font_file_name != "all") { 89 await this.process_and_load_font(font_file_name, false); 90 } else { 91 applyCss("", "custom_font_base64"); 92 const files = await this.app.vault.adapter.list(this.font_folder_path); 93 for (const file of files.files) { 94 const file_name = file.split("/")[2]; 95 await this.process_and_load_font(file_name, true); 96 } 97 } 98 } else { 99 applyCss("", "custom_font_base64"); 100 applyCss("", "custom_font_general"); 101 } 102 } catch (error) { 103 new import_obsidian.Notice(error); 104 } 105 } 106 async process_and_load_font(font_file_name, load_all_fonts) { 107 console.log("loading %s", font_file_name); 108 const css_font_path = `${this.plugin_folder_path}/${font_file_name.toLowerCase().replace(".", "_")}.css`; 109 if (!await this.app.vault.adapter.exists(css_font_path)) { 110 await this.convert_font_to_css(font_file_name, css_font_path); 111 } else { 112 await this.load_font(css_font_path, load_all_fonts); 113 await this.load_css(font_file_name); 114 } 115 } 116 async load_font(css_font_path, appendMode) { 117 const content = await this.app.vault.adapter.read(css_font_path); 118 applyCss(content, "custom_font_base64", appendMode); 119 } 120 async load_css(font_file_name) { 121 let css_string = ""; 122 const font_family_name = font_file_name.split(".")[0]; 123 if (this.settings.custom_css_mode) { 124 css_string = this.settings.custom_css; 125 } else { 126 css_string = get_default_css(font_family_name); 127 } 128 if (this.settings.force_mode) 129 css_string += ` 130 * { 131 font-family: ${font_family_name} !important; 132 } 133 `; 134 applyCss(css_string, "custom_font_general"); 135 } 136 async convert_font_to_css(font_file_name, css_font_path) { 137 new import_obsidian.Notice("Processing Font files"); 138 const file = `${this.config_dir}/fonts/${font_file_name}`; 139 const arrayBuffer = await this.app.vault.adapter.readBinary(file); 140 const base64 = arrayBufferToBase64(arrayBuffer); 141 const font_family_name = font_file_name.split(".")[0]; 142 const font_extension_name = font_file_name.split(".")[1]; 143 let css_type = ""; 144 switch (font_extension_name) { 145 case "woff": 146 css_type = "font/woff"; 147 break; 148 case "ttf": 149 css_type = "font/truetype"; 150 break; 151 case "woff2": 152 css_type = "font/woff2"; 153 break; 154 case "otf": 155 css_type = "font/opentype"; 156 break; 157 default: 158 css_type = "font"; 159 } 160 const base64_css = `@font-face{ 161 font-family: '${font_family_name}'; 162 src: url(data:${css_type};base64,${base64}); 163 }`; 164 this.app.vault.adapter.write(css_font_path, base64_css); 165 console.log("saved font %s into %s", font_family_name, css_font_path); 166 console.log("Font CSS Saved into %s", css_font_path); 167 await this.load_plugin(); 168 } 169 async onload() { 170 this.load_plugin(); 171 this.addSettingTab(new FontSettingTab(this.app, this)); 172 } 173 async onunload() { 174 applyCss("", "custom_font_base64"); 175 applyCss("", "custom_font_general"); 176 } 177 async loadSettings() { 178 this.settings = Object.assign( 179 {}, 180 DEFAULT_SETTINGS, 181 await this.loadData() 182 ); 183 } 184 async saveSettings() { 185 await this.saveData(this.settings); 186 } 187 }; 188 var FontSettingTab = class extends import_obsidian.PluginSettingTab { 189 constructor(app, plugin) { 190 super(app, plugin); 191 this.plugin = plugin; 192 } 193 async display() { 194 const { containerEl } = this; 195 containerEl.empty(); 196 const font_folder_path = `${this.app.vault.configDir}/fonts`; 197 const infoContainer = containerEl.createDiv(); 198 infoContainer.setText("In Order to set the font, copy your font into '.obsidian/fonts/' directory."); 199 const options = [{ name: "none", value: "None" }]; 200 try { 201 if (!await this.app.vault.adapter.exists(font_folder_path)) { 202 await this.app.vault.adapter.mkdir(font_folder_path); 203 } 204 if (await this.app.vault.adapter.exists(font_folder_path)) { 205 const files = await this.app.vault.adapter.list(font_folder_path); 206 for (const file of files.files) { 207 const file_name = file.split("/")[2]; 208 options.push({ name: file_name, value: file_name }); 209 } 210 } 211 options.push({ name: "all", value: "Multiple fonts" }); 212 } catch (error) { 213 console.log(error); 214 } 215 new import_obsidian.Setting(containerEl).setName("Font").setDesc("Choose font (If you choose multiple fonts option, we will load and process all fonts in the folder for you)").addDropdown((dropdown) => { 216 for (const opt of options) { 217 dropdown.addOption(opt.name, opt.value); 218 } 219 dropdown.setValue(this.plugin.settings.font).onChange(async (value) => { 220 this.plugin.settings.font = value; 221 await this.plugin.saveSettings(); 222 await this.plugin.load_plugin(); 223 }); 224 }); 225 new import_obsidian.Setting(containerEl).setName("Force Style").setDesc("This option should only be used if you have installed a community theme and normal mode doesn't work").addToggle((toggle) => { 226 toggle.setValue(this.plugin.settings.force_mode); 227 toggle.onChange(async (value) => { 228 this.plugin.settings.force_mode = value; 229 await this.plugin.saveSettings(); 230 await this.plugin.load_plugin(); 231 }); 232 }); 233 new import_obsidian.Setting(containerEl).setName("Custom CSS Mode").setDesc("If you want to apply a custom css style rather than default style, choose this.").addToggle((toggle) => { 234 toggle.setValue(this.plugin.settings.custom_css_mode); 235 toggle.onChange(async (value) => { 236 if (this.plugin.settings.custom_css_mode == false) { 237 this.plugin.settings.custom_css = ""; 238 } 239 this.plugin.settings.custom_css_mode = value; 240 this.plugin.saveSettings(); 241 this.plugin.load_plugin(); 242 this.display(); 243 }); 244 }); 245 if (this.plugin.settings.custom_css_mode) { 246 new import_obsidian.Setting(containerEl).setName("Custom CSS Style").setDesc("Input your custom css style").addTextArea(async (text) => { 247 text.onChange( 248 async (new_value) => { 249 this.plugin.settings.custom_css = new_value; 250 await this.plugin.saveSettings(); 251 await this.plugin.load_plugin(); 252 } 253 ); 254 text.setDisabled(!this.plugin.settings.custom_css_mode); 255 if (this.plugin.settings.custom_css == "") { 256 let font_family_name = ""; 257 try { 258 font_family_name = this.plugin.settings.font.split(".")[0]; 259 } catch (error) { 260 console.log(error); 261 } 262 if (font_family_name == "all") { 263 if (await this.app.vault.adapter.exists(font_folder_path)) { 264 const files = await this.app.vault.adapter.list(font_folder_path); 265 let final_str = ""; 266 for (const file of files.files) { 267 const file_name = file.split("/")[2]; 268 const font_family = file_name.split(".")[0]; 269 final_str += "\n" + get_custom_css(font_family, "." + font_family); 270 } 271 text.setValue(final_str); 272 } 273 } else { 274 text.setValue(get_default_css(font_family_name)); 275 } 276 } else { 277 text.setValue(this.plugin.settings.custom_css); 278 } 279 text.onChanged(); 280 text.inputEl.style.width = "100%"; 281 text.inputEl.style.height = "100px"; 282 }); 283 } 284 } 285 };