'; $customSection = $customSection . '
' . $siteDescription . '
'; $customSection = $customSection . '
'; } $about = '.about'; // add about and index to allowed files $aboutpath = getFileInfos($rootDir . '/' . $about)[0]; $indexpath = getFileInfos($rootDir . '/' . $index)[0]; $aboutpath = '/' . $aboutpath; $indexpath = '/' . $indexpath; array_push($avFiles, $aboutpath); array_push($avFiles, $indexpath); // hide folders if (strcmp($hideFolders, '')) { $hideFolders = explode(',', $hideFolders); } else { $hideFolders = array(); } // path management if (!strcmp($rootDir, "")) { $rootDir = getcwd(); $vaultName = mb_basename($rootDir); $startDir = ""; } else { $startDir = $rootDir; } // custom sort function to prefer underscore function cmp($a, $b) { $aTemp = str_replace('_', '0', $a); $bTemp = str_replace('_', '0', $b); return strnatcasecmp($aTemp, $bTemp); } function menu($dir, $folder = '') { global $hiddenFileAccess; global $avFiles; $html = ''; // get all files from current dir $files = glob($dir . '/*'); // sort array usort($files, "cmp"); // iterate the folders foreach ($files as $file) { if (is_dir($file)) { // check if we want to hide the folder if (isValidFolder($file)) { // split Folder Infos $folder = getFolderInfos($file)[0]; $folderClean = getFolderInfos($file)[1]; $folderName = getFolderInfos($file)[2]; $folderId = str_replace(' ', '_', $folderClean); $folderId = preg_replace('/[^A-Za-z\-]/', '_', $folderId); $folderId = '_' . $folderId; $html .= ' '; } else if($hiddenFileAccess) { // dont list the folder but add the file to the array menu($file); } } } // iterate the files foreach ($files as $file) { if (isMDFile($file)) { $path = getFileInfos($file)[0]; $mdFile = getFileInfos($file)[1]; $path = '/' . $path; // push the the path to the array array_push($avFiles, $path); // URL Encode the Path for the JS call $pathClean = rawurlencode($path); $pathID = str_replace(' ', '_', $path); $pathID = preg_replace('/[^A-Za-z0-9\-]/', '_', $path); $html .= ' '; } } return $html; } function doSearch($dir, $searchfor) { // $Parsedown = new Parsedown(); // $Parsedown->setSafeMode(false); //$cleanSearch = htmlspecialchars($searchfor, ENT_QUOTES); $result = search($dir, $searchfor); $content = $result; //$content = $Parsedown->text($result); if ($content === '') { $content = '
No matches found.
'; } return $content; } function search($dir, $searchfor, $folder = '') { $files = glob($dir . '/*'); $result = ''; $matches = []; foreach ($files as $file) { // in case of folder if (is_dir($file)) { if (isValidFolder($file)) { $folder = getFolderInfos($file)[0]; $result .= search($file, $searchfor, $folder . '/'); } } else { if (isMDFile($file)) { $pathClean = getFileInfos($file)[0]; $urlPathClean = rawurlencode($pathClean); // get the file contents, assuming the file to be readable (and exist) $contents = file_get_contents($file); $contents = $contents . $pathClean; // escape special characters in the query $pattern = preg_quote($searchfor, '/'); // check if we search for an tag, if yes first parse the document to get the front matter tags if (substr($searchfor, 0, 1) === '#') { $Parsedown = new PerliteParsedown(); $Parsedown->setSafeMode(true); $contents = $Parsedown->text($contents); $contents = strip_tags($contents); } // finalise the regular expression, matching the whole line $pattern = "/^.*$pattern.*\$/mi"; // search, and store all matching occurences in $matches if (preg_match_all($pattern, $contents, $matches)) { $result .= '
'; } } } } return $result; } // check if file is a md file function isMDFile($file) { $fileinfo = pathinfo($file); if (isset($fileinfo['extension']) and strtolower($fileinfo['extension']) == 'md') { return true; } return false; } function getFileInfos($file) { global $rootDir; $mdFile = mb_basename($file); if (strcmp(substr($mdFile, -3), ".md") === 0) { $mdFile = substr($mdFile, 0, -3); } $folderClean = str_replace('$' . $rootDir, '', '$' . pathinfo($file)["dirname"]); $folderClean = substr($folderClean, 1); if (!strcmp($folderClean, '')) { $pathClean = $mdFile; } else { $pathClean = $folderClean . '/' . $mdFile; } return [$pathClean, $mdFile]; } function mb_basename($path) { if (preg_match('@^.*[\\\\/]([^\\\\/]+)$@s', $path, $matches)) { return $matches[1]; } else if (preg_match('@^([^\\\\/]+)$@s', $path, $matches)) { return $matches[1]; } return ''; } function getFolderInfos($file) { global $rootDir; $folder = str_replace($rootDir . '/', '', $file); $folderClean = str_replace('/', '-', $folder); $folderClean = str_replace(' ', '-', $folderClean); $folderName = mb_basename($file); return [$folder, $folderClean, $folderName]; } function isValidFolder($file) { global $hideFolders; $folderName = mb_basename($file); // check if folder is in array if (in_array($folderName, $hideFolders, true)) { return false; } if (strcmp(substr($folderName, 0, 1), '.') !== 0) { return true; } return false; } function isCached($jsonMetadaFile, $metadaTempFileSum) { if (is_file($metadaTempFileSum)) { $md5_envsum = file_get_contents($metadaTempFileSum); $md5_filesum = md5_file($jsonMetadaFile); if ($md5_envsum === $md5_filesum) { return true; } } return false; } function getfullGraph($rootDir) { global $tempPath; global $vaultName; $jsonMetadaFile = $rootDir . '/metadata.json'; $metadaTempFile = $tempPath . '/metadata_' . $vaultName . '.temp'; $metadaTempFileSum = $tempPath . '/metadata_' . $vaultName . '.md5'; if (!is_file($jsonMetadaFile)) { return; } // check if metadata file has changed if (is_file($metadaTempFile) and isCached($jsonMetadaFile, $metadaTempFileSum)) { return file_get_contents($metadaTempFile); } // metadata has changed / was not cached $jsonData = file_get_contents($jsonMetadaFile); if ($jsonData === false) { return; } $json_obj = json_decode($jsonData, true); if ($json_obj === null) { return; } $graphNodes = array(); $graphEdges = array(); $currentNode = -1; $nodeID = 0; // create nodes foreach ($json_obj as $id => $node) { $nodePath = removeExtension($node['relativePath']); // check if node from the json file really exists if (checkArray($nodePath)) { $thisNodeID = $nodeID; // does not get overwritten when a tag node gets created // add node to the graph array_push($graphNodes, ['id' => $nodeID, 'label' => $node['fileName'], 'title' => $nodePath]); $nodeID += 1; // create tag nodes if they don't already exist if (isset($node["tags"])) { foreach ($node["tags"] as $tag) { $tag = "#" . $tag; $tagID = -1; $tagExists = false; foreach ($graphNodes as $graphNode) { if ($graphNode["label"] == $tag) { $tagID = $graphNode["id"]; $tagExists = true; } } if (!$tagExists) { $tagID = $nodeID; $tag = ["id" => $nodeID, "label" => $tag, "title" => $tag, "group" => "tag"]; array_push($graphNodes, $tag); $nodeID += 1; } array_push($graphEdges, ["from" => $thisNodeID, "to" => $tagID]); } } } } $targetId = -1; $sourceId = -1; // create links foreach ($json_obj as $index => $node) { $nodePath = removeExtension($node['relativePath']); // check if node from the json file really exists if (checkArray($nodePath)) { // create the linking between the nodes if (isset($node['links'])) { foreach ($node['links'] as $i => $links) { $source = ""; $target = ""; if (isset($node['relativePath'])) { $tempPath = removeExtension($node['relativePath']); if (checkArray($tempPath)) { $source = $tempPath; $tempPath = null; } } if (isset($links['relativePath'])) { $tempPath = removeExtension($links['relativePath']); if (checkArray($tempPath)) { $target = $tempPath; $tempPath = null; } } if ($source !== '' && $target !== '') { foreach ($graphNodes as $index => $element) { $elementTitle = $element['title']; if (strcmp($elementTitle, $target) == 0) { $targetId = $element['id']; } if (strcmp($elementTitle, $source) == 0) { $sourceId = $element['id']; } $edgeExists = false; foreach ($graphEdges as $edge) { if ($edge['from'] === $sourceId && $edge['to'] === $targetId) { $edgeExists = true; break; } if ($edge['to'] === $sourceId && $edge['from'] === $targetId) { $edgeExists = true; break; } } if ($targetId !== -1 && $sourceId !== -1) { if (!$edgeExists) { array_push($graphEdges, ['from' => $sourceId, 'to' => $targetId]); } $targetId = -1; $sourceId = -1; } } } } } } } foreach ($graphEdges as $graphEdge) { foreach ($graphNodes as &$graphNode) { if ($graphEdge["from"] == $graphNode["id"] or $graphEdge["to"] == $graphNode["id"]) { $nodeValue = 0; if (isset($graphNode["value"])) { $nodeValue = $graphNode["value"]; } $nodeValue += 1; $graphNode["value"] = $nodeValue; } } } $myGraphNodes = json_encode($graphNodes, JSON_UNESCAPED_SLASHES); $myGraphEdges = json_encode($graphEdges, JSON_UNESCAPED_SLASHES); // write tempfile and store sum $metadaTempFile_handler = fopen($metadaTempFile, "w") or die("Unable to open file!"); $graphHTML = ''; fwrite($metadaTempFile_handler, $graphHTML); fclose($metadaTempFile_handler); $metadaTempFile_handler = fopen($metadaTempFileSum, "w") or die("Unable to open file!"); $md5_filesum = md5_file($jsonMetadaFile); fwrite($metadaTempFile_handler, $md5_filesum); fclose($metadaTempFile_handler); return $graphHTML; } function removeExtension($path) { return substr($path, 0, -3); } // check if node is in array function checkArray($requestNode) { global $avFiles; $requestNode = '/' . $requestNode; if (in_array($requestNode, $avFiles, true)) { return true; } return false; } function loadSettings($rootDir) { global $disablePopHovers; global $showTOC; global $showLocalGraph; global $index; global $siteTitle; global $siteType; global $siteImage; global $siteURL; global $siteDescription; global $siteName; global $siteTwitter; global $uriPath; // get themes $themes = ""; $folders = glob($rootDir . '/.obsidian/themes/*'); $appearanceFile = $rootDir . '/.obsidian/appearance.json'; $defaultTheme = ""; if (is_file($appearanceFile)) { $jsonData = file_get_contents($appearanceFile); if ($jsonData) { $json_obj = json_decode($jsonData, true); if ($json_obj) { // if theme is set, set it as default if (array_key_exists('cssTheme', $json_obj)) { $defaultTheme = $json_obj["cssTheme"]; } } } } // iterate the folders foreach ($folders as $folder) { if (is_dir($folder)) { $folderName = getFolderInfos($folder)[2]; $folderClean = str_replace(' ', '_', $folderName); $themePath = $uriPath . $rootDir . '/.obsidian/themes/' . $folderName . '/theme.css'; if ($defaultTheme === $folderName) { $themes .= ''; } else { $themes .= ''; } } } // Meta Tags $defaultSettings = ' '; // default settings $defaultSettings .= ''; $defaultSettings .= ''; $defaultSettings .= ''; $defaultSettings .= ''; return $themes . $defaultSettings; }