Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A wish to use import statements in the handlebars custom helper functions...? #38

Closed
rustyleaf opened this issue Feb 25, 2023 · 4 comments

Comments

@rustyleaf
Copy link

I am attempting to simply require a file into that helper function but it seems to me that when it runs it is running from the "renderer" path location.
/Applications/Obsidian.app/Contents/Resources/electron.asar/renderer
The absolute path for requiring a file works but would be nice to have relative paths.
Not sure if this is doable with ease...?

//example file to require: planets.js

module.exports = {
  planet_symbol: {
    sun: '☉',
    earth: '⊗',
    moon: '☾',
    mars: '♂',
    venus: '♀',
    mercury: '☿',
    jupiter: '♃',
    saturn: '♄',
    neptune: '♆',
    uranus: '♅',
  }
}
// js 'require' with path relative to the helper function file...
const Planets = require('./planets.js');
console.log(Planets.planet_symbol[sun])
@farling42
Copy link
Owner

farling42 commented Feb 25, 2023

The contents of the supplied additional handlebars custom helper file are read into the importer and used as the body of a function using the following code:

let initJsonHelpers = new Function('handlebars', await helperfile.text());
if (initJsonHelpers) initJsonHelpers(handlebars);

The function is then called and the handlebars parameter is provided for the helper file to register it's own helpers. My testing used a file containing the following:

function hb_farling() {
	let orig = arguments[0];
	orig += ' from Helper';
	return orig;
}
handlebars.registerHelper('farling', hb_farling);

@rustyleaf
Copy link
Author

rustyleaf commented Feb 26, 2023

Thanks @farling42. I love what the helper functions can do and I love the functionality of your plugin!

I have got the helper functions working and have included an example of how I am trying to require the file... but it seems it goes beyond my basic javascript knowledge - because of how electron packages the module I got a bit lost.

Thought I would clarify - but no big issue if it gets complicated i will just use absolute paths to require the file.

console.log(__dirname);
/// Applications/Obsidian.app/Contents/Resources/electron.asar/renderer

const Elements = require('./elements.js');

/*
 I can add some of my extra js objects like this (below) *rt_expand_center()*
 that can be used in the helper functions etc.. but I was hoping to 
 use require's to bring them in from a local directory so I can use them like on line 36
 but it seems due to the path when the actual function runs
 it cannot find the file.
 */

const bodygraph_centers = {
  identity: 'G-center',
  life_force: 'Sacral',
  mind: 'Ajna',
  emotion: 'Solar Plexus',
  expression: 'Throat',
  intuition: 'Spleen',
  drive: 'Root',
  willpower: 'Heart/Ego',
  inspiration: 'Head',
};

const clean_string = (str) => {
  return str.toLowerCase().replace(/\s/g, '_');
};

function rt_get_planet_symbol() {
  const planet = arguments[0];
  const _planet = clean_string(planet);
  const element = Elements.planet_symbol[_planet];
  let str = `${planet} [${element}]`;
  return str;
}

handlebars.registerHelper('get_planet_symbol', rt_get_planet_symbol);

function rt_expand_center() {
  const type = arguments[0];
  const _type = clean_string(type);
  const center = bodygraph_centers[_type];

  let str = `center: ${center}`;
  str += '\n';
  str += `center_type: ${type}`;
  return str;
}

handlebars.registerHelper('expand_center', rt_expand_center);

@farling42
Copy link
Owner

You could try using the full path from the root of your vault, such as:

const Elements = require(app.vault.root.path + 'foldername/elements.js');

@rustyleaf
Copy link
Author

That's perfect!!! It works...! Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants