import type BratPlugin from "./main"; /** Simple checksum for tracking theme CSS changes */ function checksum(str: string): number { let sum = 0; for (let i = 0; i < str.length; i++) { sum += str.charCodeAt(i); } return sum; } export const checksumForString = (str: string): string => checksum(str).toString(); export interface ThemeInformation { repo: string; // checksum of theme file (either theme.css or theme-beta.css) lastUpdate: string; } export interface PluginVersion { repo: string; // `user/repo` (GitHub) or repository URL (Gitea) version: "latest" | string; // version of the plugin (semver or latest) /** @deprecated Tokens are now stored in SecretStorage (Obsidian 1.11.4+) */ token?: string; // optional private API key tokenName?: string; // name of secret in SecretStorage for per-repo token isIncompatible?: boolean; // if the plugin is incompatible } export interface Settings { pluginList: string[]; pluginSubListFrozenVersion: PluginVersion[]; themesList: ThemeInformation[]; updateAtStartup: boolean; updateThemesAtStartup: boolean; enableAfterInstall: boolean; loggingEnabled: boolean; loggingPath: string; loggingVerboseEnabled: boolean; debuggingMode: boolean; notificationsEnabled: boolean; /** @deprecated Tokens are now stored in SecretStorage (Obsidian 1.11.4+) */ personalAccessToken?: string; globalTokenName?: string; // name of secret in SecretStorage for global PAT selectLatestPluginVersionByDefault: boolean; allowIncompatiblePlugins: boolean; } export const DEFAULT_SETTINGS: Settings = { pluginList: [], pluginSubListFrozenVersion: [], themesList: [], updateAtStartup: true, updateThemesAtStartup: true, enableAfterInstall: true, loggingEnabled: false, loggingPath: "BRAT-log", loggingVerboseEnabled: false, debuggingMode: false, notificationsEnabled: true, globalTokenName: "", personalAccessToken: "", selectLatestPluginVersionByDefault: false, allowIncompatiblePlugins: false, }; /** * Adds a plugin for beta testing to the data.json file of this plugin * * @param plugin - the plugin object * @param repositoryPath - `user/repo` (GitHub) or repository URL (Gitea) * @param specifyVersion - if the plugin needs to stay at the frozen version, we need to also record the version * @param isIncompatible - if the plugin is incompatible * @param secretName - optional: name of secret in SecretStorage for this repo */ export function addBetaPluginToList( plugin: BratPlugin, repositoryPath: string, specifyVersion = "latest", isIncompatible = false, secretName = "", ): void { let save = false; if (!plugin.settings.pluginList.contains(repositoryPath)) { plugin.settings.pluginList.unshift(repositoryPath); save = true; } // If it's an existing frozen version plugin, update it instead of checking for duplicates const existingFrozenPlugin = plugin.settings.pluginSubListFrozenVersion.find( (p) => p.repo === repositoryPath, ); if (existingFrozenPlugin) { Object.assign(existingFrozenPlugin, { repo: repositoryPath, version: specifyVersion, token: undefined, // Don't store token in settings tokenName: secretName || existingFrozenPlugin.tokenName, isIncompatible: isIncompatible || undefined, }); save = true; } else { plugin.settings.pluginSubListFrozenVersion.unshift({ repo: repositoryPath, version: specifyVersion, token: undefined, // Don't store token in settings tokenName: secretName || undefined, isIncompatible: isIncompatible || undefined, }); save = true; } if (save) { void plugin.saveSettings(); } } /** * Tests if a plugin is in data.json * * @param plugin - the plugin object * @param repositoryPath - path to the GitHub repository * */ export function existBetaPluginInList( plugin: BratPlugin, repositoryPath: string, ): boolean { return plugin.settings.pluginList.contains(repositoryPath); } /** * Adds a theme for beta testing to the data.json file of this plugin * * @param plugin - the plugin object * @param repositoryPath - path to the GitHub repository * @param themeCss - raw text of the theme. It is checksummed and this is used for tracking if changes occurred to the theme * */ export function addBetaThemeToList( plugin: BratPlugin, repositoryPath: string, themeCss: string, ): void { const newTheme: ThemeInformation = { repo: repositoryPath, lastUpdate: checksumForString(themeCss), }; plugin.settings.themesList.unshift(newTheme); void plugin.saveSettings(); } /** * Tests if a theme is in data.json * * @param plugin - the plugin object * @param repositoryPath - path to the GitHub repository * */ export function existBetaThemeinInList( plugin: BratPlugin, repositoryPath: string, ): boolean { const testIfThemExists = plugin.settings.themesList.find( (t) => t.repo === repositoryPath, ); return !!testIfThemExists; } /** * Updates the token name for a plugin * * @param plugin - the plugin object * @param repositoryPath - path to the GitHub repository * @param tokenName - name of secret in SecretStorage for this repo (empty string to clear) */ export function updatePluginTokenName( plugin: BratPlugin, repositoryPath: string, tokenName: string, ): void { const existingFrozenPlugin = plugin.settings.pluginSubListFrozenVersion.find( (p) => p.repo === repositoryPath, ); if (existingFrozenPlugin) { existingFrozenPlugin.tokenName = tokenName || undefined; void plugin.saveSettings(); } } /** * Update the lastUpdate field for the theme * * @param plugin - the plugin object * @param repositoryPath - path to the GitHub repository * @param checksum - checksum of file. In past we used the date of file update, but this proved to not be consisent with the GitHub cache. * */ export function updateBetaThemeLastUpdateChecksum( plugin: BratPlugin, repositoryPath: string, checksum: string, ): void { for (const t of plugin.settings.themesList) { if (t.repo === repositoryPath) { t.lastUpdate = checksum; void plugin.saveSettings(); } } }