Nriver / trilium-py

Python client for ETAPI of Trilium Note. Trilium 的 Python版 ETAPI 客户端
GNU Affero General Public License v3.0
118 stars 27 forks source link

Import markdown files with frontmatter yaml? #24

Closed justyns closed 9 months ago

justyns commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

The file upload is introduced because of this issue https://github.com/Nriver/trilium-py/issues/5. I implement the feature in this way https://github.com/Nriver/trilium-py/commit/99d2197c42fffae4dea3f91d89246550a7440f7f. 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.