Markdown input as folder or file
When using markdown to anki, it's likely to create different templates for different decks. As #11 turns markdown files in "templates" or "note types", it's likely that users will have a main folder where all of the templates live.
As of now, the configuration only expects a file as input, but it would be useful if it could also accept a folder and automatically iterate and process all files.
This would require also a slightly different interface (UI) that reflects which file is being processed, as well as thinking how to handle bad cards (maybe they can be written to the original file after clearing it?)
Steps
- Update the config interface to reflect the change
- Update the config validation to allow for directories
- Add a conditional statement checking if it's a directory or a file
- Use os to find and iterate through all .md files
- Add for loop to process all files
- Update logging to reflect what file is being processed
I would like to have this feature, would be happy to implement it.
One question though about this: what if user has a folder if markdown files where not all files are "Markdown2Anki" files and shouldn't be processed by the md2anki?
I can see this happening for the Obsidian users, where they could keep the usual markdown about some topic and then the "Markdown2Anki" file with questions and answers.
What would be the suitable filtering system in this case? A specific filename convention? Usage of mandatory field in the frontmatter, like md2anki: true?
In this case the UI should also show which (or just how many) MD files it found, and how many are even valid to be considered for processing.
What do you think?
I would like to have this feature, would be happy to implement it.
One question though about this: what if user has a folder if markdown files where not all files are "Markdown2Anki" files and shouldn't be processed by the
md2anki?I can see this happening for the Obsidian users, where they could keep the usual markdown about some topic and then the "Markdown2Anki" file with questions and answers.
What would be the suitable filtering system in this case? A specific filename convention? Usage of mandatory field in the frontmatter, like
md2anki: true?In this case the UI should also show which (or just how many) MD files it found, and how many are even valid to be considered for processing.
What do you think?
We can either make users add the flag or enforce having only markdown2anki template files in the folder.
Enforcing filenames is pretty much the same as enforcing users to have a special folder that only holds md2anki files and is not very flexible I think.
I think the enforcing only markdown2anki files in a folder is hard...
Since there's not really an easy way to run pre-processing to ensure files are valid markdown2anki inputs, so I'd go with the first option and make it required only if users are using the folder input.
Example
- Users uses the input folder option -> only files with "md2anki: true" in the frontmatter will be selected for processing
- User uses a file input -> the flag is not required, since we don't need to filters files to know which one to process
CLI UX
As for the user feedback in the CLI, I think saying "📚 X md2anki files found! Starting to process them..." and "📖 processing:
Bad cards
What puzzles me a bit would be how to handle "bad cards" with multiple files. It's easy if the "clear file" option is set to true, since we can just replace the inserted cards with the bad ones, but I'm not sure how to handle them when users want to keep their input...
Let me know if you agree and have ideas on this :)
We can either make users add the flag or enforce having only markdown2anki template files in the folder.
Enforcing filenames is pretty much the same as enforcing users to have a special folder that only holds md2anki files and is not very flexible I think.
I think the enforcing only markdown2anki files in a folder is hard... Since there's not really an easy way to run pre-processing to ensure files are valid markdown2anki inputs, so I'd go with the first option and make it required only if users are using the folder input.
I agree with this. The filename pattern and keeping only the markdown2anki in a folder is hard. And also not practical. As a user I just want to point the anki2md towards some folder and it should figure out what needs to be converted and what left alone.
As for the CLI UX, how to specify if we are doing the input file or input folder and what to do with bad cards feature, I have some ideas that I have been thinking for some time now. The ideas themselves likely clash with the current design of the md2anki, but I think that the usage of the tool would be improved. In very general terms they fall under the category of simplification: if we can do the same thing with less why don't we?
Ideas
-
I think this one is the prerequisite for a lot of other changes that I will write below: let's remove
clear file?setting. It is quite uncommon for a CLI tool to delete given input file (besidesrmI can't think quickly of one). If I want to remove the file I still can. Once we implement the update Anki card feature #8 the clearing of the file would also be counter intuitive. Removing this feature would then make the input file backup feature obsolete. -
Let's make the input file or input folder as a positional CLI arguments. For example:
- If I run
md2anki some_file.md, onlysome_file.mdis processed. - If I run
md2anki some_file.md some_file2.md,some_file.mdandsome_file2.mdare processed. - If I run
md2anki some_dir/, all files insome_dirand it's sub-directories are processed.
- If I run
~~If I have multiple files and I want to process them, I don't want to change the input file every-time in the .ini config file when I am switching.~~ I see now that there is a -f command line option, so editing in the .ini file is not needed. In that case I would just remove the option from the .ini file andkeep it only as a mandatory positional argument (when someone wants to parse some md files, calling md2anki without args should still run the wizard or print help).
-
Do we need the bad cards feature? If we fail to parse a card shouldn't be enough to just specify the line number and the type of the syntax error (maybe a small preview window with an arrow pointing to the exact problem (that might be too much though). If md2anki is not clearing the input files and if we have #8 done, then the bad cards stays in their place and user can correct them and then re-run the md2anki command. Rinse and repeat until all cards are uploaded.
-
Something unrelated to this topic: while I was writing the support for the Ankiconnect I was looking at the image folder setting that is currently required. I was thinking why is that? As far as I could quickly tell, we should be able to determine if card in markdown uses the image link, and if it does we go look for it (to the local storage or the web) and then upload it to the Anki.
As a final note, but the most important one: This is just my perspective, which might differ from yours. And that is ok. I am happy that you made such a cool project, that also contains one of the cleanest codebases that I have seen in a while :)