farling42 / obsidian-import-json

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

Feature Request: Allow duplicate named files to be created by appending something to file name #62

Closed buttonpushertv closed 6 months ago

buttonpushertv commented 8 months ago

Can there be a setting to allow for duplicate files to be renamed on import, by appending something to the filename?

The specific challenge here is that Fantasy Map Generator can create Burgs or Provinces that have the same name as other Burgs or Provinces. When importing via JSON, there's no easy way to handle those duplicates (unless you modify the JSON data). Yes, a CSV can be created to include path names that make this easier to handle, but not so easy for JSON data.

As you know, REPLACE just overwrites the previous version with next dupe, with no indication that that has happened. With KEEP, you do get to see the ones that got skipped, but then there's no easy way to re-import those skipped items to another location or with alternate names. And APPEND, merges two, or more, files into one (currently, this is probably the "best" solution, as you will get all the data on import, but you will need to go through and manually break those merged files apart - it would be nice to get a report at the end that lists all notes that were appended or merged.).

A simple solution would be to append something like a hyphen and number for each subsequent duplicate (e.g. Anytown, Anytown-2, Anytown-3). Another option would be to have a backup field to use as the value to append to the file name (like state or id appended with a hyphen or surrounded by parentheses). Possibly having the ability to use other fields within the prefix/suffix settings or use to multiple fields within the filename setting could also be solutions, but I'm not sure that would be the most elegant solution (e.g. {{id}}-{{name}} OR {{name}}-{{state}}).

farling42 commented 8 months ago

That sounds like a fairly simple improvement. I would have to decide how to handle the case where replacement of old notes (e.g. from a previous import) is required, but duplicates then appear in the new data.

buttonpushertv commented 7 months ago

In version 1.5.3 of Obsidian, the APPEND option no longer seems to add the content of a duplicated note to the end of an existing note with the same name. It just fails with an error: app.vault.create: Error: File already exists.

I looked for a way to allow APPEND to work as it had previously in the settings and I can't see an option (within vanilla Obsidian) that will allow for a file to be created with the same name as an existing file.

Perhaps something has changed with app.vault.create that causes this to fail now?

farling42 commented 7 months ago

I will investigate.

