Skip to content
This repository was archived by the owner on Jan 21, 2024. It is now read-only.

Commit c4372d7

Browse files
committed
Add caching abilities w/ tests and blade directive
1 parent 65c862e commit c4372d7

File tree

5 files changed

+110
-8
lines changed

5 files changed

+110
-8
lines changed

README.md

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
![Gitdown - a simple package to parse markdown in PHP](banner.png)
1+
![GitDown - a simple package to parse markdown in PHP](banner.png)
22

33
# GitDown
44
A simple package to parse Github Flavored Markdown in PHP.
@@ -8,28 +8,62 @@ This package is a fraud. All it does is fire off your markdown to a public GitHu
88

99
I personally think this is not a bug, but a feature, because the markdown is actually getting parsed by GitHub itself, and not a third-party library.
1010

11-
However, each time you call `GitDown::parse()` you are hitting a live endpoint. Because of this, it is STRONGLY recommended that you store the parsed output or cache it.
11+
However, each time you call `GitDown::parse()` you are hitting a live endpoint. Because of this, it is STRONGLY recommended that you store the parsed output or use it's caching features.
1212

1313
## Installation
1414

1515
```bash
1616
composer require calebporzio/gitdown
1717
```
1818

19-
## Usage
19+
## Simplest Usage
2020

2121
```php
22-
CalebPorzio\GitDown::parse('# Some Markdown');
22+
CalebPorzio\GitDown::parse($markdown);
23+
CalebPorzio\GitDown::parseAndCache($markdown);
24+
```
25+
26+
## Laravel-only Usage
27+
```php
28+
// Will be cached forever. (suggested)
29+
CalebPorzio\GitDown::parseAndCache($markdown);
30+
31+
// Will be cached for 24 hours. (minutes in Laravel < 5.8, seconds otherwise)
32+
CalebPorzio\GitDown::parseAndCache($markdown, $seconds = 86400);
33+
```
34+
35+
## Non-Laravel Usage
36+
```php
37+
CalebPorzio\GitDown::parse($markdown);
38+
39+
// Pass in your own custom caching strategy.
40+
CalebPorzio\GitDown::parseAndCache($markdown, function ($parse) {
41+
return Cache::rememberForever(sha1($markdown), function () use ($parse) {
42+
return $parse();
43+
});
44+
});
2345
```
2446

2547
## Making it look good
2648

2749
Styling markdown with CSS has always been a bit of a pain for me. Not to mention trying to style syntax inside code blocks. Not to worry!
2850

29-
GitDown ships with all the CSS you need to make your markdown look exactly like it does on GitHub. Just add this code somewhere on your HTML page, preferably near your other stylesheets.
51+
GitDown ships with all the CSS you need to make your markdown look exactly like it does on GitHub. Just add this code somewhere on your HTML page, preferably near your other stylesheets in the `<head>` section.
52+
53+
**Laravel-only**
54+
```php
55+
<head>
56+
[...]
57+
@gitdown
58+
</head>
59+
```
3060

61+
**Non-Laravel**
3162
```php
32-
<style><?php echo CalebPorzio\GitDown::styles(); ?></style>
63+
<head>
64+
[...]
65+
<style><?php echo CalebPorzio\GitDown::styles(); ?></style>
66+
</head>
3367
```
3468

3569
Bam! That's all you need to make everything look good 🤙.

composer.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
"require-dev": {
1717
"phpunit/phpunit": "^7.0"
1818
},
19+
"extra": {
20+
"laravel": {
21+
"providers": [
22+
"CalebPorzio\\GitDownServiceProvider"
23+
]
24+
}
25+
},
1926
"autoload": {
2027
"psr-4": {
2128
"CalebPorzio\\": "src"

src/GitDown.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@
44

55
class GitDown
66
{
7+
public static function parseAndCache($content, $minutes = null)
8+
{
9+
if (is_callable($minutes)) {
10+
return $minutes(static::generateParserCallback($content));
11+
} elseif (is_null($minutes)) {
12+
return cache()->rememberForever(sha1($content), function () use ($content) {
13+
return static::parse($content);
14+
});
15+
}
16+
17+
return cache()->remember(sha1($content), $minutes, function () use ($content) {
18+
return static::parse($content);
19+
});
20+
}
21+
22+
public static function generateParserCallback($content)
23+
{
24+
return function () use ($content) {
25+
return static::parse($content);
26+
};
27+
}
28+
729
public static function parse($content)
830
{
931
$ch = curl_init();

src/GitDownServiceProvider.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace CalebPorzio;
4+
5+
use Illuminate\Support\Facades\Blade;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class GitDownServiceProvider extends ServiceProvider
9+
{
10+
public function boot()
11+
{
12+
Blade::directive('gitdown', function () {
13+
return '<style>'. GitDown::styles() .'</style>';
14+
});
15+
}
16+
}

tests/GitDownTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,31 @@ public function github_properly_parses_markdown()
2020
$this->assertEquals(<<<EOT
2121
<p><strong>foo</strong></p>
2222
<p><a href="baz">bar</a></p>
23-
2423
EOT
25-
, $parsed);
24+
, trim($parsed));
25+
}
26+
27+
/** @test */
28+
public function can_provide_caching_strategy()
29+
{
30+
$numberOfTimesGitHubWasCalled = 0;
31+
32+
$firstResult = GitDown::parseAndCache('**foo**', $this->cacheStrategy($numberOfTimesGitHubWasCalled));
33+
$secondResult = GitDown::parseAndCache('**foo**', $this->cacheStrategy($numberOfTimesGitHubWasCalled));
34+
35+
$this->assertEquals('<p><strong>foo</strong></p>', trim($firstResult));
36+
$this->assertEquals('cached', $secondResult);
37+
}
38+
39+
protected function cacheStrategy(&$callCount)
40+
{
41+
return function ($parse) use (&$callCount) {
42+
if ($callCount < 1) {
43+
$callCount++;
44+
return $parse();
45+
} else {
46+
return 'cached';
47+
}
48+
};
2649
}
2750
}

0 commit comments

Comments
 (0)