diff --git a/README.md b/README.md index 63032fc..6f14b89 100644 --- a/README.md +++ b/README.md @@ -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. @@ -8,7 +8,7 @@ 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 @@ -16,20 +16,54 @@ However, each time you call `GitDown::parse()` you are hitting a live endpoint. 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 `
` section. + +**Laravel-only** +```php + + [...] + @gitdown + +``` +**Non-Laravel** ```php - + + [...] + + ``` Bam! That's all you need to make everything look good 🤙. diff --git a/composer.json b/composer.json index 5c98bb0..5b17376 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,13 @@ "require-dev": { "phpunit/phpunit": "^7.0" }, + "extra": { + "laravel": { + "providers": [ + "CalebPorzio\\GitDownServiceProvider" + ] + } + }, "autoload": { "psr-4": { "CalebPorzio\\": "src" diff --git a/src/GitDown.php b/src/GitDown.php index b2993c2..61ab1dc 100644 --- a/src/GitDown.php +++ b/src/GitDown.php @@ -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(); diff --git a/src/GitDownServiceProvider.php b/src/GitDownServiceProvider.php new file mode 100644 index 0000000..625442e --- /dev/null +++ b/src/GitDownServiceProvider.php @@ -0,0 +1,16 @@ +'. GitDown::styles() .''; + }); + } +} diff --git a/tests/GitDownTest.php b/tests/GitDownTest.php index fffa81a..1645e5c 100644 --- a/tests/GitDownTest.php +++ b/tests/GitDownTest.php @@ -20,8 +20,31 @@ public function github_properly_parses_markdown() $this->assertEquals(<<foo
', trim($firstResult)); + $this->assertEquals('cached', $secondResult); + } + + protected function cacheStrategy(&$callCount) + { + return function ($parse) use (&$callCount) { + if ($callCount < 1) { + $callCount++; + return $parse(); + } else { + return 'cached'; + } + }; } }