@@ -54,3 +54,70 @@ def _process_block_and_children(
54
54
if hasattr (block , "children" ) and block .children :
55
55
for child in block .children :
56
56
_process_block_and_children (child , block_map )
57
+
58
+
59
+ def replace_urls (
60
+ input_obj : Page | BlockBase | list [BlockBase ],
61
+ url_replace_map : dict [str , str ],
62
+ ) -> None :
63
+ """
64
+ Recursively processes all blocks in the input object and replaces any file or image URLs
65
+ that are found in the url_replace_map with their corresponding values.
66
+
67
+ Args:
68
+ input_obj: Can be either a Page object, a single Block object, or a list of Block objects
69
+ url_replace_map: A dictionary mapping original URLs to their replacement URLs
70
+ """
71
+ # Extract all blocks
72
+ blocks = extract_blocks (input_obj )
73
+
74
+ # Process each block
75
+ for block_id , block in blocks .items ():
76
+ _replace_url_in_block (block , url_replace_map )
77
+
78
+
79
+ def _replace_url_in_container (container , container_type , url_replace_map ):
80
+ """
81
+ Helper function to replace URL in a file or external container.
82
+
83
+ Args:
84
+ container: The container object that might have URL information
85
+ container_type: The type of container ('file' or 'external')
86
+ url_replace_map: Dictionary mapping original URLs to replacement URLs
87
+ """
88
+ if not hasattr (container , container_type ):
89
+ return
90
+
91
+ url_container = getattr (container , container_type )
92
+ if hasattr (url_container , "url" ):
93
+ current_url = getattr (url_container , "url" )
94
+ if current_url in url_replace_map :
95
+ setattr (url_container , "url" , url_replace_map [current_url ])
96
+
97
+
98
+ def _replace_url_in_block (block : BlockBase , url_replace_map : dict [str , str ]) -> None :
99
+ """
100
+ Helper function to replace URLs in a specific block based on the url_replace_map.
101
+
102
+ Args:
103
+ block: The block to process
104
+ url_replace_map: A dictionary mapping original URLs to their replacement URLs
105
+ """
106
+ if not hasattr (block , "type" ):
107
+ return
108
+
109
+ block_type = getattr (block , "type" )
110
+
111
+ # Check for image block
112
+ if block_type == "image" and hasattr (block , "image" ):
113
+ image = getattr (block , "image" )
114
+ if hasattr (image , "type" ):
115
+ image_type = getattr (image , "type" )
116
+ _replace_url_in_container (image , image_type , url_replace_map )
117
+
118
+ # Check for file block
119
+ elif block_type == "file" and hasattr (block , "file" ):
120
+ file_block = getattr (block , "file" )
121
+ if hasattr (file_block , "type" ):
122
+ file_type = getattr (file_block , "type" )
123
+ _replace_url_in_container (file_block , file_type , url_replace_map )
0 commit comments