Skip to content

Commit

Permalink
Image info block now tries to auto resolve file moves
Browse files Browse the repository at this point in the history
Updated readme
  • Loading branch information
TomNCatz committed Sep 8, 2023
1 parent dd3da2e commit 749775c
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 25 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,30 @@ Argument Info:

## Settings:

![](https://raw.githubusercontent.com/TomNCatz/obsidian-gallery/main/images/Gallery_Settings.png)
![](https://raw.githubusercontent.com/TomNCatz/obsidian-gallery/main/images/Gallery_Settings.png)


# Release Notes
## v0.7.0
- Total image count now only count file the gallery thinks it can display in the first place
- More large gallery optimization
- Added a setting for default hidden info
- Added the option to set your own template file for the meta files

## v0.6.2
- Tag filter support added to gallery block
- Tag filter support is more stable and consistent now
- added support for .gif and .webm support
- indicator in filter of how many files are being shown verses the total(this currently show all file types for the total, not just supported image file types, will fix later)
- some work done to improve handling of exceptionally large image collections(over 14000)
- moved the reverse sorting checkbox into the filter header so it can be changed on the fly
- cleaned up some packages that might have had vulnerabilities
- fixed issue with supporting multiple spaces in a file name

## V0.6.1
- when desktop version parses color information for an image is saves it and mobile loads it from that save (work around for not being able to get palette information on mobile)
- tag filter field added
- can filter by multiple tags if separated by spaces
- putting a '-' at the front of a tag such as '-photos' will exclude all results with that tag
- tag filter mode checkbox added, when checked only shows results that match ALL tags, when off includes result that match ANY tags
- Fixed MP$ display on desktop(still broken on mobile)
64 changes: 56 additions & 8 deletions src/block.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Vault, MetadataCache, FrontMatterCache } from 'obsidian'
import { MarkdownRenderer, TFile, getAllTags, Platform } from 'obsidian'
import type { Vault, MetadataCache, FrontMatterCache, EditorPosition } from 'obsidian'
import { MarkdownRenderer, TFile, getAllTags, Platform, MarkdownView } from 'obsidian'
import { extractColors } from '../node_modules/extract-colors'
import type { GalleryBlockArgs, InfoBlockArgs } from './utils'
import
{
EXTENSIONS, GALLERY_DISPLAY_USAGE, EXTRACT_COLORS_OPTIONS, OB_GALLERY_INFO,
VIDEO_REGEX,
getImgInfo, updateFocus
getImgInfo, updateFocus, GALLERY_INFO_USAGE, searchForFile
} from './utils'
import { GalleryInfoView } from './view'
import type GalleryTagsPlugin from './main'
Expand Down Expand Up @@ -191,7 +191,7 @@ export class GalleryProcessor
}
}

async galleryImageInfo(source: string, el: HTMLElement, vault: Vault, metadata: MetadataCache, plugin: GalleryTagsPlugin)
async galleryImageInfo(source: string, el: HTMLElement, sourcePath: string, vault: Vault, metadata: MetadataCache, plugin: GalleryTagsPlugin)
{
const args: InfoBlockArgs = {
imgPath: '',
Expand Down Expand Up @@ -219,16 +219,64 @@ export class GalleryProcessor
attr: { style: 'width: 100%; height: auto; float: left' }
})

const imgTFile = vault.getAbstractFileByPath(args.imgPath)
const imgURL = vault.adapter.getResourcePath(args.imgPath)
let imgTFile = vault.getAbstractFileByPath(args.imgPath)
let imgURL = vault.adapter.getResourcePath(args.imgPath)

// Handle problematic arg
if (!args.imgPath || !imgTFile)
if(!args.imgPath)
{
MarkdownRenderer.renderMarkdown('GALLERY_INFO_USAGE', elCanvas, '/', plugin)
MarkdownRenderer.render(plugin.app, GALLERY_INFO_USAGE, elCanvas, '/', plugin)
return;
}

