Content.php

<?php
/**
 * Get the content for the requested file / dir
 * @param $project a project from $provi->parse_url2()
 */

$path = $project->abs_file_path;

if (is_dir($path)
    &&file_exists($readme=$path.'/README.md')){
    $file=$readme;
    $source = file_get_contents($readme);
    $extension = 'md';
} else if (is_dir($path)){
    // echo 'dir dir dir';
    // exit;
    $extension = 'md';
    $source = $lia->view('DirListing',['project'=>$project]);
} else if (is_file($path)) {
    $file = $path;
    $source = file_get_contents($file);
    $extension = pathinfo($file,PATHINFO_EXTENSION);
    if ('.'.$extension==basename($path))$extension = 'txt';
} else {
    echo "The project or branch or file was not found.";
    return;
}

$mimetype = \Lia\FastFileRouter::get_mime_type('.'.$extension) ?? '.txt'; 
// $mimetype = \Lia\Content\RawContent::extensionMimeType($extension);
$mimeparts = explode('/',$mimetype);
if ($mimeparts[0]=='image'){
    $blob = 'data:'.$mimetype.';base64,'.base64_encode($source);
    $html = '<img src="'.$blob.'" />';
} else if ($extension=='md'){
    $converter = new \League\CommonMark\CommonMarkConverter();
    $html = $converter->convert($source);


    if (isset($actual_liaison)){
        $page_title_start = strpos($html, "<h1>");
        $page_title_end = strpos($html, "</h1>");
        $page_title = substr($html, $page_title_start+4, $page_title_end - $page_title_start - 4);

        $seo_title = trim($page_title). ' | ' . $project->project_name.':'.$project->branch;
        $actual_liaison->seoTitle($seo_title);
    }

    $base_url = $project->project_url;

    $is_docs_request = false;
    if (strpos($base_url, "-src/") === false
        && strpos($base_url, "-src:") === false) {
        $is_docs_request = true;
    } 

    if (substr($base_url,-1)=='/')$base_url = substr($base_url,0,-1);

    // link correction
    $html = preg_replace_callback("/(href=\"\/)([^\/]*)\/?([^\"]*)/",
        function($matches) use ($base_url, $is_docs_request, $project){
            // sample matches: 
            //Array
            //(
                //[0] => href="/docs/api/code/addon/Resources.php.md
                //[1] => href="/
                //[2] => docs
                //[3] => api/code/addon/Resources.php.md
            //)
            $href = substr($matches[0],7);//$matches[1] . $matches[2] . $matches[3];
            if (filter_var($href, FILTER_VALIDATE_URL) !== false){
                return "href=\"".$href;
                return $matches[0];
            }

            if ($is_docs_request && '/'.$matches[2].'/' == $project->prefix){
                // docs request AND the url points to the docs folder
                return 'href="'.str_replace('//','/',$base_url.'/'.$matches[3]);
            }
            else if ($is_docs_request) {

                // docs request and the url does NOT point to the docs folder (switch to src url)
                $pos = strrpos($base_url,':');
                if ($pos === false){ 
                    // this means we're looking at the default branch
                    $src_url = $base_url.'-src/';
                } else {
                    $src_url = substr($base_url,0,$pos).'-src'.substr($base_url,$pos);
                }

                //if (strpos($matches[3],'Resources.php') !==false){
                    //echo 'zeep';
                    //print_r($matches);
                    //var_dump($src_url);
                    //exit;
                //}
                $new_url = $src_url.'/'.$matches[2].'/'.$matches[3];
                return 'href="'.str_replace('//','/',$new_url);
            } else {
                // not a docs request, simply prepend the base url
                return 'href="'.str_replace('//','/',$base_url.'/'.$matches[2].'/'.$matches[3]);
            }
        },
        $html
    );
} else {
    $source = htmlentities($source);
    $basename = basename($path);
    $html = 
    <<<HTML
        <h2>{$basename}</h2>
        <pre><code class="language-{$extension}">{$source}</code></pre>
    HTML;
}


echo $html;