fix: use file path instead of filename for finding templates

This commit is contained in:
Kelvin John Falk Szolnoky 2023-12-28 11:54:29 +01:00
parent 841fbec1a3
commit 06946c73da
No known key found for this signature in database
GPG key ID: 02BA666D8C0E7E1B
2 changed files with 17 additions and 11 deletions

View file

@ -17,11 +17,11 @@ export class FileSuggest extends TextInputSuggest<TFile> {
}
renderSuggestion(file: TFile, el: HTMLElement): void {
el.setText(file.name);
el.setText(file.path);
}
selectSuggestion(file: TFile): void {
this.inputEl.value = file.name;
this.inputEl.value = file.path;
this.inputEl.trigger('input');
this.close();
}

View file

@ -69,22 +69,28 @@ export class MediaTypeManager {
}
async getTemplate(mediaTypeModel: MediaTypeModel, app: App): Promise<string> {
const templateFileName = this.mediaTemplateMap.get(mediaTypeModel.getMediaType());
const templateFilePath = this.mediaTemplateMap.get(mediaTypeModel.getMediaType());
if (!templateFileName) {
if (!templateFilePath) {
return '';
}
const templateFile: TFile = app.vault
.getFiles()
.filter((f: TFile) => f.name === templateFileName)
.first();
let templateFile = app.vault.getAbstractFileByPath(templateFilePath);
if (!templateFile) {
return '';
// WARNING: This was previously selected by filename, but that could lead to collisions and unwanted effects.
// This now falls back to the previous method if no file is found
if (!templateFile || templateFile instanceof TFolder) {
templateFile = app.vault
.getFiles()
.filter((f: TFile) => f.name === templateFilePath)
.first();
if (!templateFile) {
return '';
}
}
const template = await app.vault.cachedRead(templateFile);
const template = await app.vault.cachedRead(templateFile as TFile);
// console.log(template);
return replaceTags(template, mediaTypeModel);
}