Refactoring code for maintaining versions.

This commit is contained in:
Andrea Alberti 2024-08-09 10:59:23 +02:00
parent e68d0af66b
commit 5a93cef1bf
10 changed files with 396 additions and 103 deletions

View file

@ -0,0 +1,6 @@
{
"dataview-recent-files": "markdown:\nMy own plugin to show the recently modified files.\n[[2024-06-13 Survey group culture]]\n\n![[00 Meta/Misc/Test.jpg]]\n\n[[2024-08-05 Review Cs-based Rydberg quantum computers]]",
"dataview": "Super-useful plugin that allows querying the notes to provide database functionality. 🔥🔥🔥",
"highlightr-plugin": "Allows highlighting the markdown text.",
"obsidian-completr": "Allows auto-completion of commands. Very useful."
}

View file

@ -0,0 +1,19 @@
{
"annotations":
{
"dataview-recent-files": "markdown:\nMy own plugin to show the recently modified files.\n[[2024-06-13 Survey group culture]]\n\n![[00 Meta/Misc/Test.jpg]]\n\n[[2024-08-05 Review Cs-based Rydberg quantum computers]]",
"dataview": "Super-useful plugin that allows querying the notes to provide database functionality. 🔥🔥🔥",
"highlightr-plugin": "Allows highlighting the markdown text.",
"obsidian-completr": "Allows auto-completion of commands. Very useful."
},
"plugins_annotations_uuid": "FAA70013-38E9-4FDF-B06A-F899F6487C19",
"hide_placeholders": true,
"delete_placeholder_string_on_insertion": true,
"label_mobile": "<b>Annotation:&nbsp;</b>",
"label_desktop": "<b>Personal annotation:&nbsp;</b>",
"label_placeholder": "<em>Add your personal comment about <strong>${plugin_name}</strong> here...</em>",
"editable": true,
"automatic_remove": false,
"markdown_file_path": "00 Meta/Misc/Plugins annotations.md",
"markdown_notes": false
}

View file

@ -0,0 +1,235 @@
{
"annotations":
{
"dataview-recent-files":
{
"name": "Dataview Recent Files",
"anno": "markdown:\nMy own plugin to show the recently modified files.\n[[2024-06-13 Survey group culture]]\n\n![[00 Meta/Misc/Test.jpg]]\n\n[[2024-08-05 Review Cs-based Rydberg quantum computers]]"
},
"dataview":
{
"name": "Dataview",
"anno": "Super-useful plugin that allows querying the notes to provide database functionality. 🔥🔥🔥"
},
"highlightr-plugin":
{
"name": "Highlightr",
"anno": "Allows highlighting the markdown text."
},
"obsidian-completr":
{
"name": "Completr",
"anno": "Allows auto-completion of commands. Very useful."
},
"find-unlinked-files":
{
"name": "Find orphaned files and broken links",
"anno": "Very useful to find orphaned files and broken links."
},
"homepage":
{
"name": "Homepage",
"anno": "Useful to show a customized homepage."
},
"obsidian-icon-folder":
{
"name": "Iconize",
"anno": "Useful to customize icons."
},
"import-attachments-plus":
{
"name": "Import Attachments+",
"anno": "My own plugin to import attachments by moving them into the vault."
},
"obsidian-languagetool-plugin":
{
"name": "LanguageTool Integration",
"anno": "Very useful as spell checker."
},
"languagetool":
{
"name": "LanguageTool Checker",
"anno": "It is claimed to be better but it does not work. It is still in beta function."
},
"number-headings-obsidian":
{
"name": "Number Headings",
"anno": "Very useful to add numbers to the headings."
},
"pdf-plus":
{
"name": "PDF++",
"anno": "Very useful to comment PDF documents in Obsidian."
},
"templater-obsidian":
{
"name": "Templater",
"anno": "Very useful to define templates."
},
"obsidian-tasks-plugin":
{
"name": "Tasks",
"anno": "Not really useful. Never used so far."
},
"obsidian-style-settings":
{
"name": "Style Settings",
"anno": "Super useful to customize the style with CCS and be able to modify the settings from Obsidian."
},
"obsidian-smart-typography":
{
"name": "Smart Typography",
"anno": "Semi useful to improve typography of symbols."
},
"wikilinks-to-mdlinks-obsidian":
{
"name": "Wikilinks to MDLinks",
"anno": "Not really needed. It converts Wikilinks to MD links."
},
"webpage-html-export":
{
"name": "Webpage HTML Export",
"anno": "Super useful to export in HTML. It is a bit clunky to load. It is per default disabled, unless I need it."
},
"frontmatter-modified-date":
{
"name": "Update modified date",
"anno": "Very useful to update the property `modified date`."
},
"supercharged-links-obsidian":
{
"name": "Supercharged Links",
"anno": "The idea is very nice but then the MD notes are not really readable outside of Obsidian. Disabled for now."
},
"tag-wrangler":
{
"name": "Tag Wrangler",
"anno": "Very useful to rename tags across all notes."
},
"sheets":
{
"name": "Sheets Extended",
"anno": "Useful to extend tables and merge cells."
},
"obsidian-html-plugin":
{
"name": "HTML Reader",
"anno": "It allows opening HTML files in Obsidian."
},
"obsidian-advanced-uri":
{
"name": "Advanced URI",
"anno": "Very useful to copy URI."
},
"callout-manager":
{
"name": "Callout Manager",
"anno": "Offers a GUI to customize callouts."
},
"obsidian-file-path-to-uri":
{
"name": "File path to URI",
"anno": "Not really needed. It converts a file path to a URI."
},
"obsidian-text-expander-js":
{
"name": "Inline Scripts",
"anno": "Quite powerful to run inline scripts, and replace predefined macros. However, not really used."
},
"omnisearch":
{
"name": "Omnisearch",
"anno": "Powerful tool to search through the vault."
},
"obsidian-outliner":
{
"name": "Outliner",
"anno": "Very handy to operate on outlines."
},
"url-into-selection":
{
"name": "Paste URL into selection",
"anno": "Not really needed. It causes more confusion when pasting on existing selected text."
},
"periodic-notes":
{
"name": "Periodic Notes",
"anno": "Manages daily notes. Just used for daily notes."
},
"settings-search":
{
"name": "Settings Search",
"anno": "Adds a search field with a fuzzy search functionality to search through Obsidian settings."
},
"text-extractor":
{
"name": "Text Extractor",
"anno": "I did not really test it how good it works."
},
"file-explorer-note-count":
{
"name": "File Explorer Note Count",
"anno": "Very useful to display the number of notes in the various folders."
},
"plugins-annotations":
{
"name": "Plugins Annotations",
"anno": "My own plugin to take notes about installed plugins."
},
"editing-toolbar":
{
"name": "Editing Toolbar",
"anno": "Nice toolbar shown above the notes. Quite handy."
},
"obsidian-spaced-repetition":
{
"name": "Spaced Repetition",
"anno": "Super effective plugin to learn flashcards."
},
"table-editor-obsidian":
{
"name": "Advanced Tables",
"anno": "A must to operate on tables in Obsidian. 🚀🚀🚀"
},
"settings-management":
{
"name": "Settings Management",
"anno": "Enhanced settings with multiple configurations."
},
"obsidian-admonition":
{
"name": "Admonition",
"anno": "Improve callouts 🖋️. They can be entered as `codeblocks`."
},
"obsidian-divide-and-conquer":
{
"name": "Divide & Conquer",
"anno": "Plugin useful to debug problems with plugins. Generally, it can be left disabled."
},
"js-engine":
{
"name": "JS Engine",
"anno": "Allows executing Javascript dynamically in the note."
},
"plugin-reloader":
{
"name": "Plugin Reloader",
"anno": "Allows reloading a specific plugin without restarting Obsidian. Hotkeys configurable."
},
"obsidian-kanban":
{
"name": "Kanban",
"anno": "Plugin for the Kanban 🗓 organization."
}
},
"plugins_annotations_uuid": "B265C5B2-A6AD-4194-9E4C-C1327DB1EA18",
"hide_placeholders": true,
"delete_placeholder_string_on_insertion": true,
"label_mobile": "<b>Annotation:&nbsp;</b>",
"label_desktop": "<b>Personal annotation:&nbsp;</b>",
"label_placeholder": "<em>Add your personal comment about <strong>${plugin_name}</strong> here...</em>",
"editable": true,
"automatic_remove": false,
"markdown_file_path": "00 Meta/Misc/Plugins annotations.md",
"markdown_notes": false
}

