Skip to content

Commit

Permalink
refactor: need plugin access in replaceVars() to handle expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgurney committed Oct 28, 2024
1 parent a1ae058 commit 0941231
Showing 4 changed files with 17 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/Settings/UI/Modals/ItemSuggestModal.ts
Original file line number Diff line number Diff line change
@@ -89,8 +89,8 @@ export class ItemSuggestModal extends SuggestModal<ToolbarItemSettings> {
// ...and is visible on this platform
if ((Platform.isMobile && showOnMobile) || (Platform.isDesktop && showOnDesktop)) {
// ...and does not have a var link and label/tooltip that resolves to nothing
if (!(hasVars(item.link) && replaceVars(this.app, item.link, this.activeFile, false) === "") &&
!(hasVars(itemName) && replaceVars(this.app, itemName, this.activeFile, false) === "")) {
if (!(hasVars(item.link) && replaceVars(this.plugin, item.link, this.activeFile, false) === "") &&
!(hasVars(itemName) && replaceVars(this.plugin, itemName, this.activeFile, false) === "")) {
itemSuggestions.push(item);
}
}
@@ -188,7 +188,7 @@ export class ItemSuggestModal extends SuggestModal<ToolbarItemSettings> {

let itemMeta = itemNameEl.createSpan();
// replace variables in labels (or tooltip, if no label set)
let title = hasVars(itemName) ? replaceVars(this.app, itemName, this.activeFile, false) : itemName;
let title = hasVars(itemName) ? replaceVars(this.plugin, itemName, this.activeFile, false) : itemName;

itemMeta.addClass("note-toolbar-item-suggester-type");
switch (item.linkAttr.type) {
8 changes: 4 additions & 4 deletions src/Utils/ImportExport.ts
Original file line number Diff line number Diff line change
@@ -65,9 +65,9 @@ function exportToCalloutList(
let itemIcon = (options.includeIcons && item.icon) ? toIconizeFormat(item.icon) : '';
itemIcon = (itemIcon && item.label) ? itemIcon + ' ' : itemIcon; // trailing space if needed

let itemText = options.replaceVars ? replaceVars(plugin.app, item.label, activeFile, false) : item.label;
let itemLink = options.replaceVars ? replaceVars(plugin.app, item.link, activeFile, false) : item.link;
let itemTooltip = options.replaceVars ? replaceVars(plugin.app, item.tooltip, activeFile, false) : item.tooltip;
let itemText = options.replaceVars ? replaceVars(plugin, item.label, activeFile, false) : item.label;
let itemLink = options.replaceVars ? replaceVars(plugin, item.link, activeFile, false) : item.link;
let itemTooltip = options.replaceVars ? replaceVars(plugin, item.tooltip, activeFile, false) : item.tooltip;

itemText = escapeTextForCallout(itemText);
itemLink = escapeLinkForCallout(itemLink);
@@ -87,7 +87,7 @@ function exportToCalloutList(
break;
case ItemType.File:
// check if the provided file links to a folder, and if so replace with a folder
let resolvedItemLink = replaceVars(plugin.app, itemLink, activeFile, false);
let resolvedItemLink = replaceVars(plugin, itemLink, activeFile, false);
let fileOrFolder = this.app.vault.getAbstractFileByPath(resolvedItemLink);
if (fileOrFolder instanceof TFolder) {
itemsExport += options.useDataEls
6 changes: 3 additions & 3 deletions src/Utils/Utils.ts
Original file line number Diff line number Diff line change
@@ -290,20 +290,20 @@ export function removeComponentVisibility(platform: { allViews?: { components: C

/**
* Replace variables in the given string of the format {{variablename}}, with metadata from the file.
* @param app App
* @param plugin NoteToolbarPlugin
* @param s String to replace the variables in.
* @param file File with the metadata (name, frontmatter) we'll use to fill in the variables.
* @param encode True if we should encode the variables (recommended if part of external URL).
* @returns String with the variables replaced.
*/
export function replaceVars(app: App, s: string, file: TFile | null, encode: boolean): string {
export function replaceVars(plugin: NoteToolbarPlugin, s: string, file: TFile | null, encode: boolean): string {

let noteTitle = file?.basename;
if (noteTitle != null) {
s = s.replace('{{note_title}}', (encode ? encodeURIComponent(noteTitle) : noteTitle));
}
// have to get this at run/click-time, as file or metadata may not have changed
let frontmatter = file ? app.metadataCache.getFileCache(file)?.frontmatter : undefined;
let frontmatter = file ? plugin.app.metadataCache.getFileCache(file)?.frontmatter : undefined;
// replace any variable of format {{prop_KEY}} with the value of the frontmatter dictionary with key = KEY
s = s.replace(/{{prop_(.*?)}}/g, (match, p1) => {
const key = p1.trim();
14 changes: 7 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -635,8 +635,8 @@ export default class NoteToolbarPlugin extends Plugin {
if ((Platform.isMobile && showOnMobile) || (Platform.isDesktop && showOnDesktop)) {
// replace variables in labels (or tooltip, if no label set)
let title = toolbarItem.label ?
(hasVars(toolbarItem.label) ? replaceVars(this.app, toolbarItem.label, file, false) : toolbarItem.label) :
(hasVars(toolbarItem.tooltip) ? replaceVars(this.app, toolbarItem.tooltip, file, false) : toolbarItem.tooltip);
(hasVars(toolbarItem.label) ? replaceVars(this, toolbarItem.label, file, false) : toolbarItem.label) :
(hasVars(toolbarItem.tooltip) ? replaceVars(this, toolbarItem.tooltip, file, false) : toolbarItem.tooltip);
switch(toolbarItem.linkAttr.type) {
case ItemType.Break:
// show breaks as separators in menus
@@ -664,7 +664,7 @@ export default class NoteToolbarPlugin extends Plugin {
}
default:
// don't show the item if the link has variables and resolves to nothing
if (hasVars(toolbarItem.link) && replaceVars(this.app, toolbarItem.link, file, false) === "") {
if (hasVars(toolbarItem.link) && replaceVars(this, toolbarItem.link, file, false) === "") {
break;
}
menu.addItem((item: MenuItem) => {
@@ -793,7 +793,7 @@ export default class NoteToolbarPlugin extends Plugin {

// if link resolves to nothing, there's no need to display the item
if (hasVars(itemSetting.link)) {
if (replaceVars(this.app, itemSetting.link, activeFile, false) === "") {
if (replaceVars(this, itemSetting.link, activeFile, false) === "") {
itemEl.addClass('hide'); // hide the containing li element
return;
}
@@ -804,11 +804,11 @@ export default class NoteToolbarPlugin extends Plugin {

// update tooltip + label
if (hasVars(itemSetting.tooltip)) {
let newTooltip = replaceVars(this.app, itemSetting.tooltip, activeFile, false);
let newTooltip = replaceVars(this, itemSetting.tooltip, activeFile, false);
setTooltip(itemSpanEl, newTooltip, { placement: "top" });
}
if (hasVars(itemSetting.label)) {
let newLabel = replaceVars(this.app, itemSetting.label, activeFile, false);
let newLabel = replaceVars(this, itemSetting.label, activeFile, false);
let itemElLabel = itemEl.querySelector('.cg-note-toolbar-item-label');
if (newLabel) {
itemElLabel?.removeClass('hide');
@@ -968,7 +968,7 @@ export default class NoteToolbarPlugin extends Plugin {

if (hasVars) {
// TODO: expand to also replace vars in labels + tooltips
linkHref = replaceVars(this.app, linkHref, activeFile, false);
linkHref = replaceVars(this, linkHref, activeFile, false);
debugLog('- uri vars replaced: ', linkHref);
}

0 comments on commit 0941231

Please sign in to comment.