Spaces:
Running
Running
File size: 1,813 Bytes
907b200 7922c7a 907b200 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
class ApiDocsController extends Controller
{
public function index()
{
$routes = collect(Route::getRoutes())->map(function ($route) {
if (str_starts_with($route->uri(), 'api/')) {
return [
'method' => $route->methods()[0] === 'HEAD' ? 'GET' : $route->methods()[0],
'path' => '/' . $route->uri(),
];
}
return null;
})->filter()->unique('path')->sortBy('path')->values();
return view('api-endpoints', compact('routes'));
}
public function showDocs()
{
$path = base_path('API.md');
if (!file_exists($path) || !is_readable($path)) {
$content = '<div class="alert alert-warning">API documentation file not found.</div>';
return view('api-docs', compact('content'));
}
$markdown = file_get_contents($path);
$content = Str::markdown($markdown);
$tocMap = [];
preg_match_all('/-\s+\[(.+?)\]\(#(.+?)\)/', $markdown, $matches, PREG_SET_ORDER);
foreach ($matches as $m) {
$tocMap[trim($m[1])] = $m[2];
}
$content = preg_replace_callback(
'/<h([2-6])>(.*?)<\/h\1>/i',
function ($m) use ($tocMap) {
$level = $m[1];
$inner = $m[2];
$text = trim(strip_tags(html_entity_decode($inner)));
$id = $tocMap[$text] ?? Str::slug($text);
return sprintf('<h%s id="%s">%s</h%s>', $level, $id, $inner, $level);
},
$content
);
return view('api-docs', compact('content'));
}
}
|