Skip to content

Commit

Permalink
Merge pull request #88 from 8fold/description-meta
Browse files Browse the repository at this point in the history
feature: Markdown can generate description
  • Loading branch information
joshbruce authored Nov 12, 2021
2 parents bd79455 + a04b6df commit f464a20
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/Content/FrontMatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ public function template(): string
}
return '';
}

public function description(): string
{
if ($this->hasMember('description')) {
return strval($this->frontMatter['description']);
}
return '';
}
}
44 changes: 44 additions & 0 deletions src/Content/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,50 @@ public function pageTitle(): string
return implode(' | ', $titles);
}

public function description(): string
{
if ($this->frontMatter()->hasMember('description')) {
$description = $this->frontMatter()->description();

} else {
$body = $this->body();
$description = preg_filter("/#(.*)\n/", '', $body);
if (is_string($description)) {
$parts = explode("\n", $description);
$parts = array_filter($parts);
$description = implode(' ', $parts);

} else {
// TODO: Doesn't guarantee meta description content.
// Log??
$description = $body;

}
}

$description = htmlentities(substr($description, 0, 200));

$parts = explode('. ', $description);
$description = '';
foreach ($parts as $part) {
$d = $part;
if (strlen($description) > 0) {
$d = $description . '. ' . $part;
}

$proposedLength = strlen($d);
if ($proposedLength >= 200) {
$ps = explode('. ', $d);
array_pop($ps);
$description = implode('. ', $ps) . '.';
break;
}
$description = $d;
}

return $description;
}

public function file(): File
{
return $this->file;
Expand Down
18 changes: 12 additions & 6 deletions src/HttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,19 @@ public function body(): string

}

$template = '';
$pageTitle = '';
$html = '';
$template = '';
$pageTitle = '';
$html = '';
$description = '';
if ($localFile->isMarkdown()) {
$markdown = Markdown::for(
file: $localFile,
in: $this->request()->fileSystem()
);
$template = $markdown->frontMatter()->template();
$pageTitle = $markdown->pageTitle();
$html = $markdown->html();
$template = $markdown->frontMatter()->template();
$pageTitle = $markdown->pageTitle();
$html = $markdown->html();
$description = $markdown->description();
}

if ($this->request()->isSitemap()) {
Expand All @@ -112,6 +114,10 @@ public function body(): string
'name viewport',
'content width=device-width,initial-scale=1'
),
Element::meta()->omitEndTag()->props(
'name description',
'content ' . $description
),
Element::link()->omitEndTag()->props(
'type image/x-icon',
'rel icon',
Expand Down
2 changes: 1 addition & 1 deletion tests/FileSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
count(
TestFileSystem::init()->publishedContentFinder()
)
)->toBeInt()->toBe(2);
)->toBeInt()->toBe(3);
})->group('filesystem');

it('has required folders', function() {
Expand Down
37 changes: 37 additions & 0 deletions tests/MarkdownTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

use JoshBruce\Site\Content\Markdown;

use JoshBruce\Site\File;
use JoshBruce\Site\Tests\TestFileSystem;

it('can get description from front matter', function() {
$fileSystem = TestFileSystem::init();
$publicRoot = $fileSystem->publicRoot();

// description field
$file = File::at($publicRoot . '/content.md', $fileSystem);
expect(
Markdown::for($file, $fileSystem)->description()
)->toBe(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce felis arcu, molestie nec imperdiet eu, tristique ut elit. Curabitur &quot;iaculis&quot; sodales turpis a pellentesque's. In ac nibh ex."
);

// derived description from content, short
$file = File::at($publicRoot . '/published-sub/content.md', $fileSystem);
expect(
Markdown::for($file, $fileSystem)->description()
)->toBe(
"Short sentence. Something a little bit longer. Third sentence."
);

// derived description from content, long
$file = File::at($publicRoot . '/published-sub/published-sub-sub/content.md', $fileSystem);
expect(
Markdown::for($file, $fileSystem)->description()
)->toBe(
"Short sentence. Something a little bit longer. Third sentence. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce felis arcu, molestie nec imperdiet eu, tristique ut elit."
);
})->group('markdown');
2 changes: 1 addition & 1 deletion tests/SitemapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
$xml->body()
)->toBe(<<<xml
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://joshbruce.com</loc></url><url><loc>https://joshbruce.com/published-sub</loc></url></urlset>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://joshbruce.com</loc></url><url><loc>https://joshbruce.com/published-sub</loc></url><url><loc>https://joshbruce.com/published-sub/published-sub-sub</loc></url></urlset>
xml
);
})->group('request', 'response', 'sitemap');
13 changes: 13 additions & 0 deletions tests/test-content/content/public/content.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
---
title: Test content root
description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce felis arcu, molestie nec imperdiet eu, tristique ut elit. Curabitur "iaculis" sodales turpis a pellentesque's. In ac nibh ex. Maecenas ornare in nisi ut commodo. Etiam consequat aliquam erat. Quisque varius mattis risus, consequat viverra eros scelerisque in. Nulla faucibus porta libero a sollicitudin. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Duis finibus placerat ante faucibus hendrerit. Phasellus condimentum nisi sed velit pretium, at feugiat quam convallis.
---

# Heading in the way

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce felis arcu, molestie nec imperdiet eu, tristique ut elit. Curabitur iaculis sodales turpis a pellentesque. In ac nibh ex. Maecenas ornare in nisi ut commodo. Etiam consequat aliquam erat. Quisque varius mattis risus, consequat viverra eros scelerisque in. Nulla faucibus porta libero a sollicitudin. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Duis finibus placerat ante faucibus hendrerit. Phasellus condimentum nisi sed velit pretium, at feugiat quam convallis.

