Browse Source

Fix handling of paths for README files that are not at the root, fixes #936

Jordi Boggiano 6 years ago
parent
commit
1a92c8b082
1 changed files with 11 additions and 2 deletions
  1. 11 2
      src/Packagist/WebBundle/Package/Updater.php

+ 11 - 2
src/Packagist/WebBundle/Package/Updater.php

@@ -615,6 +615,15 @@ class Updater
             '*.class'
         );
 
+        // detect base path if the github readme is located in a subfolder like docs/README.md
+        $basePath = '';
+        if ($isGithub && preg_match('{^<div id="readme" [^>]+?data-path="([^"]+)"}', $readme, $match) && false !== strpos($match[1], '/')) {
+            $basePath = dirname($match[1]);
+        }
+        if ($basePath) {
+            $basePath .= '/';
+        }
+
         $config = \HTMLPurifier_Config::createDefault();
         $config->set('HTML.AllowedElements', implode(',', $elements));
         $config->set('HTML.AllowedAttributes', implode(',', $attributes));
@@ -637,7 +646,7 @@ class Updater
             } elseif ($isGithub && false === strpos($link->getAttribute('href'), '//')) {
                 $link->setAttribute(
                     'href',
-                    'https://github.com/'.$owner.'/'.$repo.'/blob/HEAD/'.$link->getAttribute('href')
+                    'https://github.com/'.$owner.'/'.$repo.'/blob/HEAD/'.$basePath.$link->getAttribute('href')
                 );
             }
         }
@@ -649,7 +658,7 @@ class Updater
                 if (false === strpos($img->getAttribute('src'), '//')) {
                     $img->setAttribute(
                         'src',
-                        'https://raw.github.com/'.$owner.'/'.$repo.'/HEAD/'.$img->getAttribute('src')
+                        'https://raw.github.com/'.$owner.'/'.$repo.'/HEAD/'.$basePath.$img->getAttribute('src')
                     );
                 }
             }