The call to vault.create should only be happening in APPEND mode if the file doesn't already exist. I guess the function I'm using to test for the file is broken (or doesn't exist) in 1.5.3

farling42 commented 7 months ago

I've just checked with a test data set, and the append seems to be working normally for me. Is there anything strange in the pathname to the note within your vault which might cause the this.app.vault.getAbstractFileByPath to fail to find the file.

buttonpushertv commented 7 months ago

I'm not sure what's causing the issue on my end. It does create the first note of the duplicated elements in the correct location. I assume that means that the path is valid and is working properly. When it encounters the next duplicated element (by the name element, it fails with that app.vault.create error. I looked over the code and I see that it shouldn't be calling create if a note exists and APPEND is selected, but it seems to be calling that on my end. It is entirely possible I'm missing a step or important setting that causes this.

I've attached the FMG JSON along with the Handlebars & Helper files needed to reproduce this error. The Atlas, States, and Provinces passes all work fine, but there are no duplicated elements there. Here are the settings used for the Burgs import pass:

Choose JSON/CSV file: (FMG Full JSON file) Specify URL to JSON data: Leave blank Data contains multiple JSON objects: Leave blank Choose TEMPLATE file: Handlebar - FMG - Burgs JSON Template.md (see below) Choose HELPERS file: Handlebar - FMG - JSON Custom Helpers.js (see below) Field containing the data: _pack.burgs (using the entire JSON file - helps with the cross-referencing) Each subfield is a separate note: unchecked Field to use as Note name: name Note name prefix/suffix: (prefix leave blank) | (suffix leave blank) Allow paths in Note name: unchecked How to Handle Existing Notes: REPLACE or APPEND (see note below) Name of Destination Folder in Vault: 01_Campaigns/(specific Campaign Path)/10-Atlas/Burgs/

(The above is taken from a guide I am writing up for my TTRPG Vault - in hopes of sharing it with others. The "(specific Campaign Path)" piece of the last line gets replaced when you do the import pass with whatever Campaign you are importing to. In the case of this error, the path is: Testing JSON Imports. I use a ton of QuickAdd/Templater stuff to create that hierarchy, and it all works, so I think it is a valid path name).

Handlebars-JSON.zip

And just for reference, here is a Google Sheet that shows which Burg names are duplicated (highlighted in red): https://docs.google.com/spreadsheets/d/1M9lq_7OMVPQo_LnU3tYciK4588UjAYPcg5JosZ3mqsM/edit?usp=sharing

buttonpushertv commented 7 months ago

Oh, and it is processing the individual elements and extracting a uniqueName for each note within a group that is duplicated. It just doesn't call app.vault.append where it should.

Here is the list of errors it generates:

uniqueName: Clifton(Penkwar) VM1229:284
uniqueName: Dunsle(Framplia) VM1229:284
uniqueName: Thaxning(Uxbrigh) VM1229:284
uniqueName: Cambrigh(Penkwar) VM1229:284
uniqueName: Clifton(Chishokia) VM1229:284
app.vault.create: Error: plugin:obsidian-import-json:20872
File already exists. 
uniqueName: Apton(Yeowland) VM1229:284
uniqueName: Horshobury(Penkwar) VM1229:284
uniqueName: Westcham(Chil) VM1229:284
uniqueName: Thaxning(Mouria) VM1229:284
app.vault.create: Error: plugin:obsidian-import-json:20872
File already exists. 
uniqueName: Apton(Mouria) VM1229:284
app.vault.create: Error: plugin:obsidian-import-json:20872
File already exists. 
uniqueName: Penkleigh(Godgia) VM1229:284
uniqueName: Cambrigh(Mouria) VM1229:284
app.vault.create: Error: plugin:obsidian-import-json:20872
File already exists. 
uniqueName: Penkleigh(Chishokia) VM1229:284
app.vault.create: Error: plugin:obsidian-import-json:20872
File already exists. 
uniqueName: Kingley(Wisterlia) VM1229:284
uniqueName: Clifton(Framplia) VM1229:284
app.vault.create: Error: plugin:obsidian-import-json:20872
File already exists. 
uniqueName: Penkleigh(Flokia) VM1229:284
app.vault.create: Error: plugin:obsidian-import-json:20872
File already exists. 
uniqueName: Kingley(Lereteria) VM1229:284
app.vault.create: Error: plugin:obsidian-import-json:20872
File already exists. 
uniqueName: Horshobury(Cockingia) VM1229:284
app.vault.create: Error: plugin:obsidian-import-json:20872
File already exists. 
uniqueName: Westcham(Godgia) VM1229:284
app.vault.create: Error: plugin:obsidian-import-json:20872
File already exists. 
uniqueName: Dunsle(Hatrinch) VM1229:284
app.vault.create: Error: plugin:obsidian-import-json:20872
File already exists. 
buttonpushertv commented 7 months ago

Bah. I see what the issue is....an extra /

This line of the plugin: let filename = settings.folderName + "/" + this.validFilename(notefile) + ".md"; adds the trailing slash.

I had put a trailing slash into the Name of Destination Folder in Vault: field in the importers options.

My error - at least for this v.1.5.3 suspected issue....sigh.

Still would be nice to be able to use a second field or other method of differentiating duplicates.

farling42 commented 7 months ago

So double-slash is doing something weird in Obsidian's vault?

buttonpushertv commented 7 months ago

With the trailing slash in the path field, it will create the first note and then give that error on creations of subsequent notes with the same name. And it appears to be attempting to create notes via app.vault.create and not append.

If I remove that trailing slash in the options, it will APPEND the file content properly. And not give any errors.

I'm not sure why a double slash does or doesn't work or causes that error.

farling42 commented 7 months ago

Duplicate slashes are handled properly in version 0.34.0

farling42 commented 6 months ago

Version 0.35.0 provides a new checkbox "Add suffix on duplicate Note Names". It also allows a Note name syntax similar to "${country}-${city}" if you want a name made from more than one field.