I exported a notebook to markdown (which is great), but I found that the files don't have links to other files like they were in Siyuan.
So, is there anyway to make markdown exports have links to other files?
I exported a notebook to markdown (which is great), but I found that the files don't have links to other files like they were in Siyuan.
So, is there anyway to make markdown exports have links to other files?
@participants We are considering built-in support for anchor hash export mode Issue #10265 · siyuan-note/siyuan
I was able to do it (for documents only, not blocks, as I don't know how to find out if the link is of a block or a document, and I don't want to mutate the linked documents anyway).
Here's the Python script:
import os
import re
import requests
# Replace 'YOUR_API_ENDPOINT' with the actual Siyuan API endpoint
SIYUAN_API_ENDPOINT = "http://localhost:6806"
# Function to make a SQL query to Siyuan API
def query_block_details(block_id):
query = {"stmt": f"SELECT * FROM blocks WHERE id='{block_id}'"}
response = requests.post(f"{SIYUAN_API_ENDPOINT}/api/query/sql", json=query)
return response.json()
# Function to process each markdown file
def process_markdown_file(file_path):
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
# Use regex to find instances of [Anchor text](siyuan://blocks/block-id)
matches = re.findall(r"\[.*?\]\(siyuan://blocks/(.*?)\)", content)
for block_id in matches:
# Query Siyuan API to get block details
result = query_block_details(block_id)
# Assumes ref is of a document, not a block
if result:
hpath_value = result["data"][0].get("hpath")
# Replace the siyuan://blocks/block-id with hpath_value.md
new_link = f"({hpath_value}.md)"
# Replace the old link in the content
content = content.replace(f"(siyuan://blocks/{block_id})", new_link)
# Write the updated content back to the file
with open(file_path, "w", encoding="utf-8") as file:
file.write(content)
def process_markdown_directory(directory_path):
for root, _, files in os.walk(directory_path):
for file_name in files:
if file_name.endswith(".md"):
file_path = os.path.join(root, file_name)
process_markdown_file(file_path)
# Replace 'YOUR_MARKDOWN_DIRECTORY_PATH' with the actual path to your exported markdown files
markdown_directory_path = "YOUR_MARKDOWN_DIRECTORY_PATH"
# Process each markdown file in the specified path
process_markdown_directory(markdown_directory_path)
You're welcome! I actually was too lazy to write it myself so I asked ChatGPT to do it for me and changed some mistakes in its code.
And I might as well do it for non-document blocks, I will post the solution here too.
In any case, it seems like this problem can be solved easily and be integrated into SiYuan.
I am not familiar with SiYuan code or Golang in general so someone else more familiar may make a Pull Request for this feature way faster than me.
If not, I don't mind spending some time to learn enough to build this feature into SiYuan. It's the least I can do in return for getting this amazing app for free!
Thanks @88250 @sagar and long live the open source!
@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
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
<a name="block-id"></a>
siyuan://blocks/block-id
part of the link to hpath_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 🙏
Welcome to here!
Here we can learn from each other how to use SiYuan, give feedback and suggestions, and build SiYuan together.
Signup About