setSafeMode($htmlSafeMode); $Parsedown->setBreaksEnabled($lineBreaks); $cleanFile = ''; // call menu again to refresh the array menu($rootDir); $path = ''; // get and parse the content, return if no content is there $content = getContent($requestFile); if ($content === '') { return; } $wordCount = str_word_count($content); $charCount = strlen($content); $content = $Parsedown->text($content); // Relative or absolute pathes if ($relPathes) { $path = $startDir; $mdpath = ''; } else { $mdpath = $path; $path = $startDir . $path; } // fix links (not used) // $oldPath = $startDir . $path; // // fix relativ links in parent folders // $pattern = array('/(\[\[)(\.\.\/.*)(\]\])/'); // $content = fixLinks($pattern, $content, $oldPath, false); // // // fix relativ links in same folders // $pattern = array('/(\[\[)+([^\/]+?)(\]\])/'); // $content = fixLinks($pattern, $content, $path, true); // // fix relativ links in subfolder and same folders // $pattern = array('/(\[\[)(?!Demo Documents\/)(.+)(\]\])/'); // $content = fixLinks($pattern, $content, $path, true); $linkFileTypes = implode('|', $allowedFileLinkTypes); $allowedImageTypes = '(\.png|\.jpg|\.jpeg|\.svg|\.gif|\.bmp|\.tif|\.tiff|\.webp)'; $src_path = $uriPath . $path; // embedded pdf links $replaces = ''; $pattern = array('/(\!\[\[)(.*?.(?:pdf))(\]\])/'); $content = preg_replace($pattern, $replaces, $content); // embedded mp4 links $replaces = ' '; $pattern = array('/(\!\[\[)(.*?.(?:mp4))(\]\])/'); $content = preg_replace($pattern, $replaces, $content); // embedded m4a links $replaces = ' '; $pattern = array('/(\!\[\[)(.*?.(?:m4a))(\]\])/'); $content = preg_replace($pattern, $replaces, $content); // links to other files with Alias $replaces = '\\3'; $pattern = array('/(\[\[)(.*?.(?:' . $linkFileTypes . '))\|(.*)(\]\])/'); $content = preg_replace($pattern, $replaces, $content); // links to other files without Alias $replaces = '\\2'; $pattern = array('/(\[\[)(.*?.(?:' . $linkFileTypes . '))(\]\])/'); $content = preg_replace($pattern, $replaces, $content); // img links with external target link $replaces = 'noreferrer">image not found'; $pattern = array('/noreferrer">(\!?\[\[)(.*?)' . $allowedImageTypes . '\|?(\d*)x?(\d*)(\]\])/'); $content = preg_replace($pattern, $replaces, $content); // img links with size $replaces = '

image not found

'; $pattern = array('/(\!?\[\[)(.*?)' . $allowedImageTypes . '\|?(\d*)x?(\d*)(\]\])/'); $content = preg_replace($pattern, $replaces, $content); // centerise or right align images with "center"/"right" directive $pattern = '/(\!?\[\[)(.*?)' . $allowedImageTypes . '\|?(center|right)\|?(\d*)x?(\d*)(\]\])/'; $replaces = function ($matches) use ($src_path) { $class = "images"; // Default class for all images if (strpos($matches[4], 'center') !== false) { $class .= " center"; // Add 'center' class } elseif (strpos($matches[4], 'right') !== false) { $class .= " right"; // Add 'right' class } $width = $matches[5] ?? 'auto'; $height = $matches[6] ?? 'auto'; return '

'; }; $content = preg_replace_callback($pattern, $replaces, $content); // img links with captions and size $replaces = '

\\4

'; $pattern = array('/(\!?\[\[)(.*?)' . $allowedImageTypes . '\|?(.+\|)\|?(\d*)x?(\d*)(\]\])/'); $content = preg_replace($pattern, $replaces, $content); // img links with captions $replaces = '

\\4

