Setup a CommentaryController to return commentary markdown files as html
This commit is contained in:
parent
5c24c3fd6f
commit
34b44d56c0
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Helpers\Traits\BookXmlFilesTrait;
|
||||
use GrahamCampbell\Markdown\Facades\Markdown;
|
||||
|
||||
class CommentaryController extends Controller
|
||||
{
|
||||
use BookXmlFilesTrait;
|
||||
|
||||
public function __invoke(Request $request, $book, $chapter)
|
||||
{
|
||||
$book = $this->fixBookName($book);
|
||||
$filepath = storage_path() . '/en_bc/'
|
||||
. strtolower(self::$bookXmlFiles[$book]) . '/'
|
||||
. $this->prepChapterName($chapter);
|
||||
|
||||
if (file_exists($filepath)) {
|
||||
return Markdown::convertToHtml(file_get_contents($filepath));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Book names we get from the FE will have space between the number and the title: 1 corinthians
|
||||
* which needs to be removed.
|
||||
*
|
||||
* @param string $bookName
|
||||
* @return string
|
||||
*/
|
||||
private function fixBookName(string $bookName) : string
|
||||
{
|
||||
if (is_numeric(substr($bookName, 0, 1))) {
|
||||
return strtolower(substr($bookName, 0, 1) . ltrim(substr($bookName, 1)));
|
||||
} else {
|
||||
return strtolower($bookName);
|
||||
}
|
||||
}
|
||||
|
||||
private function prepChapterName(string $chapterName) : string
|
||||
{
|
||||
if (strtolower($chapterName) === "intro") {
|
||||
return 'intro.md';
|
||||
} else {
|
||||
return sprintf("%02d.md", $chapterName);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,8 +18,9 @@ Route::middleware('auth:api')->get('/user', function (Request $request) {
|
|||
return $request->user();
|
||||
});
|
||||
|
||||
Route::prefix('api')->group(function () {
|
||||
Route::prefix('api/v1')->group(function () {
|
||||
Route::post('contact/send', 'ContactController@send');
|
||||
Route::get('commentary/{book}/chapter/{chapter}', 'CommentaryController');
|
||||
});
|
||||
|
||||
JsonApi::register('default')->routes(function ($api) {
|
||||
|
|
Loading…
Reference in New Issue