Giter Club home page Giter Club logo

Comments (4)

farling42 avatar farling42 commented on September 3, 2024

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);

from obsidian-import-json.

rustyleaf avatar rustyleaf commented on September 3, 2024

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);

from obsidian-import-json.

farling42 avatar farling42 commented on September 3, 2024

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');

from obsidian-import-json.

rustyleaf avatar rustyleaf commented on September 3, 2024

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

from obsidian-import-json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.