View file

@ -1,22 +1,5 @@
// defaults.ts
import { PluginsAnnotationsSettingsWithoutNames, PluginsAnnotationsSettings } from './types';
// import { PluginsAnnotationsSettings_1_4_0 } from './types_legacy';
export const DEFAULT_SETTINGS: PluginsAnnotationsSettings = {
annotations: {},
plugins_annotations_uuid: 'B265C5B2-A6AD-4194-9E4C-C1327DB1EA18',
hide_placeholders: false,
delete_placeholder_string_on_insertion: false,
label_mobile: '<b>Annotation:&nbsp;</b>',
label_desktop: '<b>Personal annotation:&nbsp;</b>',
label_placeholder : "<em>Add your personal comment about <strong>${plugin_name}</strong> here...</em>",
editable: true,
automatic_remove: false,
markdown_file_path: '',
}
export const DEFAULT_SETTINGS_WITHOUT_NAMES: PluginsAnnotationsSettingsWithoutNames = {
...DEFAULT_SETTINGS,
annotations: {}, // Override the annotations property with the appropriate type
plugins_annotations_uuid: 'FAA70013-38E9-4FDF-B06A-F899F6487C19',
};
export const a = 1;

22
src/defaults_legacy.ts Normal file
View file

@ -0,0 +1,22 @@
// defaults.ts
import { PluginsAnnotationsSettings_1_3_0, PluginsAnnotationsSettings_1_4_0 } from './types_legacy';
export const DEFAULT_SETTINGS_1_4_0: PluginsAnnotationsSettings_1_4_0 = {
annotations: {},
plugins_annotations_uuid: 'B265C5B2-A6AD-4194-9E4C-C1327DB1EA18',
hide_placeholders: false,
delete_placeholder_string_on_insertion: false,
label_mobile: '<b>Annotation:&nbsp;</b>',
label_desktop: '<b>Personal annotation:&nbsp;</b>',
label_placeholder : "<em>Add your personal comment about <strong>${plugin_name}</strong> here...</em>",
editable: true,
automatic_remove: false,
markdown_file_path: '',
}
export const DEFAULT_SETTINGS_1_3_0: PluginsAnnotationsSettings_1_3_0 = {
...DEFAULT_SETTINGS_1_4_0,
annotations: {}, // Override the annotations property with the appropriate type
plugins_annotations_uuid: 'FAA70013-38E9-4FDF-B06A-F899F6487C19',
};

View file

