ChatGPT4 and outlink inheritance with Python

I wanted to find a way to inherit the outlinks of a file B into another file A. I experimented with GPT-4 and “we” (it 90%) came up with a python solution that can be used with cronjob or a similar task manager.

the code takes into account the following things and has the following restrictions:

  • Removes duplicates
  • You can specify to copy the outlinks after a specific line which begins with a trigger_text (could be a heading of the beginning of a line) If specified as None then appended to end of file
  • It scans file A weather the outlinks of B already exist
  • It writes them in a space separated style 10 per line
import os
import re
import sys

# Set the paths to your Obsidian vault and the note files
obsidian_vault_path = "Path to your Obsidian vault"
source_file = "Relative path to the source file (e.g. notes/source.md))"
target_file = "Relative path to the target file (e.g. notes/destination.md))"

# Set the trigger line or heading
trigger_line = None  # Set to None to append links to the end of the file or set to a line or heading e.g. "## Heading" or "Start of a line"

# Read the contents of the source and target files
with open(
    os.path.join(obsidian_vault_path, source_file), "r", encoding="utf-8"
) as file:
    source_content = file.read()

with open(
    os.path.join(obsidian_vault_path, target_file), "r", encoding="utf-8"
) as file:
    target_content = file.read()

# Extract the links from the source file
links = re.findall(r"\[\[([^|\]]+?)(\.md)?(?=\||\])\]\]", source_content)
links = list(
    set([link[0] + link[1] for link in links])
)  # Remove duplicates and format the links

# Filter out non-.md links
links = [link for link in links if link.lower().endswith(".md") or not "." in link]


# Find the position to insert links
insert_position = -1
if trigger_line:
    insert_position = target_content.find(trigger_line)
search_position = 0 if insert_position == -1 else insert_position

# If the trigger_line is found, adjust the insert_position to place links after the line
if insert_position != -1:

    insert_position = target_content.find("\n", insert_position)

# Extract the existing links from the target file after the trigger line
existing_links_section = target_content[search_position:]
existing_links = re.findall(
    r"\[\[([^|\]]+?)(\.md)?(?=\||\])\]\]", existing_links_section
)
existing_links = set([link[0] + link[1] for link in existing_links])

# Only append new unique links
links = [link for link in links if link not in existing_links]
if len(links) == 0:
    sys.exit()
# Split the list of links into sublists, each with up to 10 links
split_links = [links[i : i + 10] for i in range(0, len(links), 10)]

# Format the links with spaces and up to 10 per line
formatted_links = "\n".join(
    [" ".join(f"[[{link}]]" for link in sublist) for sublist in split_links]
)

# Insert the links into the target file
if insert_position != -1:
    updated_target_content = (
        target_content[:insert_position]
        + "\n"
        + formatted_links
        + target_content[insert_position:]
    )
else:
    updated_target_content = target_content + "\n" + formatted_links

# Write the updated content back to the target file
with open(
    os.path.join(obsidian_vault_path, target_file), "w", encoding="utf-8"
) as file:
    file.write(updated_target_content)