ghwikipp: format: live previews no longer mangle relative URLs.

From daa281a01016435dd238dbf41ea6d76928ef800e Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Fri, 10 May 2024 02:13:21 +0000
Subject: [PATCH] format: live previews no longer mangle relative URLs.

Fixes #54.
---
 index.php | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/index.php b/index.php
index f5e301b..5099cc0 100644
--- a/index.php
+++ b/index.php
@@ -319,6 +319,20 @@ function recook_search_index()
 }
 
 
+// Convert any relative links in a wiki page to full links. You only want to do this for
+// throwaway cooks for preview content while editing, since that's running off a subdir
+// of where the edited page would normally be, which messes up links.
+function fixup_preview_links($page, $data)
+{
+    global $base_url;
+
+    $stripped_page = dirname($page);  // this just happens to work.
+    $pattern = '/(\<a href=")((?![a-z]*\:\/\/|\/)(.*?))("\>)/i';
+    $replacement = '$1' . "$base_url/$stripped_page/" . '$3$4';
+    return preg_replace($pattern, $replacement, $data);
+}
+
+
 // Stole some of this from https://github.com/dintel/php-github-webhook/blob/master/src/Handler.php
 function validate_webhook_signature($gitHubSignatureHeader, $payload)
 {
@@ -1198,9 +1212,12 @@ function build_index($base, $dname, &$output)
         fail400('Unsupported document format');
     }
 
-    $data = str_replace("\r\n", "\n", $data);
-    print($data == '' ? $data : cook_string($data, $pandoc_format));
-
+    if ($data != '') {
+        $data = str_replace("\r\n", "\n", $data);
+        $data = cook_string($data, $pandoc_format);
+        $data = fixup_preview_links($document, $data);
+        print($data);
+    }
 } else if ($operation == 'history') {
     foreach ($supported_formats as $ext => $format) {
         if (file_exists("$raw_data/$document.$ext")) {