@ -14,14 +14,15 @@ import {
// App,
} from 'obsidian';
import { around } from 'monkey-around';
import { PluginAnnotationDict, PluginsAnnotationsSettingsWithoutNames, isPluginAnnotationDictWithoutNames, isPluginsAnnotationsSettings, isPluginsAnnotationsSettingsWithoutNames, PluginsAnnotationsSettings, AnnotationType } from './types';
import { DEFAULT_SETTINGS, DEFAULT_SETTINGS_WITHOUT_NAMES } from './defaults';
import { AnnotationType } from './types';
import { PluginAnnotationDict_1_4_0, isPluginsAnnotationsSettings_1_4_0, PluginsAnnotationsSettings_1_4_0, PluginsAnnotationsSettings_1_3_0, isPluginAnnotationDictFormat_1_3_0, isSettingsFormat_1_3_0} from 'types_legacy'
import { DEFAULT_SETTINGS_1_3_0, DEFAULT_SETTINGS_1_4_0 } from './defaults_legacy';
import { PluginsAnnotationsSettingTab } from 'settings_tab'
import * as path from 'path';
import { readAnnotationsFromFile, writeAnnotationsToFile } from 'manageAnnotations';
export default class PluginsAnnotations extends Plugin {
settings: PluginsAnnotationsSettings = {...DEFAULT_SETTINGS};
settings: PluginsAnnotationsSettings_1_4_0 = {...DEFAULT_SETTINGS_1_4_0};
pluginNameToIdMap: Record<string,string> = {};
pluginIdToNameMap: Record<string,string> = {};
@ -62,42 +63,54 @@ export default class PluginsAnnotations extends Plugin {
this.pluginNameToIdMap = this.constructPluginNameToIdMap();
this.pluginIdToNameMap = this.generateInvertedMap(this.pluginNameToIdMap);
// Set to true when the settings are updated to the new format
let wasUpdated = false;
// Nested function to handle different versions of settings
const getSettingsFromData = (data: unknown): unknown => {
if (isPluginsAnnotationsSettings(data)) {
const settings: PluginsAnnotationsSettings = data;
// type: AnnotationType.markdown,
if (isPluginsAnnotationsSettings_1_4_0(data)) {
const settings: PluginsAnnotationsSettings_1_4_0 = data;
return settings;
} else if (isPluginsAnnotationsSettingsWithoutNames(data)) { // previous versions where the name of the plugins was not stored
} else if (isSettingsFormat_1_3_0(data)) { // previous versions where the name of the plugins was not stored
// Upgrade annotations format
const upgradedAnnotations: PluginAnnotationDict = {};
const upgradedAnnotations: PluginAnnotationDict_1_4_0 = {};
for (const pluginId in data.annotations) {
const annotation = data.annotations[pluginId];
upgradedAnnotations[pluginId] = {
name: this.pluginIdToNameMap[pluginId] || pluginId,
anno: annotation,
type: AnnotationType.markdown,
};
}
const oldSettings: PluginsAnnotationsSettingsWithoutNames = data;
const oldSettings: PluginsAnnotationsSettings_1_3_0 = data;
// Update the data with the new format
const newSettings: PluginsAnnotationsSettings = {
const newSettings: PluginsAnnotationsSettings_1_4_0 = {
...oldSettings,
annotations: upgradedAnnotations,
plugins_annotations_uuid: DEFAULT_SETTINGS.plugins_annotations_uuid,
plugins_annotations_uuid: DEFAULT_SETTINGS_1_4_0.plugins_annotations_uuid,
};
wasUpdated = true;
return getSettingsFromData(newSettings);
} else {
// Very first version of the plugin -- no options were stored, only the dictionary of annotations
const newSettings: PluginsAnnotationsSettingsWithoutNames = { ...DEFAULT_SETTINGS_WITHOUT_NAMES };
newSettings.annotations = isPluginAnnotationDictWithoutNames(data) ? data : DEFAULT_SETTINGS_WITHOUT_NAMES.annotations;
// Very first version of the plugin 1.0 -- no options were stored, only the dictionary of annotations
const newSettings: PluginsAnnotationsSettings_1_3_0 = { ...DEFAULT_SETTINGS_1_3_0 };
newSettings.annotations = isPluginAnnotationDictFormat_1_3_0(data) ? data : DEFAULT_SETTINGS_1_3_0.annotations;
wasUpdated = true;
return getSettingsFromData(newSettings);
}
};
// Merge loaded settings with default settings
this.settings = Object.assign({}, DEFAULT_SETTINGS, getSettingsFromData(await this.loadData()));
this.settings = Object.assign({}, DEFAULT_SETTINGS_1_4_0, getSettingsFromData(await this.loadData()));
if(wasUpdated) {
console.log("SAVED");
this.debouncedSaveAnnotations();
}
if(this.settings.markdown_file_path!=='') {
readAnnotationsFromFile(this);
@ -121,7 +134,7 @@ export default class PluginsAnnotations extends Plugin {
} else return "";
}
async saveSettings(settings:PluginsAnnotationsSettings) {
async saveSettings(settings:PluginsAnnotationsSettings_1_4_0) {
try {
await this.saveData(settings);
} catch (error) {
@ -459,7 +472,7 @@ export default class PluginsAnnotations extends Plugin {
this.settings.annotations[pluginId] = {
anno: annotation_text,
name: pluginName,
type: AnnotationType.markdown,
// type: AnnotationType.markdown,
};
annotation_div.classList.remove('plugin-comment-placeholder');
isPlaceholder = false;
@ -631,9 +644,9 @@ export default class PluginsAnnotations extends Plugin {
this.disconnectObservers();
}
getUninstalledPlugins(): PluginAnnotationDict {
getUninstalledPlugins(): PluginAnnotationDict_1_4_0 {
const installedPluginIds = new Set(Object.keys(this.app.plugins.manifests));
const uninstalledPlugins: PluginAnnotationDict = {};
const uninstalledPlugins: PluginAnnotationDict_1_4_0 = {};
for (const pluginId in this.settings.annotations) {
if (!installedPluginIds.has(pluginId)) {

View file

@ -4,7 +4,7 @@ import PluginsAnnotations from "main";
import { Platform, TFile } from "obsidian";
import { createFolderIfNotExists, joinPaths, makePosixPathOScompatible, parseFilePath, showConfirmationDialog } from "utils";
import { parse, SyntaxError } from "./peggy.mjs";
import { PluginAnnotationDict } from "types";
import { PluginAnnotationDict_1_4_0 } from "types_legacy";
export async function handleMarkdownFilePathChange(plugin: PluginsAnnotations, filepath: string): Promise<boolean> {
const file = plugin.app.vault.getAbstractFileByPath(filepath);
@ -74,7 +74,7 @@ export async function readAnnotationsFromFile(plugin: PluginsAnnotations): Promi
const md_content = await plugin.app.vault.read(file);
try {
const md_content_parsed = parse(md_content) as PluginAnnotationDict;
const md_content_parsed = parse(md_content) as PluginAnnotationDict_1_4_0;
plugin.settings.annotations = md_content_parsed;
} catch(error) {
if (error instanceof SyntaxError) {
@ -103,6 +103,8 @@ export async function writeAnnotationsToFile(plugin: PluginsAnnotations) {
content.push(`# ${annotations[pluginId].name}\n\n<!-- id: ${pluginId} -->\n<!-- BEGIN ANNOTATION -->\n${annotations[pluginId].anno}\n<!-- END ANNOTATION -->\n`);
}
console.log(content.join('\n'));
try {
let file = plugin.app.vault.getFileByPath(filePath);
if (!file) {

View file

@ -3,7 +3,7 @@
import PluginsAnnotations from "main";
import { handleMarkdownFilePathChange } from "manageAnnotations";
import { AbstractInputSuggest, App, Platform, PluginSettingTab, prepareFuzzySearch, SearchResult, Setting, TFile } from "obsidian";
import { PluginAnnotationDict } from "types";
import { PluginAnnotationDict_1_4_0 } from "types_legacy";
import { parseFilePath } from "utils";
class FileSuggestion extends AbstractInputSuggest<TFile> {
@ -56,7 +56,7 @@ export class PluginsAnnotationsSettingTab extends PluginSettingTab {
}
createUninstalledPluginSettings(containerEl: HTMLElement) {
const uninstalledPlugins:PluginAnnotationDict = this.plugin.getUninstalledPlugins();
const uninstalledPlugins:PluginAnnotationDict_1_4_0 = this.plugin.getUninstalledPlugins();
const heading = new Setting(containerEl).setName('Personal annotations of no longer installed community plugins').setHeading();
const headingEl = heading.settingEl;

View file

@ -1,38 +1,8 @@
// types.ts
import { DEFAULT_SETTINGS, DEFAULT_SETTINGS_WITHOUT_NAMES } from './defaults';
import { DEFAULT_SETTINGS_1_4_0 } from './defaults_legacy';
export interface PluginAnnotation {
name: string; // extended name of the plugin
anno: string; // personal annontation
type: AnnotationType; // type of annotation
}
export interface PluginAnnotationDict {
[pluginId: string]: PluginAnnotation;
}
export interface PluginsAnnotationsSettings {
annotations: PluginAnnotationDict;
plugins_annotations_uuid: string;
hide_placeholders: boolean;
delete_placeholder_string_on_insertion: boolean;
label_mobile: string;
label_desktop: string;
label_placeholder: string;
editable: boolean;
automatic_remove: boolean;
markdown_file_path: string;
}
export function isPluginsAnnotationsSettings(s:unknown): s is PluginsAnnotationsSettings {
if (typeof s !== 'object' || s === null) {
return false;
}
return 'annotations' in s
&& 'plugins_annotations_uuid' in s
&& (s as PluginsAnnotationsSettings).plugins_annotations_uuid === DEFAULT_SETTINGS.plugins_annotations_uuid;
}
// type: AnnotationType; // type of annotation
export enum AnnotationType {
text = 'text',
@ -40,32 +10,6 @@ export enum AnnotationType {
markdown = 'markdown',
}
// For backward compatibility only with version 'FAA70013-38E9-4FDF-B06A-F899F6487C19'
export function isPluginAnnotationDictWithoutNames(d: unknown): d is PluginAnnotationDictWithoutNames {
if (typeof d !== 'object' || d === null) {
return false;
}
return Object.values(d).every(value => typeof value === 'string');
}
export interface PluginAnnotationDictWithoutNames {
[pluginId: string]: string;
}
// Extend the original interface and override the annotations property
export interface PluginsAnnotationsSettingsWithoutNames extends Omit<PluginsAnnotationsSettings, 'annotations'> {
annotations: PluginAnnotationDictWithoutNames;
}
export function isPluginsAnnotationsSettingsWithoutNames(s:unknown): s is PluginsAnnotationsSettingsWithoutNames {
if (typeof s !== 'object' || s === null) {
return false;
}
return 'annotations' in s
&& 'plugins_annotations_uuid' in s
&& (s as PluginsAnnotationsSettingsWithoutNames).plugins_annotations_uuid === DEFAULT_SETTINGS_WITHOUT_NAMES.plugins_annotations_uuid;
}
export interface ParsedPath {
dir: string,
base: string,
@ -73,6 +17,3 @@ export interface ParsedPath {
ext: string,
path: string
}
// For backward compatibility only with very first version (no UUID assigned)
export type PluginsAnnotationsSettingsWithoutOptions = string[];

72
src/types_legacy.ts Normal file
View file

@ -0,0 +1,72 @@
// types_legacy.ts
import { DEFAULT_SETTINGS_1_3_0, DEFAULT_SETTINGS_1_4_0 } from "defaults_legacy";
/* VERSION 1.4 */
export interface PluginAnnotation_1_4_0 {
name: string; // extended name of the plugin
anno: string; // personal annontation
}
export interface PluginAnnotationDict_1_4_0 {
[pluginId: string]: PluginAnnotation_1_4_0;
}
export interface PluginsAnnotationsSettings_1_4_0 {
annotations: PluginAnnotationDict_1_4_0;
plugins_annotations_uuid: string;
hide_placeholders: boolean;
delete_placeholder_string_on_insertion: boolean;
label_mobile: string;
label_desktop: string;
label_placeholder: string;
editable: boolean;
automatic_remove: boolean;
markdown_file_path: string;
}
export function isPluginsAnnotationsSettings_1_4_0(s:unknown): s is PluginsAnnotationsSettings_1_4_0 {
if (typeof s !== 'object' || s === null) {
return false;
}
return 'annotations' in s
&& 'plugins_annotations_uuid' in s
&& (s as PluginsAnnotationsSettings_1_4_0).plugins_annotations_uuid === DEFAULT_SETTINGS_1_4_0.plugins_annotations_uuid;
}
/* VERSION 1.3 */
// For backward compatibility only with version 1.3.0 'FAA70013-38E9-4FDF-B06A-F899F6487C19'
export function isPluginAnnotationDictFormat_1_3_0(d: unknown): d is PluginAnnotationDict_1_3_0 {
if (typeof d !== 'object' || d === null) {
return false;
}
return Object.values(d).every(value => typeof value === 'string');
}
export interface PluginAnnotationDict_1_3_0 {
[pluginId: string]: string;
}
// Extend the original interface and override the annotations property
export interface PluginsAnnotationsSettings_1_3_0 extends Omit<PluginsAnnotationsSettings_1_4_0, 'annotations'> {
annotations: PluginAnnotationDict_1_3_0;
}
export function isSettingsFormat_1_3_0(s:unknown): s is PluginsAnnotationsSettings_1_3_0 {
if (typeof s !== 'object' || s === null) {
return false;
}
return 'annotations' in s
&& 'plugins_annotations_uuid' in s
&& (s as PluginsAnnotationsSettings_1_3_0).plugins_annotations_uuid === DEFAULT_SETTINGS_1_3_0.plugins_annotations_uuid;
}
/* VERSION 1.0 */
// For backward compatibility only with very first version 1.0 (no UUID assigned)
// In this version, no options were provided, just the list of annotations.
export type PluginsAnnotationsSettings_1_0_0 = string[];