Skip to content

Commit

Permalink
fix critical bug.
Browse files Browse the repository at this point in the history
- Fixed a bug that caused note creation to fail if the tag {{Ratings}} was used.
- Ratings now correctly display

closes #15
  • Loading branch information
Superschnizel committed Nov 29, 2023
1 parent 6150770 commit 9345509
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
21 changes: 18 additions & 3 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { App, Editor, MarkdownView, Modal, Menu, Notice, Plugin, PluginSettingTa

import {MoviegrabberSettings, DEFAULT_SETTINGS, DEFAULT_TEMPLATE} from "./src/MoviegrabberSettings"
import {MoviegrabberSearchModal} from "./src/MoviegrabberSearchModal"
import {MOVIE_DATA_LOWER, MovieData, MovieSearch, MovieSearchItem, TEST_SEARCH} from "./src/MoviegrabberSearchObject"
import {MOVIE_DATA_LOWER, MovieData, MovieRating, MovieSearch, MovieSearchItem, Rating, TEST_SEARCH} from "./src/MoviegrabberSearchObject"
import { MoviegrabberSelectionModal } from 'src/MoviegrabberSelectionModal';
import { MovieGalleryView, VIEW_TYPE_MOVIE_GALLERY } from 'src/MovieGalleryView';
import { ConfirmOverwriteModal } from 'src/ConfirmOverwriteModal';
Expand Down Expand Up @@ -323,20 +323,35 @@ export default class Moviegrabber extends Plugin {

async FillTemplate(template : string, data : MovieData) : Promise<string> {
return template.replace(/{{(.*?)}}/g, (match) => {
// console.log(match);
let inner = match.split(/{{|}}/).filter(Boolean)[0];
if (!inner) {
return match;
}
let split = inner.split(/(?<!\\)\|/); // split at not escaped "|"
const prefix = split.length >= 2 ? split[1].replace(/\\\|/, '|') : '';
const suffix = split.length >= 3 ? split[2].replace(/\\\|/, '|') : '';

let result = '';
// handle the data being a list.
let name = MOVIE_DATA_LOWER[split[0].trim().toLowerCase()];
let items = data[name]?.split(/\,\s?/);
// console.log(`${name}, --> ${data[name]}`);

let rawData = data[name];

let items = rawData instanceof Array
? rawData.map( (elem) : string => {
let r = elem as MovieRating;
return `${r.Source}: ${r.Value}`;
})
: rawData?.split(/\,\s?/);

if (!items) {
console.log(`Tag "{{${inner}}}" could not be resolved.`);
this.SendWarningNotice(`Tag "{{${inner}}}" could not be resolved.`);
new Notice(`Warning: Tag "{{${inner}}}" could not be resolved.`)
return `{{${inner}}}`;
}

result += prefix;
result += items[0]; // data
result += suffix;
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "moviegrabber",
"name": "Moviegrabber",
"version": "1.1.9",
"version": "1.1.10",
"minAppVersion": "0.15.0",
"description": "Grab movie data from public APIs and transform it into notes that can be used with dataview and properties",
"author": "Superschnizel",
Expand Down
15 changes: 14 additions & 1 deletion src/MoviegrabberSearchObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface MovieData{
Country: string,
Awards: string,
Poster: string,
Ratings: Array<any>,
Ratings: Array<MovieRating>,
Metascore: number,
imdbRating: number,
imdbVotes: number,
Expand All @@ -42,6 +42,19 @@ export interface MovieData{
YoutubeEmbed: string
}

export interface MovieRating{
Source : string;
Value: string;
}

export class Rating implements MovieRating {
Source: string;
Value: string;
public toString = () : string => {
return `${this.Source}: ${this.Value}`;
}
}

interface MovieDataLowercase {
[key:string]: string
}
Expand Down

0 comments on commit 9345509

Please sign in to comment.