From 23fc950d7081a53a5c87f33176c852c89292e657 Mon Sep 17 00:00:00 2001 From: Mara Date: Fri, 24 May 2024 20:10:07 +0200 Subject: [PATCH 1/5] sanitize --- biome.json | 2 +- src/i18n/locales | 2 +- src/settings.ts | 467 ++++++++++-------------------- src/settings/help.ts | 675 +++++++++++++------------------------------ src/styles.css | 2 +- 5 files changed, 348 insertions(+), 800 deletions(-) diff --git a/biome.json b/biome.json index 3f172825..3c1093de 100644 --- a/biome.json +++ b/biome.json @@ -113,7 +113,7 @@ "enabled": true, "indentStyle": "tab", "indentWidth": 2, - "lineWidth": 90 + "lineWidth": 150 }, "javascript": { "formatter": { diff --git a/src/i18n/locales b/src/i18n/locales index 4105c68c..33bbd451 160000 --- a/src/i18n/locales +++ b/src/i18n/locales @@ -1 +1 @@ -Subproject commit 4105c68c60baa7369d409a38d064d47e57be143b +Subproject commit 33bbd45128099a5a353e1b9a878ba3a4ccef4575 diff --git a/src/settings.ts b/src/settings.ts index f5b53adc..76511d8d 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -1,44 +1,16 @@ -import { - EnumbSettingsTabId, - FolderSettings, - type GitHubPublisherSettings, - GithubTiersVersion, - type Repository, -} from "@interfaces"; +import { EnumbSettingsTabId, FolderSettings, type GitHubPublisherSettings, GithubTiersVersion, type Repository } from "@interfaces"; import i18next from "i18next"; -import { type App, Notice, PluginSettingTab, setIcon, Setting } from "obsidian"; +import { type App, Notice, PluginSettingTab, sanitizeHTMLToDom, setIcon, Setting } from "obsidian"; import type GithubPublisherPlugin from "src/main"; -import { - help, - KeyBasedOnSettings, - multipleRepoExplained, - supportMe, - usefulLinks, -} from "src/settings/help"; +import { help, KeyBasedOnSettings, multipleRepoExplained, supportMe, usefulLinks } from "src/settings/help"; import { migrateToken } from "src/settings/migrate"; -import { - ExportModal, - ImportLoadPreset, - ImportModal, - loadAllPresets, -} from "src/settings/modals/import_export"; +import { ExportModal, ImportLoadPreset, ImportModal, loadAllPresets } from "src/settings/modals/import_export"; import { ModalAddingNewRepository } from "src/settings/modals/manage_repo"; import { AutoCleanPopup } from "src/settings/modals/popup"; -import { - ModalRegexFilePathName, - ModalRegexOnContents, - OverrideAttachmentsModal, -} from "src/settings/modals/regex_edition"; +import { ModalRegexFilePathName, ModalRegexOnContents, OverrideAttachmentsModal } from "src/settings/modals/regex_edition"; import { TokenEditPath } from "src/settings/modals/token_path"; -import { - autoCleanCondition, - folderHideShowSettings, - showHideBasedOnFolder, -} from "src/settings/style"; -import { - checkRepositoryValidity, - verifyRateLimitAPI, -} from "src/utils/data_validation_test"; +import { autoCleanCondition, folderHideShowSettings, showHideBasedOnFolder } from "src/settings/style"; +import { checkRepositoryValidity, verifyRateLimitAPI } from "src/utils/data_validation_test"; export class GithubPublisherSettingsTab extends PluginSettingTab { plugin: GithubPublisherPlugin; @@ -112,13 +84,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .onClick(async () => { const octokit = await this.plugin.reloadOctokit(); const presetLists = await loadAllPresets(octokit.octokit, this.plugin); - new ImportLoadPreset( - this.app, - this.plugin, - presetLists, - octokit.octokit, - this - ).open(); + new ImportLoadPreset(this.app, this.plugin, presetLists, octokit.octokit, this).open(); }); }); const tabBar = containerEl.createEl("nav", { @@ -195,14 +161,8 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .setDesc(i18next.t("settings.github.apiType.desc")) .addDropdown((dropdown) => { dropdown - .addOption( - GithubTiersVersion.Free, - i18next.t("settings.github.apiType.dropdown.free") - ) - .addOption( - GithubTiersVersion.Entreprise, - i18next.t("settings.github.apiType.dropdown.enterprise") - ) + .addOption(GithubTiersVersion.Free, i18next.t("settings.github.apiType.dropdown.free")) + .addOption(GithubTiersVersion.Entreprise, i18next.t("settings.github.apiType.dropdown.enterprise")) .setValue(githubSettings.api.tiersForApi) .onChange(async (value) => { githubSettings.api.tiersForApi = value as GithubTiersVersion; @@ -300,14 +260,12 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { }) ); - new Setting(this.settingsPage) - .setName(i18next.t("settings.github.automaticallyMergePR")) - .addToggle((toggle) => - toggle.setValue(githubSettings.automaticallyMergePR).onChange(async (value) => { - githubSettings.automaticallyMergePR = value; - await this.plugin.saveSettings(); - }) - ); + new Setting(this.settingsPage).setName(i18next.t("settings.github.automaticallyMergePR")).addToggle((toggle) => + toggle.setValue(githubSettings.automaticallyMergePR).onChange(async (value) => { + githubSettings.automaticallyMergePR = value; + await this.plugin.saveSettings(); + }) + ); new Setting(this.settingsPage) .setName(i18next.t("settings.github.dryRun.enable.title")) .setDesc(i18next.t("settings.github.dryRun.enable.desc")) @@ -340,38 +298,20 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .setClass("connect-button") .onClick(async () => { const octokit = await this.plugin.reloadOctokit(); - this.settings.github.verifiedRepo = await checkRepositoryValidity( - octokit, - null, - null - ); - this.settings.github.rateLimit = await verifyRateLimitAPI( - octokit.octokit, - this.settings - ); + this.settings.github.verifiedRepo = await checkRepositoryValidity(octokit, null, null); + this.settings.github.rateLimit = await verifyRateLimitAPI(octokit.octokit, this.settings); await this.plugin.saveSettings(); }) ) .addButton((button) => - button - .setButtonText(i18next.t("settings.github.smartRepo.button")) - .onClick(async () => { - const repository: Repository[] = this.copy( - this.settings.github?.otherRepo ?? [] - ); - new ModalAddingNewRepository( - this.app, - this.settings, - this.branchName, - this.plugin, - repository, - async (result) => { - this.settings.github.otherRepo = result; - await this.plugin.saveSettings(); - this.plugin.reloadCommands(); - } - ).open(); - }) + button.setButtonText(i18next.t("settings.github.smartRepo.button")).onClick(async () => { + const repository: Repository[] = this.copy(this.settings.github?.otherRepo ?? []); + new ModalAddingNewRepository(this.app, this.settings, this.branchName, this.plugin, repository, async (result) => { + this.settings.github.otherRepo = result; + await this.plugin.saveSettings(); + this.plugin.reloadCommands(); + }).open(); + }) ); this.settingsPage.createEl("h3", { text: "GitHub Workflow" }); new Setting(this.settingsPage) @@ -430,13 +370,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .setValue(uploadSettings.behavior) .onChange(async (value: string) => { uploadSettings.behavior = value as FolderSettings; - await folderHideShowSettings( - frontmatterKeySettings, - rootFolderSettings, - autoCleanSetting, - value, - this.plugin - ); + await folderHideShowSettings(frontmatterKeySettings, rootFolderSettings, autoCleanSetting, value, this.plugin); await this.plugin.saveSettings(); this.renderSettingsPage(EnumbSettingsTabId.Upload); }); @@ -462,13 +396,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .setValue(uploadSettings.defaultName) .onChange(async (value) => { uploadSettings.defaultName = value.replace(/\/$/, ""); - await autoCleanCondition( - value, - autoCleanSetting, - this.plugin, - "defaultName", - this - ); + await autoCleanCondition(value, autoCleanSetting, this.plugin, "defaultName", this); await this.plugin.saveSettings(); }); }); @@ -494,13 +422,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .setValue(uploadSettings.rootFolder) .onChange(async (value) => { uploadSettings.rootFolder = value.replace(/\/$/, ""); - await autoCleanCondition( - value, - autoCleanSetting, - this.plugin, - "rootFolder", - this - ); + await autoCleanCondition(value, autoCleanSetting, this.plugin, "rootFolder", this); await this.plugin.saveSettings(); }); }); @@ -509,13 +431,11 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .setDesc(i18next.t("settings.upload.useFrontmatterTitle.desc")) .setClass("title") .addToggle((toggle) => { - toggle - .setValue(uploadSettings.frontmatterTitle.enable) - .onChange(async (value) => { - uploadSettings.frontmatterTitle.enable = value; - await this.plugin.saveSettings(); - this.renderSettingsPage(EnumbSettingsTabId.Upload); - }); + toggle.setValue(uploadSettings.frontmatterTitle.enable).onChange(async (value) => { + uploadSettings.frontmatterTitle.enable = value; + await this.plugin.saveSettings(); + this.renderSettingsPage(EnumbSettingsTabId.Upload); + }); }); if (uploadSettings.frontmatterTitle.enable) { frontmatterTitleSet.addText((text) => { @@ -543,20 +463,15 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { if (uploadSettings.behavior !== FolderSettings.Fixed) { allRegex = allRegex.concat(uploadSettings.replacePath); } - new ModalRegexFilePathName( - this.app, - this.settings, - this.copy(allRegex), - async (result) => { - uploadSettings.replacePath = result.filter((title) => { - return title.type === "path"; - }); - uploadSettings.replaceTitle = result.filter((title) => { - return title.type === "title"; - }); - await this.plugin.saveSettings(); - } - ).open(); + new ModalRegexFilePathName(this.app, this.settings, this.copy(allRegex), async (result) => { + uploadSettings.replacePath = result.filter((title) => { + return title.type === "path"; + }); + uploadSettings.replaceTitle = result.filter((title) => { + return title.type === "title"; + }); + await this.plugin.saveSettings(); + }).open(); }); }); @@ -581,38 +496,27 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { await this.plugin.saveSettings(); }); }); - new Setting(this.settingsPage) - .setName(i18next.t("settings.upload.folderNote.addTitle.title")) - .addToggle((toggle) => { - toggle - .setValue(uploadSettings.folderNote.addTitle.enable) + new Setting(this.settingsPage).setName(i18next.t("settings.upload.folderNote.addTitle.title")).addToggle((toggle) => { + toggle.setValue(uploadSettings.folderNote.addTitle.enable).onChange(async (value) => { + uploadSettings.folderNote.addTitle.enable = value; + await this.plugin.saveSettings(); + this.renderSettingsPage(EnumbSettingsTabId.Upload); + }); + }); + if (uploadSettings.folderNote.addTitle.enable) { + new Setting(this.settingsPage).setName(i18next.t("settings.upload.folderNote.addTitle.key")).addText((text) => { + text + .setPlaceholder("title") + .setValue(uploadSettings.folderNote.addTitle.key) .onChange(async (value) => { - uploadSettings.folderNote.addTitle.enable = value; + uploadSettings.folderNote.addTitle.key = value; await this.plugin.saveSettings(); - this.renderSettingsPage(EnumbSettingsTabId.Upload); }); }); - if (uploadSettings.folderNote.addTitle.enable) { - new Setting(this.settingsPage) - .setName(i18next.t("settings.upload.folderNote.addTitle.key")) - .addText((text) => { - text - .setPlaceholder("title") - .setValue(uploadSettings.folderNote.addTitle.key) - .onChange(async (value) => { - uploadSettings.folderNote.addTitle.key = value; - await this.plugin.saveSettings(); - }); - }); } } - showHideBasedOnFolder( - this.settings, - frontmatterKeySettings, - rootFolderSettings, - folderNoteSettings - ); + showHideBasedOnFolder(this.settings, frontmatterKeySettings, rootFolderSettings, folderNoteSettings); if (this.app.plugins.getPlugin("metadata-extractor")) { new Setting(this.settingsPage) @@ -636,8 +540,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { toggle.setValue(uploadSettings.autoclean.enable).onChange(async (value) => { if ( value && - ((this.settings.upload.behavior === "yaml" && - this.settings.upload.defaultName.length === 0) || + ((this.settings.upload.behavior === "yaml" && this.settings.upload.defaultName.length === 0) || this.settings.upload.rootFolder.length === 0) ) new AutoCleanPopup(this.app, this.settings, (result) => { @@ -672,22 +575,14 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .setName(i18next.t("settings.githubWorkflow.includeAttachments.title")) .setDesc(i18next.t("settings.githubWorkflow.includeAttachments.desc")) .addToggle((toggle) => { - toggle - .setValue(uploadSettings.autoclean.includeAttachments) - .onChange(async (value) => { - uploadSettings.autoclean.includeAttachments = value; - await this.plugin.saveSettings(); - }); + toggle.setValue(uploadSettings.autoclean.includeAttachments).onChange(async (value) => { + uploadSettings.autoclean.includeAttachments = value; + await this.plugin.saveSettings(); + }); }); } - folderHideShowSettings( - frontmatterKeySettings, - rootFolderSettings, - autoCleanSetting, - uploadSettings.behavior, - this.plugin - ); + folderHideShowSettings(frontmatterKeySettings, rootFolderSettings, autoCleanSetting, uploadSettings.behavior, this.plugin); } /** @@ -706,9 +601,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { text: i18next.t("settings.conversion.links.desc"), }); - const shareAll = this.settings.plugin.shareAll?.enable - ? ` ${i18next.t("settings.conversion.links.internals.shareAll")}` - : ""; + const shareAll = this.settings.plugin.shareAll?.enable ? ` ${i18next.t("settings.conversion.links.internals.shareAll")}` : ""; const internalLinksDesc = document.createDocumentFragment(); internalLinksDesc.createEl("p", { text: i18next.t("settings.conversion.links.internals.desc") + shareAll, @@ -756,11 +649,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { }); }); const slugifySetting = - typeof textSettings.links.slugify == "boolean" - ? textSettings.links.slugify - ? "strict" - : "disable" - : textSettings.links.slugify; + typeof textSettings.links.slugify == "boolean" ? (textSettings.links.slugify ? "strict" : "disable") : textSettings.links.slugify; if (textSettings.links.wiki || textSettings.links.internal) { new Setting(this.settingsPage) .setName(i18next.t("settings.conversion.links.slugify.title")) @@ -774,9 +663,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { }) .setValue(slugifySetting) .onChange(async (value) => { - textSettings.links.slugify = ["disable", "strict", "lower"].includes(value) - ? (value as "disable" | "strict" | "lower") - : "disable"; + textSettings.links.slugify = ["disable", "strict", "lower"].includes(value) ? (value as "disable" | "strict" | "lower") : "disable"; await this.plugin.saveSettings(); }); }); @@ -800,9 +687,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .setDesc(i18next.t("settings.conversion.dataview.desc")) .addToggle((toggle) => { toggle - .setValue( - textSettings.dataview && isDataviewEnabled && textSettings.links.internal - ) + .setValue(textSettings.dataview && isDataviewEnabled && textSettings.links.internal) .setDisabled(!isDataviewEnabled || !textSettings.links.internal) .onChange(async (value) => { textSettings.dataview = value; @@ -887,28 +772,24 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { cls: "center", }); - new Setting(this.settingsPage) - .setName(i18next.t("settings.embed.transferImage.title")) - .addToggle((toggle) => { - toggle.setValue(embedSettings.attachments).onChange(async (value) => { - embedSettings.attachments = value; - await this.plugin.saveSettings(); - this.renderSettingsPage(EnumbSettingsTabId.Embed); - }); + new Setting(this.settingsPage).setName(i18next.t("settings.embed.transferImage.title")).addToggle((toggle) => { + toggle.setValue(embedSettings.attachments).onChange(async (value) => { + embedSettings.attachments = value; + await this.plugin.saveSettings(); + this.renderSettingsPage(EnumbSettingsTabId.Embed); }); + }); if (embedSettings.attachments) { new Setting(this.settingsPage) .setName(i18next.t("settings.embed.imagePath.title")) .setDesc(i18next.t("settings.embed.imagePath.desc")) .addToggle((toggle) => { - toggle - .setValue(embedSettings.useObsidianFolder ?? false) - .onChange(async (value) => { - embedSettings.useObsidianFolder = value; - await this.plugin.saveSettings(); - this.renderSettingsPage(EnumbSettingsTabId.Embed); - }); + toggle.setValue(embedSettings.useObsidianFolder ?? false).onChange(async (value) => { + embedSettings.useObsidianFolder = value; + await this.plugin.saveSettings(); + this.renderSettingsPage(EnumbSettingsTabId.Embed); + }); }); if (!embedSettings.useObsidianFolder) { new Setting(this.settingsPage) @@ -930,15 +811,10 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .setDesc(i18next.t("settings.embed.overrides.desc")) .addButton((button) => { button.setIcon("pencil").onClick(async () => { - new OverrideAttachmentsModal( - this.app, - this.settings, - this.copy(embedSettings.overrideAttachments), - async (result) => { - embedSettings.overrideAttachments = result; - await this.plugin.saveSettings(); - } - ).open(); + new OverrideAttachmentsModal(this.app, this.settings, this.copy(embedSettings.overrideAttachments), async (result) => { + embedSettings.overrideAttachments = result; + await this.plugin.saveSettings(); + }).open(); }); }); @@ -1003,11 +879,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .addOption("bake", i18next.t("settings.embed.links.dp.bake")) .setValue(embedSettings.convertEmbedToLinks ?? "keep") .onChange(async (value) => { - embedSettings.convertEmbedToLinks = value as - | "keep" - | "remove" - | "links" - | "bake"; + embedSettings.convertEmbedToLinks = value as "keep" | "remove" | "links" | "bake"; await this.plugin.saveSettings(); await this.renderEmbedConfiguration(); }); @@ -1065,25 +937,19 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { cls: ["warning", "embed"], }); - new Setting(this.settingsPage) - .setName(i18next.t("settings.embed.bake.textBefore.title")) - .addTextArea((text) => { - text - .setValue(embedSettings.bake?.textBefore ?? "") - .onChange(async (value) => { - embedSettings.bake!.textBefore = value; - await this.plugin.saveSettings(); - }); + new Setting(this.settingsPage).setName(i18next.t("settings.embed.bake.textBefore.title")).addTextArea((text) => { + text.setValue(embedSettings.bake?.textBefore ?? "").onChange(async (value) => { + embedSettings.bake!.textBefore = value; + await this.plugin.saveSettings(); }); + }); - new Setting(this.settingsPage) - .setName(i18next.t("settings.embed.bake.textAfter.title")) - .addTextArea((text) => { - text.setValue(embedSettings.bake?.textAfter ?? "").onChange(async (value) => { - embedSettings.bake!.textAfter = value; - await this.plugin.saveSettings(); - }); + new Setting(this.settingsPage).setName(i18next.t("settings.embed.bake.textAfter.title")).addTextArea((text) => { + text.setValue(embedSettings.bake?.textAfter ?? "").onChange(async (value) => { + embedSettings.bake!.textAfter = value; + await this.plugin.saveSettings(); }); + }); } } } @@ -1099,17 +965,15 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .setName(i18next.t("settings.plugin.shareKey.all.title")) .setDesc(i18next.t("settings.plugin.shareKey.all.desc")) .addToggle((toggle) => - toggle - .setValue(pluginSettings.shareAll?.enable ?? false) - .onChange(async (value) => { - pluginSettings.shareAll = { - enable: value, - excludedFileName: pluginSettings.shareAll?.excludedFileName ?? "DRAFT", - }; - if (value) this.settings.conversion.links.internal = true; - await this.plugin.saveSettings(); - this.renderSettingsPage(EnumbSettingsTabId.Plugin); - }) + toggle.setValue(pluginSettings.shareAll?.enable ?? false).onChange(async (value) => { + pluginSettings.shareAll = { + enable: value, + excludedFileName: pluginSettings.shareAll?.excludedFileName ?? "DRAFT", + }; + if (value) this.settings.conversion.links.internal = true; + await this.plugin.saveSettings(); + this.renderSettingsPage(EnumbSettingsTabId.Plugin); + }) ); if (!pluginSettings.shareAll || !pluginSettings.shareAll.enable) { new Setting(this.settingsPage) @@ -1125,17 +989,15 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { }) ); } else { - new Setting(this.settingsPage) - .setName(i18next.t("settings.plugin.shareKey.excludedFileName.title")) - .addText((text) => - text - .setPlaceholder("DRAFT") - .setValue(pluginSettings.shareAll?.excludedFileName ?? "DRAFT") - .onChange(async (value) => { - pluginSettings.shareAll!.excludedFileName = value.trim(); - await this.plugin.saveSettings(); - }) - ); + new Setting(this.settingsPage).setName(i18next.t("settings.plugin.shareKey.excludedFileName.title")).addText((text) => + text + .setPlaceholder("DRAFT") + .setValue(pluginSettings.shareAll?.excludedFileName ?? "DRAFT") + .onChange(async (value) => { + pluginSettings.shareAll!.excludedFileName = value.trim(); + await this.plugin.saveSettings(); + }) + ); } new Setting(this.settingsPage) @@ -1235,34 +1097,25 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .setName(i18next.t("settings.plugin.copyLink.toUri.title")) .setDesc(i18next.t("settings.plugin.copyLink.toUri.desc")) .addToggle((toggle) => - toggle - .setValue(pluginSettings.copyLink.transform.toUri) - .onChange(async (value) => { - pluginSettings.copyLink.transform.toUri = value; - await this.plugin.saveSettings(); - }) + toggle.setValue(pluginSettings.copyLink.transform.toUri).onChange(async (value) => { + pluginSettings.copyLink.transform.toUri = value; + await this.plugin.saveSettings(); + }) ); - new Setting(this.settingsPage) - .setName(i18next.t("settings.plugin.copyLink.slugify.title")) - .addDropdown((dropdown) => { - dropdown - .addOptions({ - disable: i18next.t("settings.plugin.copyLink.slugify.disable"), - strict: i18next.t("settings.plugin.copyLink.slugify.strict"), - lower: i18next.t("settings.plugin.copyLink.slugify.lower"), - }) - .setValue( - pluginSettings.copyLink.transform.slugify as "disable" | "strict" | "lower" - ) - .onChange(async (value) => { - pluginSettings.copyLink.transform.slugify = value as - | "disable" - | "strict" - | "lower"; - await this.plugin.saveSettings(); - }); - }); + new Setting(this.settingsPage).setName(i18next.t("settings.plugin.copyLink.slugify.title")).addDropdown((dropdown) => { + dropdown + .addOptions({ + disable: i18next.t("settings.plugin.copyLink.slugify.disable"), + strict: i18next.t("settings.plugin.copyLink.slugify.strict"), + lower: i18next.t("settings.plugin.copyLink.slugify.lower"), + }) + .setValue(pluginSettings.copyLink.transform.slugify as "disable" | "strict" | "lower") + .onChange(async (value) => { + pluginSettings.copyLink.transform.slugify = value as "disable" | "strict" | "lower"; + await this.plugin.saveSettings(); + }); + }); new Setting(this.settingsPage) .setName(i18next.t("settings.plugin.copyLink.applyRegex.title")) @@ -1307,24 +1160,19 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .setClass("max-width") .addExtraButton((button) => { button.setIcon("trash").onClick(async () => { - pluginSettings.copyLink.transform.applyRegex = - pluginSettings.copyLink.transform.applyRegex.filter( - (item) => item !== apply - ); + pluginSettings.copyLink.transform.applyRegex = pluginSettings.copyLink.transform.applyRegex.filter((item) => item !== apply); await this.plugin.saveSettings(); this.renderSettingsPage(EnumbSettingsTabId.Plugin); }); }); } - new Setting(this.settingsPage) - .setName(i18next.t("settings.plugin.copyLink.command.desc")) - .addToggle((toggle) => - toggle.setValue(pluginSettings.copyLink.addCmd).onChange(async (value) => { - pluginSettings.copyLink.addCmd = value; - await this.plugin.saveSettings(); - }) - ); + new Setting(this.settingsPage).setName(i18next.t("settings.plugin.copyLink.command.desc")).addToggle((toggle) => + toggle.setValue(pluginSettings.copyLink.addCmd).onChange(async (value) => { + pluginSettings.copyLink.addCmd = value; + await this.plugin.saveSettings(); + }) + ); } this.settingsPage.createEl("h3", { text: i18next.t("settings.plugin.head.other") }); @@ -1333,12 +1181,10 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .setName(i18next.t("settings.plugin.embedEditRepo.title")) .setDesc(i18next.t("settings.plugin.embedEditRepo.desc")) .addToggle((toggle) => - toggle - .setValue(pluginSettings.displayModalRepoEditing) - .onChange(async (value) => { - pluginSettings.displayModalRepoEditing = value; - await this.plugin.saveSettings(); - }) + toggle.setValue(pluginSettings.displayModalRepoEditing).onChange(async (value) => { + pluginSettings.displayModalRepoEditing = value; + await this.plugin.saveSettings(); + }) ); new Setting(this.settingsPage) @@ -1347,9 +1193,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { .addToggle((toggle) => toggle.setValue(pluginSettings.saveTabId ?? true).onChange(async (value) => { pluginSettings.saveTabId = value; - this.settings.tabsId = value - ? EnumbSettingsTabId.Plugin - : EnumbSettingsTabId.Github; + this.settings.tabsId = value ? EnumbSettingsTabId.Plugin : EnumbSettingsTabId.Github; await this.plugin.saveSettings(); }) ); @@ -1381,35 +1225,22 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { * Render the help page */ renderHelp() { - this.settingsPage.createEl("h2", { - text: i18next.t("settings.help.usefulLinks.title"), - }); + new Setting(this.settingsPage).setName(i18next.t("settings.help.usefulLinks.title")).setHeading(); this.settingsPage.appendChild(usefulLinks()); this.settingsPage.createEl("hr"); - this.settingsPage.createEl("h2", { - text: i18next.t("settings.help.frontmatter.title"), - }); - this.settingsPage.createEl("p", { - text: i18next.t("settings.help.frontmatter.desc"), - }); - this.settingsPage - .createEl("p", { - text: i18next.t("settings.help.frontmatter.nestedKey"), - }) - .createEl("code", { - text: "key.subkey: value", - }) - .createEl("span", { - text: ".", - }); + new Setting(this.settingsPage).setName(i18next.t("settings.help.frontmatter.title")).setHeading(); + const dom = sanitizeHTMLToDom(` +

${i18next.t("settings.help.frontmatter.desc")}

+

${i18next.t("settings.help.frontmatter.nestedKey")} key.subkey: value.

`); + this.settingsPage.appendChild(dom); this.settingsPage.createEl("pre", { cls: "language-yaml" }).createEl("code", { text: KeyBasedOnSettings(this.settings), cls: "language-yaml", }); this.settingsPage.appendChild(help(this.settings)); - this.settingsPage.createEl("h2", { - text: i18next.t("settings.help.multiRepoHelp.title"), - }); + + this.settingsPage.createEl("hr"); + new Setting(this.settingsPage).setName(i18next.t("settings.help.multiRepoHelp.title")).setHeading(); this.settingsPage.appendChild(multipleRepoExplained(this.settings)); this.settingsPage.appendChild(supportMe()); } diff --git a/src/settings/help.ts b/src/settings/help.ts index a7d0a5ef..8bc327b4 100644 --- a/src/settings/help.ts +++ b/src/settings/help.ts @@ -1,419 +1,174 @@ import { FolderSettings, type GitHubPublisherSettings } from "@interfaces"; +import dedent from "dedent"; import i18next from "i18next"; -import { normalizePath } from "obsidian"; +import { normalizePath, sanitizeHTMLToDom } from "obsidian"; import { regexOnPath } from "src/conversion/file_path"; -function spanAtRule( - text: string, - code: DocumentFragment, - br: boolean = true -): HTMLElement { - if (br) code.createEl("br"); - return code.createEl("span", { text, cls: ["token", "key", "atrule"] }); -} - -function spanBoolean(text: boolean, code: DocumentFragment): HTMLElement { - const textToString = text ? "true" : "false"; - return code.createEl("span", { - text: textToString, - cls: ["token", "boolean", "important"], - }); -} - -function spanComment(text: string, code: DocumentFragment): HTMLElement { - return code.createEl("span", { text, cls: ["token", "comment"] }); -} - -function spanString(text: string, code: DocumentFragment): HTMLElement { - return code.createEl("span", { text, cls: ["token", "string"] }); -} - -function spanCategory(settings: GitHubPublisherSettings, code: DocumentFragment) { - if (settings.upload.behavior === FolderSettings.Yaml) { - const defaultPath = - settings.upload.defaultName.length > 0 ? `${settings.upload.defaultName}` : "/"; - return { - rule: spanAtRule( - settings.upload.yamlFolderKey.length > 0 - ? `${settings.upload.yamlFolderKey}: ` - : "category: ", - code - ), - token: spanString(normalizePath(defaultPath), code), - }; - } -} - /** * Export the YAML help to create an example of yaml with the value based on the Settings - */ + * YAML: + * ```yaml + * share: true + * path: file.md #given as an example path + * links: + * mdlinks: true + * convert: true + * internals: true + * nonShared: true + * embed: + * send: true + * remove: keep + * char: -> + * attachment: + * send: true + * folder: docs/images + * dataview: true + * hardBreak: false + * includeLinks: true + * shortRepo: test1 + * repo: + * owner: sandboxingRepo + * repo: obsidian-sandbox + * branch: main + * autoclean: true + * copylink: + * base: https://obsidian-sandbox.github.io/obsidian-sandbox +``` +*/ export function KeyBasedOnSettings(settings: GitHubPublisherSettings): DocumentFragment { - const code = document.createDocumentFragment(); - const defaultPath = - settings.upload.defaultName.length > 0 ? `${settings.upload.defaultName}` : "/"; + const defaultPath = settings.upload.defaultName.length > 0 ? `${settings.upload.defaultName}` : "/"; let path = settings.upload.behavior === FolderSettings.Yaml - ? `${ - settings.upload.rootFolder.length > 0 ? settings.upload.rootFolder : "" - }/${defaultPath}/file.md` + ? `${settings.upload.rootFolder.length > 0 ? settings.upload.rootFolder : ""}/${defaultPath}/file.md` : `${defaultPath}/file.md`; path = normalizePath(regexOnPath(path, settings)); - - spanAtRule(`${settings.plugin.shareKey}: `, code, false); - spanBoolean(true, code); - spanCategory(settings, code); - spanAtRule("path: ", code); - spanString(path, code); - spanComment(" #given as an example path", code); - spanAtRule("links: ", code); - spanAtRule(" mdlinks: ", code); - spanBoolean(settings.conversion.links.wiki, code); - spanAtRule(" convert: ", code); - spanBoolean(true, code); - spanAtRule(" internals: ", code); - spanBoolean(settings.conversion.links.internal, code); - spanAtRule(" nonShared: ", code); - spanBoolean(settings.conversion.links.unshared, code); - spanAtRule("embed: ", code); - spanAtRule(" send: ", code); - spanBoolean(settings.embed.notes, code); - spanAtRule(" remove: ", code); - spanString(settings.embed.convertEmbedToLinks, code); - spanAtRule(" char: ", code); - spanString(settings.embed.charConvert, code); - spanAtRule("attachment: ", code); - spanAtRule(" send: ", code); - spanBoolean(settings.embed.attachments, code); - spanAtRule(" folder: ", code); - spanString(settings.embed.folder, code); - spanAtRule("dataview: ", code); - spanBoolean(settings.conversion.dataview, code); - spanAtRule("hardBreak: ", code); - spanBoolean(settings.conversion.hardbreak, code); - spanAtRule("includeLinks: ", code); - spanBoolean(settings.embed.sendSimpleLinks, code); + const rules = "token key atrule"; + const comments = "token comment"; + const str = "token string"; + const boolean = "token boolean important"; + const category = () => { + if (settings.upload.behavior === FolderSettings.Yaml) { + const defaultPath = settings.upload.defaultName.length > 0 ? `${settings.upload.defaultName}` : "/"; + return dedent(` +
${ + settings.upload.yamlFolderKey.length > 0 ? `${settings.upload.yamlFolderKey}: ` : "category: " + }${normalizePath(defaultPath)} + `); + } + return ""; + }; + let html = dedent(`${settings.plugin.shareKey}: true${category()} + path: file.md # given as an example path + links: + mdlinks: ${settings.conversion.links.wiki} + convert: true + internals: ${settings.conversion.links.internal} + nonShared: ${settings.conversion.links.unshared} + embed: + send: ${settings.embed.notes} + remove: ${settings.embed.convertEmbedToLinks} + char: ${settings.embed.charConvert} + attachment: + send: ${settings.embed.attachments} + folder: ${settings.embed.folder} + dataview: ${settings.conversion.dataview} + hardBreak: ${settings.conversion.hardbreak} + includeLinks: ${settings.embed.sendSimpleLinks} + `); if (settings.github.otherRepo.length > 0) { - spanAtRule("shortRepo: ", code); - spanString( - settings.github.otherRepo[0].smartKey.length > 0 - ? settings.github.otherRepo[0].smartKey - : "smartkey", - code - ); + console.log(settings.github.otherRepo[0].smartKey); + const smartkey = settings.github.otherRepo[0].smartKey.length > 0 ? settings.github.otherRepo[0].smartKey : "smartkey"; + html += dedent(`
shortRepo: ${smartkey}`); } - spanAtRule("repo: ", code); - spanAtRule(" owner: ", code); - spanString(settings.github.user, code); - spanAtRule(" repo: ", code); - spanString(settings.github.repo, code); - spanAtRule(" branch: ", code); - spanString(settings.github.branch, code); - spanAtRule(" autoclean: ", code); - spanBoolean(settings.upload.autoclean.enable, code); - spanAtRule("copylink: ", code); - spanAtRule(" base: ", code); - spanString( - settings.plugin.copyLink.links.length > 0 - ? settings.plugin.copyLink.links - : `https://${settings.github.repo}.github.io/${settings.github.repo}`, - code - ); - const removePart = settings.plugin.copyLink.removePart - .map((val) => `"${val}"`) - .join(", "); + html += dedent(`
repo: + owner: ${settings.github.user} + repo: ${settings.github.repo} + branch: ${settings.github.branch} + autoclean: ${settings.upload.autoclean.enable} + copylink: + base: ${ + settings.plugin.copyLink.links.length > 0 ? settings.plugin.copyLink.links : `https://${settings.github.repo}.github.io/${settings.github.repo}` + } + `); + const removePart = settings.plugin.copyLink.removePart.map((val) => `"${val}"`).join(", "); if (removePart.length > 0) { - spanAtRule(" remove: ", code); - spanString(removePart, code); + html += dedent(` remove: ${removePart}`); } - return code; + html += "
"; + return sanitizeHTMLToDom(html); } /** * Create the contents of the help settings tab */ export function help(settings: GitHubPublisherSettings) { - const explanation = document.createDocumentFragment(); - explanation.createEl("ul", undefined, (span) => { - span.createEl("li", undefined, (span) => { - span.createEl("code", { - text: `${settings.plugin.shareKey}${i18next.t("common.points")}`, - cls: "code-title", - }); - span.createEl("span", { - text: `${i18next.t("settings.help.frontmatter.share.title")}`, - }); - span.createEl("ul", undefined, (l) => { - l.createEl("span", { text: i18next.t("settings.help.frontmatter.share.other") }); - }); - }); - span.createEl("li", undefined, (span) => { - span.createEl("code", { - text: `path${i18next.t("common.points")}`, - cls: "code-title", - }); - span.createEl("span", { - text: ` ${i18next.t("settings.help.frontmatter.path")}`, - }); - }); - span.createEl("li", undefined, (span) => { - span.createEl("code", { - text: `links${i18next.t("common.points")}`, - cls: "code-title", - }); - }); - span.createEl("ul", undefined, (l) => { - l.createEl("li", undefined, (p) => { - p.createEl("code", { text: "mdlinks" }); - p.createEl("span", { - text: `${i18next.t("common.points")}${i18next.t( + const els = dedent(` + + `); + return sanitizeHTMLToDom(els); } /** @@ -421,105 +176,67 @@ export function help(settings: GitHubPublisherSettings) { * @return {DocumentFragment} */ export function usefulLinks(): DocumentFragment { - const usefulLinks = document.createDocumentFragment(); - usefulLinks.createEl("ul", undefined, (el) => { - el.createEl("li", undefined, (el) => { - el.createEl("a", { - text: i18next.t("settings.help.usefulLinks.documentation"), - href: i18next.t("settings.help.usefulLinks.links"), - }); - }); - el.createEl("li", undefined, (el) => { - el.createEl("a", { - text: i18next.t("common.repository"), - href: "https://github.com/ObsidianPublisher/obsidian-github-publisher", - }); - }); - el.createEl("li", undefined, (el) => { - el.createEl("a", { - text: i18next.t("settings.help.usefulLinks.issue"), - href: "https://github.com/ObsidianPublisher/obsidian-github-publisher/issues", - }); - }); - el.createEl("li", undefined, (el) => { - el.createEl("a", { - text: i18next.t("settings.help.usefulLinks.discussion"), - href: "https://github.com/ObsidianPublisher/obsidian-github-publisher/discussions", - }); - }); - }); - return usefulLinks; + const els = dedent(` +