if (!imgTFile)
{
const found = await searchForFile(args.imgPath, plugin);

if(found.length == 0)
{
MarkdownRenderer.render(plugin.app,GALLERY_INFO_USAGE, elCanvas, '/', plugin)
return;
}

if(found.length == 1)
{
// set file and path for current usage
imgTFile = vault.getAbstractFileByPath(found[0])
imgURL = vault.adapter.getResourcePath(found[0])

// replace file path for future usage
const view = plugin.app.workspace.getActiveViewOfType(MarkdownView);

if (view)
{
for(let i = 0; i < view.editor.lineCount(); i++)
{
let line = view.editor.getLine(i);
if(line.contains(args.imgPath))
{
const from: EditorPosition = {line: i, ch: line.indexOf(args.imgPath)};
const to: EditorPosition = {line: i, ch: line.indexOf(args.imgPath)+args.imgPath.length};
view.editor.replaceRange(found[0], from, to);
}
}
}

}
else
{
// too many options, tell the user about it
let output = "### File path not found. Were you looking for one of these?\n"
for(let i = 0; i < found.length; i++)
{
output += "- "+found[i]+"\n";
}

MarkdownRenderer.render(plugin.app,output, elCanvas, '/', plugin)
return;
}
}

let measureEl, isVideo
let hexList: string[] = [];
// Get image dimensions
Expand Down
13 changes: 1 addition & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ export default class GalleryTagsPlugin extends Plugin
{
settings!: GallerySettings;
containerEl!: HTMLElement;
currentMetaTemplate: string;

async onload()
{
// Load message
await this.loadSettings();
await this.loadMetaTemplate();
console.log('Loaded Gallery Tags Plugin');

// Register gallery display block renderer
Expand All @@ -28,7 +26,7 @@ export default class GalleryTagsPlugin extends Plugin
this.registerMarkdownCodeBlockProcessor('gallery-info', async (source, el, ctx) =>
{
const proc = new GalleryProcessor()
await proc.galleryImageInfo(source, el, this.app.vault, this.app.metadataCache, this)
await proc.galleryImageInfo(source, el, ctx.sourcePath, this.app.vault, this.app.metadataCache, this)
});

// Add Gallery Icon
Expand Down Expand Up @@ -76,15 +74,6 @@ 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()
{
Expand Down
1 change: 0 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ export class GallerySettingTab extends PluginSettingTab
this.plugin.settings.imgmetaTemplatePath = imgmetaPathInput
imgmetaPathInput = ''
this.plugin.saveSettings()
await this.plugin.loadMetaTemplate();
}))
.addText(text => text
.setPlaceholder(this.plugin.settings.imgmetaTemplatePath)
Expand Down
32 changes: 29 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ e.g. Input:
\`\`\`
imgPath=Resources/Images/Image_example_1.png
infoList=Name;tags;size;backlinks
ignoreInfo=Name;tags;size;backlinks
\`\`\`
----
Expand All @@ -111,11 +111,12 @@ infoList=Name;tags;size;backlinks
- Path is the relative path of the file withing the obsidian vault.
- Make sure the image exists!!
- It is case sensitive!
- IgnoreInfo is a list of fields NOT to display(if not included uses Default Hidden Info setting)
----
Please Check Release Notes for plugin changes:<br>
https://github.com/Darakah/obsidian-gallery#release-notes
https://github.com/TomNCatz/obsidian-gallery#release-notes
`

export const GALLERY_RESOURCES_MISSING = `
Expand Down Expand Up @@ -202,7 +203,15 @@ export const getImgInfo = async (imgPath: string, vault: Vault, metadata: Metada
counter++;
}

await vault.adapter.write(`${plugin.settings.imgDataFolder}/${fileName}.md`, initializeInfo(plugin.currentMetaTemplate, imgPath, imgName))

const templateTFile = plugin.app.vault.getAbstractFileByPath(plugin.settings.imgmetaTemplatePath+".md") as TFile;
let template = defaultTemplate;
if(templateTFile)
{
template = await plugin.app.vault.read(templateTFile);
}

await vault.adapter.write(`${plugin.settings.imgDataFolder}/${fileName}.md`, initializeInfo(template, imgPath, imgName))
infoFile = (vault.getAbstractFileByPath(`${plugin.settings.imgDataFolder}/${fileName}.md`) as TFile)
}
return infoFile
Expand All @@ -212,6 +221,23 @@ export const getImgInfo = async (imgPath: string, vault: Vault, metadata: Metada
return null
};

export const searchForFile = async (path: string, plugin: GalleryTagsPlugin): Promise<string[]> =>
{
const foundPaths: string[] = []
const vaultFiles: TFile[] = plugin.app.vault.getFiles();
const fileName: string = path.substring(path.lastIndexOf('/'));

for (const file of vaultFiles)
{
if (EXTENSIONS.contains(file.extension.toLowerCase()) && file.path.contains(fileName) )
{
foundPaths.push(file.path);
}
}

return foundPaths;
}

/**
* Return images in the specified directory
* @param path - path to project e.g. 'Test Project/First Sub Project'
Expand Down

0 comments on commit 749775c

Please sign in to comment.