'; $pattern = array('/(\!?\[\[)(.*?)' . $allowedImageTypes . '\|?(.+|)(\]\])/'); $content = preg_replace($pattern, $replaces, $content); // handle internal site links // search for links outside of the current folder $pattern = array('/(\[\[)(?:\.\.\/)+(.*?)(\]\])/'); $content = translateLink($pattern, $content, $path, false); // search for links in the same folder $pattern = array('/(\[\[)(.*?)(\]\])/'); $content = translateLink($pattern, $content, $mdpath, true); // add some meta data $content = '
' . $cleanFile . '
' . $wordCount . '
' . $charCount . '
' . $content; echo $content; return; } // translate relativ links (not used) // function fixLinks($pattern, $content, $path, $sameFolder) { // return preg_replace_callback($pattern, // function($matches) use ($path, $sameFolder) { // $newAbPath = $path; // echo "path: " . $path; // echo "
"; // $pathSplit = explode("/",$path); // $linkFilePart = $matches[2]; // $esapeSequence = "#regex_run#"; // echo '$matches[1]: ' . $matches[1]; // echo '
'; // echo '$matches[2]: ' . $linkFilePart; // echo '
'; // $linkDesc = ""; // # handle custom link comments and sizes // $splitLink = explode("|", $matches[2]); // if (count($splitLink) > 1) { // $linkFilePart = $splitLink[0]; // array_shift($splitLink); // $linkDesc = '|' .implode("|", $splitLink); // } // // do extra stuff to get the absolute path // if ($sameFolder == false) { // $countDirs = count(explode("../",$linkFilePart)); // $countDirs = $countDirs -1; // $newPath = array_splice($pathSplit, 1, -$countDirs); // $newAbPath = implode('/', $newPath); // echo "new file path: " . $newAbPath; // echo "
"; // echo "old file path: " . $linkFilePart; // echo "
"; // } // if (substr($newAbPath,0,1) == '/') { // $newAbPath = substr($newAbPath,1); // } // $origPath = explode('/', $linkFilePart); // array_pop($origPath); // $origPath = implode('/', $origPath); // //check if its already an absolut path // echo "new file path: " . $newAbPath; // echo "
"; // echo "old file path: " . $origPath; // echo "
"; // if (count_chars($origPath) >= count_chars($newAbPath)) { // $urlPath = $linkFilePart; // } else { // $linkFile = str_replace("../","",$linkFilePart); // $urlPath = $newAbPath. '/'. $linkFile; // } // return '[['.$urlPath.$linkDesc.']]'; // } // ,$content); // } //internal links // can be simplified (no need of path translation) function translateLink($pattern, $content, $path, $sameFolder) { return preg_replace_callback( $pattern, function ($matches) use ($path, $sameFolder) { global $uriPath; $newAbPath = $path; $pathSplit = explode("/", $path); $linkName_full = $matches[2]; $linkName = $linkName_full; $linkFile = $matches[2]; # handle custom internal obsidian links $splitLink = explode("|", $matches[2]); if (count($splitLink) > 1) { $linkFile = $splitLink[0]; $linkName = $splitLink[1]; } # handle internal popups $popupClass = ''; $popUpIcon = ''; if (count($splitLink) > 2) { $popupClass = ' internal-popup'; $popUpIcon = ''; } // do extra stuff to get the absolute path if ($sameFolder == false) { $countDirs = count(explode("../", $matches[0])); $countDirs = $countDirs - 1; $newPath = array_splice($pathSplit, 1, -$countDirs); $newAbPath = implode('/', $newPath); } $urlPath = $newAbPath . '/' . $linkFile; if (substr($urlPath, 0, 1) == '/') { #$urlPath = '/' . $urlPath; $urlPath = substr($urlPath, 1); } $refName = ''; # if same document heading reference if (substr($linkName_full, 0, 1) == '#') { $splitLink = explode("#", $urlPath); $urlPath = ''; $refName = $splitLink[1]; $refName = '#' . $refName; $href = 'href="'; } else { #$href = 'href="?link='; $href = 'href="' . $uriPath; } $urlPath = str_replace('&', '&', $urlPath); #$urlPath = rawurlencode($urlPath); $urlPath = str_replace('%23', '#', $urlPath); $urlPath = str_replace('~', '%80', $urlPath); $urlPath = str_replace('-', '~', $urlPath); $urlPath = str_replace(' ', '-', $urlPath); return '' . $linkName . '' . $popUpIcon; } , $content ); } // read content from file function getContent($requestFile) { global $avFiles; global $path; global $cleanFile; global $rootDir; $content = ''; // check if file is in array if (in_array($requestFile, $avFiles, true)) { $cleanFile = $requestFile; $n = strrpos($requestFile, "/"); $path = substr($requestFile, 0, $n); $content .= file_get_contents($rootDir . $requestFile . '.md', true); } return $content; } ?>