diff --git a/src/GalleryInfo.ts b/src/GalleryInfo.ts index 037e04f..5117022 100644 --- a/src/GalleryInfo.ts +++ b/src/GalleryInfo.ts @@ -32,6 +32,7 @@ export class GalleryInfo if(!this.infoList.contains("name")) { + current = block.createDiv({ cls: 'gallery-info-section' }); current.createSpan({ cls: 'gallery-info-section-label' }).textContent = "Name"; current.createDiv({ cls: 'gallery-info-section-value' }).textContent = this.imgFile.basename; @@ -41,7 +42,9 @@ export class GalleryInfo { current = block.createDiv({ cls: 'gallery-info-section' }); current.createSpan({ cls: 'gallery-info-section-label' }).textContent = "Path"; - current.createDiv({ cls: 'gallery-info-section-value' }).textContent = this.imgFile.path; + const imgLink = current.createDiv({ cls: 'gallery-info-section-value' }).createEl("a", { cls: 'internal-link' }); + imgLink.href = this.imgFile.path; + imgLink.textContent = this.imgFile.path; } if(!this.infoList.contains("extension")) diff --git a/src/main.ts b/src/main.ts index d9160ce..577168c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -import { Plugin, type WorkspaceLeaf, addIcon } from 'obsidian' +import { Plugin, type WorkspaceLeaf, addIcon, Menu, Editor, MarkdownView, type MarkdownFileInfo, MenuItem, Notice, TFile, Vault } from 'obsidian' import { type GallerySettings, SETTINGS, OB_GALLERY, OB_GALLERY_INFO, galleryIcon, gallerySearchIcon } from './utils' import { GallerySettingTab } from './settings' import { GalleryProcessor } from './block' @@ -8,12 +8,14 @@ export default class GalleryTagsPlugin extends Plugin { settings!: GallerySettings; containerEl!: HTMLElement; + currentMetaTemplate: string; async onload() { // Load message - await this.loadSettings() - console.log('Loaded Gallery Tags Plugin') + await this.loadSettings(); + await this.loadMetaTemplate(); + console.log('Loaded Gallery Tags Plugin'); // Register gallery display block renderer this.registerMarkdownCodeBlockProcessor('gallery', async (source, el, ctx) => @@ -48,6 +50,25 @@ export default class GalleryTagsPlugin extends Plugin // Save settings this.saveSettings() + + // this.registerEvent( + // app.workspace.on( + // "editor-menu", + // this.testOption + // ) + // ); + } + + testOption (menu: Menu, editor: Editor, info: MarkdownView | MarkdownFileInfo) + { + menu.addItem((item: MenuItem) => { + item.setTitle("Test Option") + //.setIcon("plus-circle") + //.setSection("cmdr") + .onClick(async () => { + new Notice("clicked option"); + }); + }); } onunload() @@ -55,6 +76,15 @@ export default class GalleryTagsPlugin extends Plugin this.app.workspace.detachLeavesOfType(OB_GALLERY_INFO) console.log('unloading Gallery Plugin') } + + async loadMetaTemplate() + { + const imgTFile = this.app.vault.getAbstractFileByPath(this.settings.imgmetaTemplatePath+".md") as TFile; + if(imgTFile) + { + this.currentMetaTemplate = await this.app.vault.read(imgTFile); + } + } async loadSettings() { diff --git a/src/settings.ts b/src/settings.ts index e5eb749..c9d0884 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -16,6 +16,7 @@ export class GallerySettingTab extends PluginSettingTab const { containerEl } = this let resourcesPathInput = '' let onOpenPathInput = '' + let imgmetaPathInput = '' let hiddenInfoInput = '' containerEl.empty() @@ -88,6 +89,33 @@ export class GallerySettingTab extends PluginSettingTab { onOpenPathInput = value.trim() })) + + const metaTemplatSetting = new Setting(containerEl) + .setName('Meta file template override') + .setDesc('') + .addButton(text => text + .setButtonText('Save') + .onClick(async () => + { + this.plugin.settings.imgmetaTemplatePath = imgmetaPathInput + imgmetaPathInput = '' + this.plugin.saveSettings() + await this.plugin.loadMetaTemplate(); + })) + .addText(text => text + .setPlaceholder(this.plugin.settings.imgmetaTemplatePath) + .onChange(async (value) => + { + imgmetaPathInput = value.trim() + })) + metaTemplatSetting.descEl.createDiv({ text: 'Location of template file to use for generating image meta files. If blank will use default.' }) + metaTemplatSetting.descEl.createDiv({ text: 'These keys will be replaced with the apropriate info for the file:' }) + metaTemplatSetting.descEl.createDiv({ text: '<% IMG LINK %> : Clickable link to the image with its name as the text' }) + metaTemplatSetting.descEl.createDiv({ text: '<% IMG EMBED %> : Embeded view of the image' }) + metaTemplatSetting.descEl.createDiv({ text: '<% IMG INFO %> : Info block for the image' }) + metaTemplatSetting.descEl.createDiv({ text: '<% IMG URI %> : The formatted URI for the image that can be used to generate a link to it' }) + metaTemplatSetting.descEl.createDiv({ text: '<% IMG PATH %> : Path to the image(including file name)' }) + metaTemplatSetting.descEl.createDiv({ text: '<% IMG NAME %> : File name for the image' }) new Setting(containerEl) .setName('Default Hidden Info') diff --git a/src/utils.ts b/src/utils.ts index b648733..8d1332f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -6,6 +6,7 @@ export interface GallerySettings { imgDataFolder: string | null galleryLoadPath: string + imgmetaTemplatePath: string | null width: number hiddenInfo: string | null } @@ -41,6 +42,7 @@ export interface InfoBlockArgs export const SETTINGS: GallerySettings = { imgDataFolder: null, galleryLoadPath: '/', + imgmetaTemplatePath: null, width: 400, hiddenInfo: "tags;palette" } @@ -124,26 +126,33 @@ export const GALLERY_RESOURCES_MISSING = ` Please make sure that a Valid Folder is specified in the settings for the plugin to use to store image information notes! ` +const defaultTemplate = '---\ntags:\n---\n<%IMGEMBED%>\n<%IMGINFO%>\n%% Description %%\n' + /** * Return initial img info file content * @param imgPath - Relative vault path of related image */ -const initializeInfo = (imgPath: string, imgName: string): string => +const initializeInfo = (template: string, imgPath: string, imgName: string): string => { - return `--- -tags: ---- - -[${imgName}](${imgPath.replaceAll(' ', '%20')}) -![](${imgPath.replaceAll(' ', '%20')}) -%% Description %% - -%% Description %% -\`\`\`gallery-info -imgPath=${imgPath} -\`\`\` -` -}; + if(template == null || template.trim() == "") + { + template = defaultTemplate; + } + const uri = imgPath.replaceAll(' ', '%20'); + const infoBlock = "```gallery-info\nimgPath="+imgPath+"\n```"; + const link = "["+imgName+"]("+uri+")"; + const embed = "![]("+uri+")"; + let final = template; + + final = final.replaceAll(new RegExp(/<%\s*(I|i)(M|m)(G|g)\s*(L|l)(I|i)(N|n)(K|k)\s*%>/g), link); + final = final.replaceAll(new RegExp(/<%\s*(I|i)(M|m)(G|g)\s*(E|e)(M|m)(B|b)(E|e)(D|d)\s*%>/g), embed); + final = final.replaceAll(new RegExp(/<%\s*(I|i)(M|m)(G|g)\s*(I|i)(N|n)(F|f)(O|o)\s*%>/g), infoBlock); + final = final.replaceAll(new RegExp(/<%\s*(I|i)(M|m)(G|g)\s*(U|u)(R|r)(I|i)\s*%>/g), uri); + final = final.replaceAll(new RegExp(/<%\s*(I|i)(M|m)(G|g)\s*(P|p)(A|a)(T|t)(H|h)\s*%>/g), imgPath); + final = final.replaceAll(new RegExp(/<%\s*(I|i)(M|m)(G|g)\s*(N|n)(A|a)(M|m)(E|e)\s*%>/g), imgName); + + return final; +} /** * Return Image Info File, if not present create it @@ -193,7 +202,7 @@ export const getImgInfo = async (imgPath: string, vault: Vault, metadata: Metada counter++; } - await vault.adapter.write(`${plugin.settings.imgDataFolder}/${fileName}.md`, initializeInfo(imgPath, imgName)) + await vault.adapter.write(`${plugin.settings.imgDataFolder}/${fileName}.md`, initializeInfo(plugin.currentMetaTemplate, imgPath, imgName)) infoFile = (vault.getAbstractFileByPath(`${plugin.settings.imgDataFolder}/${fileName}.md`) as TFile) } return infoFile