Is it possible for markdown export to link 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?

    Related articles

    Welcome to here!

    Here we can learn from each other how to use SiYuan, give feedback and suggestions, and build SiYuan together.

    Signup About
    Please input reply content ...
    • adham
      Author

      Thanks @sagar for your helpful solution!

    • MiscReply
    • Siysuy

      The anchor hash feature currently creates a span object <span id="20231110120736-ze6m41f"></span>. Some kind of program should be able to understand and find this block inside the note as reference. Can anyone help me understand how this can be useful after export, where do I use this, which program understands this syntax?

      The export mode Anchor hash for notebook Markdown exporting has some limitations.

      In the case i want to publish a particular note (with links to other notes) my approach is:

      1. From main notebook export the notebook you want to publish to .sy.zip file
      2. Create new (temporary) notebook
      3. import .sy.zip file to new temp notebook
      4. Export temp notebook export it to any selectable format (only markdown is currently selectable)

      Other limitation of Anchor has export and the only way I got it to work (performed testing by exporting to Obsidian):

      • currently only works for notebook and does not allow nested notes. In the process of exporting all notes must be unnested inside notebook for .md export to function.
      • Currently does not support block reference. Can only link to main note. Tested by importing to obsidian (not a siyuan issue, but a markdown syntax limitation i presume)

      Depending on the responses here I might add a feature request soon that involves the concept of user selectable export syntax. In which before execution of export using anchor hash a user can type in the conversion syntax to apply. User is presented with input fields much like the settings > Export > Anchor text wrapping symbol fields.

      Conversion syntax for siyuan > obsidian would be: [[test3.md/Note 2#^d8bf62|Note 2]] and conversion input fields could somehow be [[ .. / .. #^ .. | .. ]]

      1 Reply
    • adham 1
      Author

      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)
      
      
    • Siysuy

      I think the issue remains with anchor hash for the reason that it is limited to notebook currently, meaning I cannot select rendering from Markdown to HTML from the notebook_ex > export > (selectable=Markdown/siyuan.sy.zip).

      How would i export my notes to HTML with anchor hash? I think I would be limited to using pandoc input.md -o output.html, I tried it with pandoc, which succesfully creates html with link to other note, but it just opens the other note without navigation to the <span> block, so no hash jump in browser (brave/firefox) to block.

      1 Reply
      4 Operate
      Siysuy updated this reply at 2024-03-21 00:58:07
      Siysuy updated this reply at 2024-03-21 00:55:13
      Siysuy updated this reply at 2024-03-21 00:41:56
      Siysuy updated this reply at 2024-03-21 00:34:11
    • Visit all replies