-
Is it possible for markdown export to link to other files?
2023-12-02 01:10@adham Wow 😍 You are a master programmer indeed! I was still trying to figure out how to use Python to open all .md files in a directory, by copy/pasting code from Stackoverflow 😄 This solution is exactly what I meant.
I do believe it can be extended to include references to any arbitrary block, not just a document block. You did say that you don't want to mutate the linked documents, but I am just laying out the approach in case any other master programmers want to try it 😄. You wrote,
I don't know how to find out if the link is of a block or a document,
I see a few different possible checks for this
- Look at the 'type' field in the response of the SQL query. If it is a document block, the type value will be 'd'. Other type values are given in the documentation at /Please Start Here/Advanced search/Type filtering
- I believe that if the block is of type document, then the 'root_id' will be the same as 'id'. See the documentation of the SQL table fields at /Please Start Here/Advanced search/Database table
So one or both of the above checks can be used to determine if the block is of type 'document' or something else.
If the block is of type something else, then the way to link to it would be
- Open the target document that contains the block (i.e. the hpath)
- Search for the occurrence of the content in the 'markdown' field of the SQL query response. This will place the "cursor" inside the document to the start of the paragraph (or whatever other content) the block points to
- At this cursor position, insert an HTML anchor text like:
<a name="block-id"></a>
- Then, update the
siyuan://blocks/block-id
part of the link tohpath_value.md#block-id
This approach enables having links in the markdown files to any other markdown files or parts inside markdown files.
@adham Thank you once again for your example code 🙏
-
Is it possible for markdown export to link to other files?
2023-12-01 08:16I think there is a fairly simple solution to this, which I will describe below. I think it can be implemented as a simple Python script that does some post-processing of the exported markdown files. I will try to create a proof-of-concept script, but it will take me a few days because I am not a programmer and will need to learn some Python programming first.
Solution description
-
In Settings > Export configure 'Ref' to 'Anchor text with block URL'. After doing this, a link to another block in a SiYuan note appears in the exported Markdown file as
[Anchor text](siyuan://blocks/block-id)
. -
Write a Python script that opens each exported markdown file and uses regex matching to identify all instances of
[Anchor text](siyuan://blocks/block-id)
. For each instance, -
Use the
/api/query/sql
API endpoint to get details of theblock-id
by making a query like:{ "stmt": "SELECT * FROM blocks WHERE id='block-id'" }
-
In the query result, you can find the
hpath
key whose value is a human readable path.- If the block-id is for a block of type document (which would be the case for a link to another document), then the Python script just needs to replace
siyuan://blocks/block-id
with hpath_value.md which would be the relative path to the exported markdown file that the link points to. - If the block-id is for a block inside a document (e.g. to some arbitrary paragraph block in the document), all you need to do is to open the exported markdown document containing that paragraph (from the value of
hpath
for that block-id), find the start of the paragraph and add an<a name="block-id"></a>
anchor at the start of the paragraph. Then, thesiyuan://blocks/block-id
in the exported markdown file can be replaced byhpath_value.md#block-id
- If the block-id is for a block of type document (which would be the case for a link to another document), then the Python script just needs to replace
This would basically solve the problem of having correct links in the exported markdown files and additionally the exported notes can easily be converted to HTML and published e.g. with a static site generator like Hugo.
@88250 Do you see any problems with this approach? I don't understand why SiYuan does not do this already when exporting to markdown.. but in any case, a Python script that does the above can post-process the exported markdown files from SiYuan and fix all the links. What do you think?
-
-
Is it possible for markdown export to link to other files?
2023-11-29 01:26@88250 I think this is an important missing feature when it comes to publishing the notes. Let me explain:
Many users, including me, like to make a lot of links between notes. We also like to publish some set of notes on our websites/blogs/digital gardens. With most other note-taking software, you can export the notes to markdown, which includes links to other markdown documents (or even specific paragraphs in the markdown documents). Then, static site generators like Hugo can ingest the markdown files and produce HTML pages for the web. These HTML pages have all the correct links to other notes' HTML pages. With SiYuan, since the exported markdown does not contain links to other notes, publishing notes with links to other notes is not possible at all. I think this is a problem for many potential users. Do you have any other suggestions for how to publish notes in SiYuan with all links-between-notes working properly?
I think this is important for another reason: With other tools like Obsidian, the notes are stored as normal markdown files. This means that the notes are not "trapped" inside the note-taking software. SiYuan does not store content as markdown files. Even if the format is still open, it still feels like the notes are "trapped" or "locked inside" SiYuan. If SiYuan has an excellent ability to export the notes to markdown (e.g. with all the links preserved) it would feel less like the notes are locked away inside SiYuan. I think this will increase trust in SiYuan, since users will not worry that if SiYuan unfortunately disappears, then their notes will disappear with it.
I understand that because SiYuan offers a lot more complex formatting options, exporting to Markdown with everything properly preserved is not possible. This comes from the limitations of Markdown. However, there should ideally be a way to publish the notes (perhaps to HTML) so that all the formatting and links are preserved. I think maybe this is currently possible for each entry in the Doc Tree, but ideally, it should be possible for an entire hierarchy in the Doc Tree i.e. a given note and all notes below it should be exported. Please let me know if this is already possible.
-
SiYuan Lifetime Subscription is now on sale, seats are limited
2023-11-26 11:31For those wondering how this works: You will get an email with a link to a PayPal invoice. After clicking that link, you can make the payment even if you do not have a PayPal account. It is possible to pay using a regular credit card (or Apple pay, if you open the link on an apple device).
-
Opening backlinks
2023-11-26 11:26Ahhh, thank you. Ctrl+Click indeed gave me what I want.
Also a note to other readers: Shift+Click will open the backlink document in a new panel below the current document. That is also nice! 😄