用 markdown 來寫文件是一個超級便利的事情,如果能再加上 Laravel 的 Blade 就更棒了,好在 Blade 可以做這件事,兩個願望一次達成啊
How to
安裝 markdown 套件
1
| composer require league/commonmark
|
Render Markdown
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
| use Illuminate\View\Factory; use League\CommonMark\CommonMarkConverter; use League\CommonMark\Environment;
class DocsController extends Controller {
private $viewFactory; public function __construct(Factory $viewFactory) { $this->viewFactory = $viewFactory; $this->viewFactory->addExtension('md', 'blade'); } public function index() { $environment = Environment::createGFMEnvironment();
$converter = new CommonMarkConverter([ 'allow_unsafe_links' => false, ], $environment); return $converter->convertToHtml($this->viewFactory->make('demo.md')); } }
|