ACF Library

MoveImages

Back to List

Description: Move images in Markdown document to images folder and rewrite image urls

FileMaker Prototype:

Set Variable [$res; ACF_Run("MoveImages"; string_SourceFile;  string_html_root;  string_relpath)]

Category: WEB

Function source:

function MoveImages ( string SourceFile, string html_root, string relpath ) 
   int i, cnt = 0; 
   print "MoveImages  enters...."; 
   int x = open ( SourceFile, "r"  ); 
   string md = read (x ) ; 
   string ImageList;
   close ( x ) ; 
   
   print "Pre prosessing"; 
   // string regex = "!\[[^\]]*\]\(((.+?\/)?([^\/)]+))\)"; // used to extract image tags...
   // changed to process urls with file:// in it. 
   string regex = "!\[[^\]]*\]\(\s*(?:file:\/\/)?((.+?\/)?([^\/)]+))\s*\)"; // used to extract image tags...
   // gr 0 = full match, gr 1 = full path, gr 2 = directory path, gr 3 = file path. 
   array string tags = regex_extract ( regex, md ) ; 
   int no = sizeof ( tags ) ; 
   if ( no == 0 ) then
      print "Pre prosessing - no tags"; 
      return ""; 
   end if
   
   string docFolder = regex_replace ( "^(.+\/)[^\/]+$", SourceFile, "\1" ) ; 
   
   string fulltag, fullpath, fileFullPath, dir, file, newPath, newRelPath, newtag, res; 
   for ( i= 1, no)
      print "\n" + tags[i]; 
      fulltag = getValue ( tags[i], 1);
      fullpath = getValue ( tags[i], 2);
      fileFullPath = fullpath; 
      dir = getValue ( tags[i], 3);
      file = getValue ( tags[i], 4);
      if ( file_exists ( fullpath ) == 0 ) then
         fullpath = docFolder + fullpath; 
      end if
      if ( file_exists ( fullpath ) ) then
         newRelPath = relpath + "images/" + file; 
         newPath = html_root + "/images/" + file; 
         if ( newRelPath != fullpath) then // Allready processed image?
            if ( file_exists ( newPath ) ) then
               // res = delete_file ( newpath );
            else
               if ( file_exists ( newPath ) == 0) then
                  res = copy_file ( fullpath, newPath ) ; 
                  if ( res == "OK") then
                     newtag = substitute ( fulltag, fileFullPath, newRelPath ) ; 
                     md = substitute ( md, fulltag, newtag ) ; 
                     ImageList += newPath + "\r\n"; 
                     cnt++; 
                  end if
               end if
            end if
         end if
      end if
   end for
   if ( cnt > 0 ) then
      // save the modified MarkDown document. Take a backup first, in case we did something nasty...
      res = copy_file ( SourceFile, SourceFile+".bak" ) ; 
      // Write back to file. 
      x = open ( SourceFile, "w"  ); 
      write ( x, md ) ; 
      close ( x ) ;
      
   end if
   $$ImageList = ImageList; // for copying to the WEB. 
   return "OK"; 
END

The MoveImages function automates the process of moving images linked in Markdown documents to a designated images folder and updating the Markdown image tags with a new relative path. This eliminates the need for manual image relocation and link updates.

Use Case

When working with Markdown documents intended for reference in a FileMaker database, users often add images by dragging them directly into the document within a Markdown editor. However, these images need to be moved to a specific Images folder, and the image links must be adjusted to point to this folder using relative paths—making the document web-ready. This function automates the process, saving time and reducing manual work.

Parameters

  • Source Path: Path to the Markdown source file.

    • Example: /Users/ole/Documents/MarkdownDocs/MyDocument.md
  • HTML Root Path: Path to the HTML root, representing the web mirror folder where the document will eventually reside.

    • Example: /Users/ole/Documents/HTML-root/
  • Relative Path Prefix: Relative path prefix for the image links, ensuring they are correctly linked for the web.

    • Example: ../HTML-root/

Example Usage

Set Variable [$res; ACF_Run("MoveImages"; 
        "/Users/ole/Documents/MarkdownDocs/MyDocument.md";  
        "/Users/ole/Documents/HTML-root/";  "../HTML-root/")]

How It Works

The function scans the document for image tags formatted like this:

![name](/Users/ole/Desktop/MyImage.png)

When images are added from external locations (e.g., the Desktop) into the Markdown editor, they may become inaccessible when the document is moved to the website. To resolve this, the function:

  1. Locates all image tags in the document.

  2. Copies each image to the images folder within the specified HTML_root directory.

  3. Updates each tag, transforming it to:

     ![name](../HTML-root/images/MyImage.png)
  4. Repeats this process for all image tags, saving the updated document.

When we later converts the Markdown document to HTML, we strip away this ../HTML-root so that the link now work for the new location of the HTML document produced.

Back to List