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

Commit

Permalink
Add caching abilities w/ tests and blade directive
Browse files Browse the repository at this point in the history
  • Loading branch information
calebporzio committed Mar 6, 2019
1 parent 65c862e commit c4372d7
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 8 deletions.
46 changes: 40 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Gitdown - a simple package to parse markdown in PHP](banner.png)
![GitDown - a simple package to parse markdown in PHP](banner.png)

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

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.

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.
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.

## Installation

```bash
composer require calebporzio/gitdown
```

## Usage
## Simplest Usage

```php
CalebPorzio\GitDown::parse('# Some Markdown');
CalebPorzio\GitDown::parse($markdown);
CalebPorzio\GitDown::parseAndCache($markdown);
```

## Laravel-only Usage
```php
// Will be cached forever. (suggested)
CalebPorzio\GitDown::parseAndCache($markdown);

// Will be cached for 24 hours. (minutes in Laravel < 5.8, seconds otherwise)
CalebPorzio\GitDown::parseAndCache($markdown, $seconds = 86400);
```

## Non-Laravel Usage
```php
CalebPorzio\GitDown::parse($markdown);

// Pass in your own custom caching strategy.
CalebPorzio\GitDown::parseAndCache($markdown, function ($parse) {
return Cache::rememberForever(sha1($markdown), function () use ($parse) {
return $parse();
});
});
```

## Making it look good

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!

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.
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.

**Laravel-only**
```php
<head>
[...]
@gitdown
</head>
```

**Non-Laravel**
```php
<style><?php echo CalebPorzio\GitDown::styles(); ?></style>
<head>
[...]
<style><?php echo CalebPorzio\GitDown::styles(); ?></style>
</head>
```

Bam! That's all you need to make everything look good 🤙.
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
"require-dev": {
"phpunit/phpunit": "^7.0"
},
"extra": {
"laravel": {
"providers": [
"CalebPorzio\\GitDownServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"CalebPorzio\\": "src"
Expand Down
22 changes: 22 additions & 0 deletions src/GitDown.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@

class GitDown
{
public static function parseAndCache($content, $minutes = null)
{
if (is_callable($minutes)) {
return $minutes(static::generateParserCallback($content));
} elseif (is_null($minutes)) {
return cache()->rememberForever(sha1($content), function () use ($content) {
return static::parse($content);
});
}

return cache()->remember(sha1($content), $minutes, function () use ($content) {
return static::parse($content);
});
}

public static function generateParserCallback($content)
{
return function () use ($content) {
return static::parse($content);
};
}

public static function parse($content)
{
$ch = curl_init();
Expand Down
16 changes: 16 additions & 0 deletions src/GitDownServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace CalebPorzio;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class GitDownServiceProvider extends ServiceProvider
{
public function boot()
{
Blade::directive('gitdown', function () {
return '<style>'. GitDown::styles() .'</style>';
});
}
}
27 changes: 25 additions & 2 deletions tests/GitDownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,31 @@ public function github_properly_parses_markdown()
$this->assertEquals(<<<EOT
<p><strong>foo</strong></p>
<p><a href="baz">bar</a></p>
EOT
, $parsed);
, trim($parsed));
}

/** @test */
public function can_provide_caching_strategy()
{
$numberOfTimesGitHubWasCalled = 0;

$firstResult = GitDown::parseAndCache('**foo**', $this->cacheStrategy($numberOfTimesGitHubWasCalled));
$secondResult = GitDown::parseAndCache('**foo**', $this->cacheStrategy($numberOfTimesGitHubWasCalled));

$this->assertEquals('<p><strong>foo</strong></p>', trim($firstResult));
$this->assertEquals('cached', $secondResult);
}

protected function cacheStrategy(&$callCount)
{
return function ($parse) use (&$callCount) {
if ($callCount < 1) {
$callCount++;
return $parse();
} else {
return 'cached';
}
};
}
}

0 comments on commit c4372d7

Please sign in to comment.