Skip to content

Commit

Permalink
Merge pull request #31 from 8fold/working
Browse files Browse the repository at this point in the history
Original content
  • Loading branch information
joshbruce authored Nov 2, 2021
2 parents 8fd2707 + 7b75a9c commit 770e5b9
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 35 deletions.
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Regardless of what happens next, we'll need a baseline markdown converter.
*/
$markdownConverter = Eightfold\Markdown\Markdown::create()
->minified()
->minified() // can't be minified due to code blocks
->smartPunctuation();

// Inject environment variables to global $_SERVER array
Expand Down
57 changes: 35 additions & 22 deletions src/PageComponents/DateBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,49 @@ class DateBlock
*/
public static function create(array $frontMatter): string
{
$updated = '';
if (
array_key_exists('updated', $frontMatter) and
$carbon = Carbon::createFromFormat('Ymd', $frontMatter['updated'])
) {
$time = HtmlElement::time($carbon->toFormattedDateString())
->props(
'property dateModified',
'content ' . $carbon->format('Y-m-d')
)->build();
$updated = HtmlElement::p("Updated on: {$time}");
$created = self::timestamp(
$frontMatter,
'created',
'Created on',
'dateCreated'
);

$moved = self::timestamp($frontMatter, 'moved', 'Moved on');

$updated = self::timestamp(
$frontMatter,
'updated',
'Updated on',
'dateModified'
);

if (empty($updated) and empty($moved) and empty($created)) {
return '';
}
return HtmlElement::div($created, $moved, $updated)
->props('is dateblock')->build();
}

$created = '';
/**
* @param array<string, mixed> $frontMatter
*/
private static function timestamp(
array $frontMatter,
string $key,
string $label,
string $schemaProp = ''
): HtmlElement|string {
if (
array_key_exists('created', $frontMatter) and
$carbon = Carbon::createFromFormat('Ymd', $frontMatter['created'])
array_key_exists($key, $frontMatter) and
$carbon = Carbon::createFromFormat('Ymd', $frontMatter[$key])
) {
$time = HtmlElement::time($carbon->toFormattedDateString())
->props(
'property dateCreated',
(strlen($schemaProp) > 0) ? "property {$schemaProp}" : '',
'content ' . $carbon->format('Y-m-d')
)->build();
$created = HtmlElement::p("Created on: {$time}");
return HtmlElement::p("{$label}: {$time}");
}

if (empty($updated) and empty($created)) {
return '';
}
return HtmlElement::div($created, $updated)
->props('is dateblock')->build();
return '';
}
}
42 changes: 42 additions & 0 deletions src/PageComponents/OriginalContentNotice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace JoshBruce\Site\PageComponents;

use Eightfold\Markdown\Markdown;

use Eightfold\HTMLBuilder\Element as HtmlElement;

use JoshBruce\Site\FileSystem;
use JoshBruce\Site\Content;

class OriginalContentNotice
{
/**
* @param array<string, mixed> $frontMatter
*/
public static function create(
array $frontMatter,
Markdown $markdownConverter,
FileSystem $fileSystem
): string {
$file = $fileSystem->with('/messages', 'original.md');
if (
array_key_exists('original', $frontMatter) and
$file->found() and
$markdown = Content::init($file)->markdown()
) {
list($link, $platform) = explode(' ', $frontMatter['original'], 2);
$originalLink = "[{$platform}]({$link})";
$markdown = str_replace(
'{{platform link}}',
$originalLink,
$markdown
);

return $markdown;
}
return '';
}
}
24 changes: 18 additions & 6 deletions src/Pages/DefaultTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@

use JoshBruce\Site\FileSystem;
use JoshBruce\Site\Content;
use JoshBruce\Site\PageComponents\Navigation;
use JoshBruce\Site\PageComponents\Data;
use JoshBruce\Site\PageComponents\DateBlock;
use JoshBruce\Site\PageComponents\Heading;
use JoshBruce\Site\PageComponents\LogList;
use JoshBruce\Site\PageComponents\Footer;
use JoshBruce\Site\PageComponents\HeadElements;
use JoshBruce\Site\PageComponents\Data;
use JoshBruce\Site\PageComponents\Heading;
use JoshBruce\Site\PageComponents\LogList;
use JoshBruce\Site\PageComponents\Navigation;
use JoshBruce\Site\PageComponents\OriginalContentNotice;

class DefaultTemplate
{
Expand All @@ -42,9 +43,10 @@ public function __construct(
) {
$this->markdownConverter = $markdownConverter
->withConfig(['html_input' => 'allow'])
->externalLinks()
->abbreviations()
->headingPermalinks(
->externalLinks([
'open_in_new_window' => true
])->headingPermalinks(
[
'min_heading_level' => 2,
'symbol' => ''
Expand All @@ -67,9 +69,19 @@ public function body(): string
$body = $this->markdown();
$body = Data::create(frontMatter: $this->frontMatter()) .
"\n\n" . $body;

$originalLink = OriginalContentNotice::create(
frontMatter: $this->frontMatter(),
fileSystem: $this->file,
markdownConverter: $this->markdownConverter
);
$body = $originalLink . "\n\n" . $body;

$body = DateBlock::create(frontMatter: $this->frontMatter()) .
"\n\n" . $body;



if ($this->file->isNotRoot()) {
$body = Heading::create(frontMatter: $this->frontMatter()) .
"\n\n" . $body;
Expand Down

0 comments on commit 770e5b9

Please sign in to comment.