.obsidian/plugins/obsidian-account-linker/main.js

409 lines
13 KiB
JavaScript

/*
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);
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// main.ts
var main_exports = {};
__export(main_exports, {
default: () => AccountLinker
});
module.exports = __toCommonJS(main_exports);
var import_obsidian3 = require("obsidian");
// src/settings/AccountLinkerSettingTab.ts
var import_obsidian2 = require("obsidian");
// src/control/utils.ts
function replaceTemplateText(beforeText, ctx) {
return beforeText.split("{{NAME}}").join(ctx.text).replace(/\{\{[^}]*\}\}/g, "");
}
function isColor(code) {
return /^#[0-9A-Fa-f]{6}$/.test(code);
}
function isURL(link) {
return true;
}
function selectFontColor(backgroundColor) {
if (!isColor(backgroundColor)) {
return "#000000";
}
const brightness = parseInt(backgroundColor.substring(1, 3), 16) * 0.299 + parseInt(backgroundColor.substring(3, 5), 16) * 0.587 + parseInt(backgroundColor.substring(5, 7), 16) * 0.114;
return Math.floor(brightness) >= 140 ? "#000000" : "#FFFFFF";
}
// src/drawing/drawAccountLink.ts
function drawAccountLink(a, config, text) {
a.empty();
a.classList.add("frontmatter-accounts");
const linkText = replaceTemplateText(config.urlTemplate, { text });
if (isURL(linkText)) {
a.href = linkText;
} else {
a.href = "";
}
const siteNameDiv = a.createEl("div");
siteNameDiv.classList.add("frontmatter-accounts-sitename");
siteNameDiv.innerText = config.name;
siteNameDiv.style.backgroundColor = isColor(config.color) ? config.color : "#ffffff";
siteNameDiv.style.color = selectFontColor(config.color);
const labelText = replaceTemplateText(config.labelTemplate, { text });
if (labelText != "") {
const labelDiv = a.createEl("div");
labelDiv.classList.add("frontmatter-accounts-label");
labelDiv.innerText = labelText;
}
}
// src/settings/WebsiteEditModal.ts
var import_obsidian = require("obsidian");
var descriptions = {
name: "Website name",
color: "Website image color(HEX)",
urlTemplate: "URL Replace Pattern",
labelTemplate: "Account Name Replace Pattern",
doesReverseResolution: "If on, it will reverse the account from the URL entered in the `accounts` field of the front matter",
previewBox: "",
saveButton: ""
};
var WebsiteEditModal = class extends import_obsidian.Modal {
constructor(plugin, config, closeCallBack) {
super(plugin.app);
this.plugin = plugin;
this.config = config;
this.closeCallBack = closeCallBack;
}
onOpen() {
const config = this.config;
this.titleEl.setText("Website Config");
const settings = {
name: new import_obsidian.Setting(this.contentEl).setName("Name").setDesc(descriptions.name).addText((cb) => {
cb.setValue(config.name).setPlaceholder("Twitter").onChange((value) => {
config.name = value;
this.updateText("name", settings);
this.updateDisplay(settings);
});
}),
color: new import_obsidian.Setting(this.contentEl).setName("Color").setDesc(descriptions.color).addText((cb) => {
cb.setValue(config.color).setPlaceholder("#3e9cec").onChange((value) => {
config.color = value;
this.updateText("color", settings);
this.updateDisplay(settings);
});
}),
urlTemplate: new import_obsidian.Setting(this.contentEl).setName("URL Template").setDesc(descriptions.urlTemplate).addText((cb) => {
cb.setValue(config.urlTemplate).setPlaceholder("https://twitter.com/{{NAME}}").onChange((value) => {
config.urlTemplate = value;
this.updateText("urlTemplate", settings);
this.updateDisplay(settings);
});
}),
labelTemplate: new import_obsidian.Setting(this.contentEl).setName("Label Template").setDesc(descriptions.labelTemplate).addText((cb) => {
cb.setValue(config.labelTemplate).setPlaceholder("@{{NAME}}").onChange((value) => {
config.labelTemplate = value;
this.updateText("labelTemplate", settings);
this.updateDisplay(settings);
});
}),
doesReverseResolution: new import_obsidian.Setting(this.contentEl).setName("Reverse Resolution(Unimplemented)").setDesc(descriptions.doesReverseResolution).addToggle((cb) => {
cb.setValue(config.doesReverseResolution).onChange((value) => {
config.doesReverseResolution = value;
this.updateText("doesReverseResolution", settings);
this.updateDisplay(settings);
}).setDisabled;
}),
previewBox: new import_obsidian.Setting(this.contentEl).setDesc(descriptions.previewBox).setName("Preview"),
saveButton: new import_obsidian.Setting(this.contentEl).setDesc(descriptions.saveButton).addButton((b) => {
b.setButtonText("Save").setDisabled(true).onClick((evt) => {
this.closeCallBack(config);
this.close();
});
})
};
["name", "color", "urlTemplate", "labelTemplate", "doesReverseResolution"].forEach((key) => {
this.updateText(key, settings);
});
this.updateDisplay(settings);
}
checkConfig(key) {
switch (key) {
case "name":
if (this.config.name == "") {
return "The name length must be greater than zero";
} else if (["aliases", "alias", "tags", "tag", "cssclass", "publish", "accounts"].includes(this.config.name.toLowerCase())) {
return "The name must be something other";
} else {
return "";
}
case "color":
if (!isColor(this.config.color)) {
return "The color must be represented by `#` and a six-digit hexadecimal number";
} else {
return "";
}
case "urlTemplate":
if (!isURL(this.config.urlTemplate.replace(/\{\{[^}]*\}\}/g, ""))) {
return "URL is invalid";
} else {
return "";
}
case "labelTemplate":
return "";
case "doesReverseResolution":
return "";
}
}
updateText(key, settings) {
if (this.checkConfig(key) != "") {
settings[key].descEl.innerHTML = descriptions[key] + `<br><span class='mod-warning'>${this.checkConfig(key)}</span>`;
} else {
settings[key].descEl.innerHTML = descriptions[key];
}
}
updateDisplay(settings) {
let f = false;
["name", "color", "urlTemplate", "labelTemplate", "doesReverseResolution"].forEach((key) => {
if (this.checkConfig(key) != "") {
f = true;
}
});
settings.saveButton.setDisabled(f);
const linker = document.createElement("a");
drawAccountLink(linker, this.config, "example");
settings.previewBox.descEl.innerHTML = linker.outerHTML;
}
};
// src/control/websiteConfig.ts
var websiteConfig = class {
constructor() {
this.name = "";
this.color = "#FFFFFF";
this.urlTemplate = "";
this.labelTemplate = "";
this.doesReverseResolution = false;
}
};
// src/settings/AccountLinkerSettingTab.ts
var AccountLinkerSettingTab = class extends import_obsidian2.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
const { containerEl } = this;
containerEl.empty();
const siteDiv = containerEl.createDiv();
this.drawSites(siteDiv);
}
drawSites(div) {
div.empty();
const websites = this.plugin.settings.websites;
websites.forEach((website, i) => {
const s = new import_obsidian2.Setting(div).setDesc(website.name).addButton((button) => {
button.onClick(() => {
const modal = new WebsiteEditModal(this.plugin, website, (config) => {
this.plugin.settings.websites[i] = config;
this.plugin.saveSettings();
this.drawSites(div);
});
modal.open();
});
button.setIcon("pencil");
button.setTooltip("Edit");
}).addButton((button) => {
button.onClick(() => {
this.plugin.settings.websites.remove(website);
this.plugin.saveSettings();
this.drawSites(div);
});
button.setIcon("cross");
button.setTooltip("Remove");
});
const linker = document.createElement("a");
drawAccountLink(linker, website, "example");
s.descEl.innerHTML += linker.outerHTML;
});
new import_obsidian2.Setting(div).addButton((button) => {
button.onClick(() => {
const modal = new WebsiteEditModal(this.plugin, new websiteConfig(), (config) => {
this.plugin.settings.websites.push(config);
this.plugin.saveSettings();
this.drawSites(div);
});
modal.open();
});
button.setIcon("plus-with-circle");
button.setTooltip("New");
});
}
};
// src/settings/AccountLinkerSettings.ts
var DEFAULT_SETTINGS = {
websites: [
{
name: "Twitter",
color: "#3e9cec",
urlTemplate: "https://twitter.com/{{NAME}}",
labelTemplate: "@{{NAME}}",
doesReverseResolution: false
},
{
name: "Facebook",
color: "#3b5998",
urlTemplate: "https://www.facebook.com/{{NAME}}",
labelTemplate: "{{NAME}}",
doesReverseResolution: false
},
{
name: "Instagram",
color: "#dc2477",
urlTemplate: "https://www.instagram.com/{{NAME}}",
labelTemplate: "@{{NAME}}",
doesReverseResolution: false
},
{
name: "GitHub",
color: "#0a0c10",
urlTemplate: "https://github.com/{{NAME}}",
labelTemplate: "@{{NAME}}",
doesReverseResolution: false
},
{
name: "Mail",
color: "#e7e7e7",
urlTemplate: "mailto:{{NAME}}",
labelTemplate: "{{NAME}}",
doesReverseResolution: false
},
{
name: "Linktree",
color: "#3ea195",
urlTemplate: "https://linktr.ee/{{NAME}}",
labelTemplate: "{{NAME}}",
doesReverseResolution: false
}
]
};
// src/drawing/frontmatterProcessor.ts
var frontmatterProcessor = (plugin) => (el, ctx) => __async(void 0, null, function* () {
const frontmatter = el.querySelector(".frontmatter");
if (frontmatter !== null) {
const embed = el.querySelector(".internal-embed");
if (embed !== null) {
return;
}
if (ctx.frontmatter) {
const siteDict = {};
plugin.settings.websites.forEach((config) => {
if (!Object.keys(siteDict).includes(config.name.toLowerCase())) {
siteDict[config.name.toLowerCase()] = [];
}
siteDict[config.name.toLowerCase()].push(config);
});
console.log(siteDict);
const accountList = [];
Object.keys(ctx.frontmatter).forEach((key) => {
if (Object.keys(siteDict).includes(key.toLowerCase())) {
const lk = key.toLowerCase();
siteDict[lk].forEach((config) => {
frontMatterRecursion(ctx.frontmatter[key], config, accountList);
});
}
});
const target = el.querySelector(".frontmatter-container");
if (accountList.length) {
target.innerHTML += `
<div class="frontmatter-section">
<span class="frontmatter-section-label">Accounts</span>
<div class="frontmatter-section-accounts">
</div>
</div>
`;
const section = target.querySelector(".frontmatter-section-accounts");
accountList.forEach((a) => {
const linkTag = section.createEl("a");
drawAccountLink(linkTag, a.config, a.value);
});
target.style.display = "block";
}
}
}
});
function frontMatterRecursion(value, config, accountList) {
if (typeof value === "string") {
accountList.push({
config,
value
});
} else {
value.forEach((v) => {
frontMatterRecursion(v, config, accountList);
});
}
}
// main.ts
var AccountLinker = class extends import_obsidian3.Plugin {
onload() {
return __async(this, null, function* () {
yield this.loadSettings();
this.registerMarkdownPostProcessor(frontmatterProcessor(this));
this.addSettingTab(new AccountLinkerSettingTab(this.app, this));
});
}
onunload() {
}
loadSettings() {
return __async(this, null, function* () {
this.settings = Object.assign({}, DEFAULT_SETTINGS, yield this.loadData());
});
}
saveSettings() {
return __async(this, null, function* () {
yield this.saveData(this.settings);
});
}
};