From 2110ca696622f6ff8648b2628fb0b27fb34e1067 Mon Sep 17 00:00:00 2001 From: Josh Bruce Date: Tue, 2 Nov 2021 00:38:18 +0000 Subject: [PATCH 1/2] origianl content --- composer.lock | 12 ++--- public/index.php | 2 +- src/PageComponents/DateBlock.php | 54 ++++++++++++-------- src/PageComponents/OriginalContentNotice.php | 42 +++++++++++++++ src/Pages/DefaultTemplate.php | 24 ++++++--- 5 files changed, 99 insertions(+), 35 deletions(-) create mode 100644 src/PageComponents/OriginalContentNotice.php diff --git a/composer.lock b/composer.lock index fc10e845..000fc15c 100644 --- a/composer.lock +++ b/composer.lock @@ -62,16 +62,16 @@ }, { "name": "8fold/commonmark-fluent-markdown", - "version": "0.10.0", + "version": "0.10.1", "source": { "type": "git", "url": "https://github.com/8fold/commonmark-fluent-markdown.git", - "reference": "cdd3b7e5064c9bd8d05ceec3d26303e6f8de6eab" + "reference": "fc0bb6c241da6d333a95edf51453824d327a90ce" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/8fold/commonmark-fluent-markdown/zipball/cdd3b7e5064c9bd8d05ceec3d26303e6f8de6eab", - "reference": "cdd3b7e5064c9bd8d05ceec3d26303e6f8de6eab", + "url": "https://api.github.com/repos/8fold/commonmark-fluent-markdown/zipball/fc0bb6c241da6d333a95edf51453824d327a90ce", + "reference": "fc0bb6c241da6d333a95edf51453824d327a90ce", "shasum": "" }, "require": { @@ -105,7 +105,7 @@ "description": "A fluent API for CommonMark by the PHP League", "support": { "issues": "https://github.com/8fold/commonmark-fluent-markdown/issues", - "source": "https://github.com/8fold/commonmark-fluent-markdown/tree/0.10.0" + "source": "https://github.com/8fold/commonmark-fluent-markdown/tree/0.10.1" }, "funding": [ { @@ -117,7 +117,7 @@ "type": "github" } ], - "time": "2021-10-24T00:16:28+00:00" + "time": "2021-11-02T00:21:47+00:00" }, { "name": "8fold/php-html-builder", diff --git a/public/index.php b/public/index.php index a7f39dda..21cb1284 100644 --- a/public/index.php +++ b/public/index.php @@ -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 diff --git a/src/PageComponents/DateBlock.php b/src/PageComponents/DateBlock.php index 2dbe7106..55726cc4 100644 --- a/src/PageComponents/DateBlock.php +++ b/src/PageComponents/DateBlock.php @@ -15,36 +15,46 @@ 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 = ''; + 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 ''; } } diff --git a/src/PageComponents/OriginalContentNotice.php b/src/PageComponents/OriginalContentNotice.php new file mode 100644 index 00000000..3d1f8e51 --- /dev/null +++ b/src/PageComponents/OriginalContentNotice.php @@ -0,0 +1,42 @@ + $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 ''; + } +} diff --git a/src/Pages/DefaultTemplate.php b/src/Pages/DefaultTemplate.php index 9c8ad3e0..2d1b7dd1 100644 --- a/src/Pages/DefaultTemplate.php +++ b/src/Pages/DefaultTemplate.php @@ -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 { @@ -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' => '#' @@ -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; From 7b75a9c6d98ffb823a94824fbefe8ca8b356106e Mon Sep 17 00:00:00 2001 From: Josh Bruce Date: Tue, 2 Nov 2021 00:43:25 +0000 Subject: [PATCH 2/2] fix prod script failures --- src/PageComponents/DateBlock.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/PageComponents/DateBlock.php b/src/PageComponents/DateBlock.php index 55726cc4..a5f8fdd9 100644 --- a/src/PageComponents/DateBlock.php +++ b/src/PageComponents/DateBlock.php @@ -38,6 +38,9 @@ public static function create(array $frontMatter): string ->props('is dateblock')->build(); } + /** + * @param array $frontMatter + */ private static function timestamp( array $frontMatter, string $key,