Skip to content

Commit

Permalink
feat(header): use a list for banner_header, allowing to use frontmatt…
Browse files Browse the repository at this point in the history
…er keys

- {{aliases}} : Use first alias
- {{file}} : Use basename
- {{anykey}} : use this key if exists
Else, return the list join by space
  • Loading branch information
Mara-Li committed Sep 20, 2023
1 parent bbf31f0 commit ff4a6b4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/banner/Banner.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
export let x = 0.5;
export let y = 0.5;
export let icon: IconString | undefined = undefined;
export let header: string | null | undefined = undefined;
export let header: string[] | null | undefined = undefined;
export let lock = false;
export let viewType: 'editing' | 'reading';
Expand Down
26 changes: 21 additions & 5 deletions src/banner/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Platform, requestUrl } from 'obsidian';
import type { TFile } from 'obsidian';
import type { FrontMatterCache, TFile } from 'obsidian';
import { IMAGE_FORMATS } from 'src/bannerData';
import type { IconString } from 'src/bannerData';
import { plug } from 'src/main';
Expand Down Expand Up @@ -59,14 +59,14 @@ export const getHeights = (embedded: Embedded, _deps?: any[]): Heights => {

const hasHeaderElement = (
icon: IconString | undefined,
header: string | null | undefined
header: string[] | null | undefined
): boolean => !!(icon || header !== undefined);

export const getBannerHeight = (
heights: Heights,
source: string | undefined,
icon: IconString | undefined,
header: string | null | undefined
header: string[] | null | undefined
): string => {
if (source) return heights.banner;
else if (hasHeaderElement(icon, header)) return heights.icon;
Expand All @@ -86,7 +86,7 @@ const getHeaderExtraOffset = (offset: string, alignment: HeaderVerticalAlignment
export const getSizerHeight = (
heights: Heights,
source: string | undefined,
header: string | null | undefined,
header: string[] | null | undefined,
icon: IconString | undefined,
iconAlignment: HeaderVerticalAlignmentOption
): string => {
Expand All @@ -103,9 +103,25 @@ export const getSizerHeight = (
return '';
};

export const getHeaderText = (header: string | null | undefined, file: TFile): string => {
export const getHeaderText = (header: string[] | null | undefined, file: TFile):
string | undefined => {
if (header === undefined) return undefined;
if (header === null) return file.basename;
if (Array.isArray(header)) {
const frontmatter = plug.app.metadataCache.getFileCache(file)?.frontmatter as FrontMatterCache;
for (const h of header) {
if (h === '{{alias}}' && frontmatter?.aliases) {
return frontmatter.aliases[0];
} else if (h.match(/\{{(.*)\}}/)) {
const key = h.match(/\{{(.*)\}}/)![1];
if (frontmatter?.[key]) return frontmatter[key]; //note : The key must exists!
} else if (h === '{{file}}') {
return file.basename;
}
}
return header.join(' ');
}

return header;
};

Expand Down

0 comments on commit ff4a6b4

Please sign in to comment.