Skip to content

Commit

Permalink
I kept changing the template for the meta files and I figured other p…
Browse files Browse the repository at this point in the history
…eople can make their own if they don't like mine
  • Loading branch information
TomNCatz committed Sep 7, 2023
1 parent 98a5d7b commit 60d2074
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/GalleryInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"))
Expand Down
36 changes: 33 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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) =>
Expand Down Expand Up @@ -48,13 +50,41 @@ 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()
{
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()
{
Expand Down
28 changes: 28 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class GallerySettingTab extends PluginSettingTab
const { containerEl } = this
let resourcesPathInput = ''
let onOpenPathInput = ''
let imgmetaPathInput = ''
let hiddenInfoInput = ''

containerEl.empty()
Expand Down Expand Up @@ -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')
Expand Down
41 changes: 25 additions & 16 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface GallerySettings
{
imgDataFolder: string | null
galleryLoadPath: string
imgmetaTemplatePath: string | null
width: number
hiddenInfo: string | null
}
Expand Down Expand Up @@ -41,6 +42,7 @@ export interface InfoBlockArgs
export const SETTINGS: GallerySettings = {
imgDataFolder: null,
galleryLoadPath: '/',
imgmetaTemplatePath: null,
width: 400,
hiddenInfo: "tags;palette"
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 60d2074

Please sign in to comment.