sourceDisk = Storage::disk('source'); $this->filename = basename($this->path); $this->fileExtension = (str_ends_with($this->filename, '.blade.php')) ? 'blade.php' : pathinfo($this->filename, PATHINFO_EXTENSION); $this->fileExtension = str($this->fileExtension)->trim('.')->toString(); $this->title = str($this->filename)->remove("." . $this->fileExtension)->title()->toString(); $this->slug = str($this->title)->slug(); $this->isIndex = str($this->filename)->remove("." . $this->fileExtension)->toString() == "index"; $this->directory = dirname($this->path); $this->key = str($this->path)->remove("." . $this->fileExtension)->replace("/", ".")->lower()->toString(); $this->entityName = $this->isIndex ? basename($this->directory) : $this->slug; $this->detectCollection(); $this->fillAdditionalData(); } // TODO: Do something with the [params] // from the collection name if present. // Maybe store it in a $collectionParams // array on the page or something. public function detectCollection() { $key_parts = explode('.', $this->key); $collections = SiteConfiguration::collections(); foreach ($collections as $c) { $parts = explode('.', $c); // If we have key: projects.something // and collection projects.[project_name].docs // then it's not it if (count($parts) > $key_parts) { continue; } // Get a subset of max length key parts $key_compare = array_slice($key_parts, 0, count($parts)); $match = true; foreach ($key_compare as $i => $v) { // TODO: Use regex here to search for [] instead of just [ if ($v !== $parts[$i] && !str($parts[$i])->contains("[")) { $match = false; break; } } if ($match) $this->collection = $c; } } /** * Grab data from the file's front matter * as well as the config and overwrite * or fill in any missing attributes. * * @return void */ public function fillAdditionalData() { // If we have a collection, get the settings from it if (!empty($this->collection)) { $config = SiteConfiguration::getConfig()['collections'][$this->collection]; // if ($this->collection == "projects") { // dd($config); // } $fillIfEmpty = ['layout']; foreach ($fillIfEmpty as $field) { if (empty($this->$field) && !empty($config[$field])) { $this->$field = $config[$field]; } } } // Grab front matter data if any if ($this->fileExtension == 'md') { // Build the markdown parser $mdEnvironment = new Environment(); $mdEnvironment->addExtension(new CommonMarkCoreExtension); $mdEnvironment->addExtension(new FrontMatterExtension); $parser = new MarkdownConverter($mdEnvironment); $parsed = $parser->convert($this->content()); if ($parsed instanceof RenderedContentWithFrontMatter) { $frontMatter = $parsed->getFrontMatter(); foreach ($frontMatter as $field => $value) { $this->$field = $value; } } } // If layout is still empty just set it to main if (empty($this->layout)) $this->layout = 'main'; } public function renderer(): Renderer { return ($this->fileExtension == 'blade.php') ? new BladeRenderer : new MarkdownRenderer; } public function render(): string { return $this->renderer()->render($this); } public function outputPath(): string { $baseDirectory = $this->directory; if ($this->isIndex) { return join_paths($baseDirectory, "index.html"); } return join_paths($baseDirectory, "{$this->slug}", "index.html"); } public function content(): string { if (!empty($this->content)) { return $this->content; } $content = Storage::disk('source')->get($this->path); $this->content = $content; return $content; } }