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

Import markdown files with frontmatter yaml? #24

Closed
justyns opened this issue Aug 25, 2023 · 5 comments
Closed

Import markdown files with frontmatter yaml? #24

justyns opened this issue Aug 25, 2023 · 5 comments

Comments

@justyns
Copy link

justyns commented Aug 25, 2023

Thanks for creating this library! I've only just started testing out Trilium, but I'm liking it so far. One of the things I'd like to do is import all of my old notes from Joplin and Obsidian.

I used a script similar to the one in the README but noticed yaml frontmatter doesn't get preserved. For example, a joplin exported note like this:

---
title: 2021-03-05 - hvac furnace tune up
updated: 2021-03-05 15:27:20Z
created: 2021-03-05 13:43:07Z
latitude: xx.xx
longitude: -yy.yy
altitude: 101
---

note contents here

Gets turned into this (when viewing the source of the trilium note):

<hr />

<p>title: 2021-03-05 - hvac furnace tune up
updated: 2021-03-05 15:27:20Z
created: 2021-03-05 13:43:07Z
latitude: xx.xx
longitude: -yy.yy</p>

<h2>altitude: 101</h2>

<p>note contents here</p>

Is it possible (and does it make sense) to convert that yaml frontmatter and turn it into attributes on the note? For my example, I think it'd get turned into something like #latitude=xx.xx #longitude=-yy.yy #altitude=101 .

I've used https://pypi.org/project/python-frontmatter/ in the past and it worked okay. I haven't looked much at the code here or the trilium api, but I might try to take a look this weekend if I continue w/ trilium.

@Nriver
Copy link
Owner

Nriver commented Aug 25, 2023

Yes, it's possible to set the meta info as labels. You can use create_attribute to add those info.

file_path = "/home/nate/data/1/test.md"

res = ea.upload_md_file(
    parentNoteId="root",
    file=file_path,
)

note_id = res['note']['noteId']

import frontmatter

post = frontmatter.load(file_path)

for k in post.keys():
    res = ea.create_attribute(
        attributeId=None,
        noteId=note_id,
        type="label",
        name=k,
        value=str(post[k]),
        isInheritable=False,
    )

You may need to change the source code little bit to clean up the yaml part from the note content itself.

@justyns
Copy link
Author

justyns commented Aug 25, 2023

Thanks for the hint @Nriver .

I ended up modifying the upload_md_file method instead of writing a new script. I was able to get it to convert all of the frontmatter keys to labels and also remove the frontmatter from the note itself. This is like 90% of what I wanted.

Would you be interested in integrating it? If so, I can clean it up a little and maybe make it optional too, and create a pr.

Do you know if it's possible to change the created and updated timestamps for a note? I didn't see an option for it in create_note and I don't think I see it listed in https://github.com/zadam/trilium/blob/master/src/etapi/etapi.openapi.yaml either but I'm not sure if there's another option.

@Nriver
Copy link
Owner

Nriver commented Aug 26, 2023

PR is welcomed.

As far as I know, changing the timestamps by ETAPI is not supported yet. Maybe you can create a feature request for that.

@justyns
Copy link
Author

justyns commented Aug 26, 2023

One more question @Nriver, while testing this I noticed markdown files that link to other markdown files end up getting the 2nd markdown file uploaded as an attachment child note. Is this intentional?

Looking at the code, I'm not sure what the best way to change that to link to another regular note (instead of file attachment) would be since trilium might not have the note you're linking to (yet)

@Nriver
Copy link
Owner

Nriver commented Aug 27, 2023

The file upload is introduced because of this issue #5. I implement the feature in this way 99d2197. The use case I thought about was file attachment like .pdf, .doc, .xls, etc.

I never used link to another md file because the link is quite difficult to maintain as you have to manually input the relative path. And the md file may get move around or renamed which will make the link invalid.

The only way I can think about to fix this is to maitain a big dict of {note_id: note_path, ...} while uploading the markdowns. Then make another iterate of all the uploaded note contents to fix the internal links.

@Nriver Nriver closed this as completed Jan 4, 2024
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