Skip to content

Commit 22370f1

Browse files
committed
feat: safety in access
Made accessing the card size information safer. An additional change here is the adding of a "cardsIndex" property to the BaseSchema, allowing us to track the first view that is actually a cards view, rather than a table one or something else. This wasn't planned before, but I realised that it should probably exist. Safety first!
1 parent 9630277 commit 22370f1

3 files changed

Lines changed: 37 additions & 12 deletions

File tree

src/listeners/bases.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,30 @@ export async function getBases(plugin: ThumbnailPlugin): Promise<Array<Base>> {
77
const files = plugin.app.vault.getFiles().filter((v) => v.extension === "base");
88
const bases: Array<Base> = [];
99
for (const file of files) {
10-
const base = plugin.bases.find((f) => f.path == file.path);
11-
if (base) {
12-
plugin.bases.remove(base);
10+
const content = await plugin.app.vault.read(file);
11+
const parsed = (parseYaml(content) as BaseSchema);
12+
let hasCardView: boolean = false;
13+
let viewIndex = 0;
14+
for (let i = 0; i < parsed.views.length; i++) {
15+
let view = parsed.views[i];
16+
if (view && view.type == "cards") {
17+
hasCardView = true;
18+
viewIndex = i;
19+
break;
20+
}
21+
}
22+
if (hasCardView) {
23+
const base = plugin.bases.find((f) => f.path == file.path);
24+
if (base) {
25+
plugin.bases.remove(base);
26+
}
27+
bases.push({
28+
path: file.path,
29+
file: file,
30+
content: content,
31+
cardsIndex: viewIndex,
32+
});
1333
}
14-
bases.push({
15-
path: file.path,
16-
file: file,
17-
content: await plugin.app.vault.read(file),
18-
});
1934
}
2035
plugin.bases = bases;
2136
console.debug("Thumnbnails - bases found:", bases)

src/main.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Command, parseYaml, Plugin, TFile } from 'obsidian';
2-
import { Base, PluginSettings } from './types';
2+
import { Base, BaseSchema, PluginSettings } from './types';
33
import { generate } from "./generator";
44
import { SettingTab } from './settings';
55
import { getBases, getFiles } from './listeners/bases';
@@ -26,9 +26,18 @@ export default class ThumbnailPlugin extends Plugin {
2626
for (const base of bases) {
2727
// TODO: automatic rebuilding of thumbnails upon changes in base cards config
2828
// TODO: automatic rebulding of thumbnails upon changes in file
29-
// TODO: make this safer
30-
const width = parseYaml(base.content).views[0].cardSize as number;
31-
const height = width * parseYaml(base.content).views[0].imageAspectRatio;
29+
const parsed = parseYaml(base.content) as BaseSchema;
30+
console.debug(parseYaml(base.content));
31+
let width, height;
32+
const view = parsed.views[base.cardsIndex];
33+
if (parsed.views && view) {
34+
width = view.cardSize as number;
35+
height = width * (view.imageAspectRatio as number);
36+
} else {
37+
width = 1080;
38+
height = 1920;
39+
}
40+
3241
let files = getFiles(this.app, base);
3342
for (const file of files) {
3443
console.debug(file);

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface Base {
1919
path: string;
2020
file: TFile;
2121
content: string;
22+
cardsIndex: number;
2223
}
2324

2425
export interface BaseSchema {

0 commit comments

Comments
 (0)