Nunc sed felis a lacus convallis porttitor ut eu nulla. Morbi ac neque elit. Cras sit amet hendrerit purus. Nullam in nisi sem. Phasellus sodales condimentum velit, eget tempus urna scelerisque ullamcorper. Curabitur viverra velit sem, vel finibus justo porttitor ultricies. Ut arcu turpis, convallis at efficitur eget, aliquet sit amet dolor. Vestibulum ut sem nec purus imperdiet finibus. Vestibulum auctor tortor a lacinia faucibus. Nulla sagittis fringilla tincidunt. Fusce suscipit et diam et porta. Mauris felis justo, aliquam et neque eget, aliquet venenatis risus. Maecenas erat arcu, semper vitae massa sit amet, facilisis vulputate diam. Praesent lacinia pulvinar vestibulum. Nullam tristique augue magna, vel fermentum mauris malesuada nec. Aenean molestie odio sodales ante molestie pharetra.

Phasellus sit amet mi vitae turpis tristique luctus. Ut pellentesque mattis nisl, rutrum accumsan mi placerat ac. Vivamus nunc ligula, malesuada non lacinia in, accumsan id sem. Proin tempus ac lacus a pulvinar. Nunc laoreet maximus sem nec scelerisque. Maecenas sem erat, faucibus non arcu sed, accumsan vehicula sapien. Nullam ipsum lorem, bibendum non arcu convallis, fringilla interdum lacus.

Vivamus elementum elit at lectus dignissim, in commodo sem convallis. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Vivamus venenatis non ante quis ullamcorper. In facilisis ultricies tempus. Proin mollis sagittis condimentum. Vestibulum ornare pretium pellentesque. In dictum sodales felis ut ornare.

Fusce nec ullamcorper magna, ac pretium quam. Integer elementum tincidunt ligula eget tristique. Vivamus orci neque, finibus ultricies vulputate eget, rutrum at velit. Vestibulum at nisi ac est vehicula luctus vitae id mauris. Nam tempor egestas justo, a consectetur purus porta non. Curabitur suscipit odio eu enim fringilla, non elementum dui luctus. Sed nec feugiat libero. Mauris luctus nulla ornare, dignissim nulla id, rutrum nunc.
12 changes: 12 additions & 0 deletions tests/test-content/content/public/published-sub/content.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
---
title: Sub-folder content title
---

# Heading in the way

Short sentence.

## Heading in the way

Something a little bit longer.

### Heading the way

Third sentence.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: Sub-folder content title
---

# Heading in the way

Short sentence.

## Heading in the way

Something a little bit longer.

### Heading the way

Third sentence.

## Heading in the way

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce felis arcu, molestie nec imperdiet eu, tristique ut elit. Curabitur iaculis sodales turpis a pellentesque. In ac nibh ex. Maecenas ornare in nisi ut commodo. Etiam consequat aliquam erat. Quisque varius mattis risus, consequat viverra eros scelerisque in. Nulla faucibus porta libero a sollicitudin. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Duis finibus placerat ante faucibus hendrerit. Phasellus condimentum nisi sed velit pretium, at feugiat quam convallis.

Nunc sed felis a lacus convallis porttitor ut eu nulla. Morbi ac neque elit. Cras sit amet hendrerit purus. Nullam in nisi sem. Phasellus sodales condimentum velit, eget tempus urna scelerisque ullamcorper. Curabitur viverra velit sem, vel finibus justo porttitor ultricies. Ut arcu turpis, convallis at efficitur eget, aliquet sit amet dolor. Vestibulum ut sem nec purus imperdiet finibus. Vestibulum auctor tortor a lacinia faucibus. Nulla sagittis fringilla tincidunt. Fusce suscipit et diam et porta. Mauris felis justo, aliquam et neque eget, aliquet venenatis risus. Maecenas erat arcu, semper vitae massa sit amet, facilisis vulputate diam. Praesent lacinia pulvinar vestibulum. Nullam tristique augue magna, vel fermentum mauris malesuada nec. Aenean molestie odio sodales ante molestie pharetra.

Phasellus sit amet mi vitae turpis tristique luctus. Ut pellentesque mattis nisl, rutrum accumsan mi placerat ac. Vivamus nunc ligula, malesuada non lacinia in, accumsan id sem. Proin tempus ac lacus a pulvinar. Nunc laoreet maximus sem nec scelerisque. Maecenas sem erat, faucibus non arcu sed, accumsan vehicula sapien. Nullam ipsum lorem, bibendum non arcu convallis, fringilla interdum lacus.

Vivamus elementum elit at lectus dignissim, in commodo sem convallis. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Vivamus venenatis non ante quis ullamcorper. In facilisis ultricies tempus. Proin mollis sagittis condimentum. Vestibulum ornare pretium pellentesque. In dictum sodales felis ut ornare.

Fusce nec ullamcorper magna, ac pretium quam. Integer elementum tincidunt ligula eget tristique. Vivamus orci neque, finibus ultricies vulputate eget, rutrum at velit. Vestibulum at nisi ac est vehicula luctus vitae id mauris. Nam tempor egestas justo, a consectetur purus porta non. Curabitur suscipit odio eu enim fringilla, non elementum dui luctus. Sed nec feugiat libero. Mauris luctus nulla ornare, dignissim nulla id, rutrum nunc.

0 comments on commit f464a20

Please sign in to comment.