farling42 / obsidian-import-json

Plug-in for Obsidian.md which will create Notes from JSON files
MIT License
85 stars 5 forks source link

Process helpers on fieldname values from import dialog? #68

Closed buttonpushertv closed 4 months ago

buttonpushertv commented 4 months ago

Based on the feature added in v0.35.0, would it be possible to make use of JS helpers within that import dialog field to parse JSON data that is stored as an index (as opposed to the corresponding text value)?

For example, in a JSON export from https://azgaar.github.io/Fantasy-Map-Generator/ there are three main pieces of data that would be useful for creating unique note names: state, province, and burg. The most common element that could get a duplicate name in FMG are 'burg's. They have data for the state and province where the burg resides stored under each burg element, but only as numeric indicies that need to be looked up to determine their text name values.

Using the technique added in v0.35.0, the result for a burg created using the data extracted from those fields results in a value that looks like this: burg_name-province_cell-state_index (for example: Anytown-453-1). Not useful for human deciphering of those numeric values.

I have JS Helpers that successfully look up that information and can parse it correctly within a given imported note (the individual burg notes, for instance), what would be great is if those helpers could also be called from the import dialog and be processed on the elements referenced within those fields (to produce, for example, a result like this: Anytown-SomeProvince-TheState).

It would be great if that could be done and would resolve one lingering issue I am struggling against in working with FMG JSON data and importing it into notes. I don't know if that's an "easy" or "hard" thing to implement.

Thanks for considering.

farling42 commented 4 months ago

Version 0.36.0 has something which might be useful. I've added the ability to use some JS to define the note name, embedding the function body within @{...js...}. this refers to the current record/field being processed. dataRoot refers to the entire JSON object.

For example, based on some old files which were provided for processing the FMG:

@{return `${(this.state > 0) && dataRoot.pack.states.find(state => state.i === this.state)?.name || "Unknown" }-${this.name}`}
farling42 commented 4 months ago

I don't think it is easily possible to access the handlebars helper functions directly from within JS, so I went for this alternative method instead.

buttonpushertv commented 4 months ago

I haven't seen the 0.36.0 release show up in Obsidian Community Plugins yet. Is it live?

farling42 commented 4 months ago

Oh you are correct.

I forgot to update the 5 other files which have to be manually updated for every release! (Foundry VTT modules are so much easier to release.)

farling42 commented 4 months ago

Version 0.36.1 is correctly marked now, and should include the improvements mentioned above.