LaravelでMarkdownをブラウザに表示する方法

Be33a704f251c77162896ac5ba03e9ac?s=47 SAW
October 30, 2019

 LaravelでMarkdownをブラウザに表示する方法

Web×PHP TechCafe 10月 の発表資料です。

Be33a704f251c77162896ac5ba03e9ac?s=128

SAW

October 30, 2019
Tweet

Transcript

  1. -BSBWFMͰ.BSLEPXOΛ ϒϥ΢βʹදࣔ͢Δํ๏ 8FCº1)15FDI$BGF  4"8

  2.  XIPBNJ w ࢯ໊Ճ౻फҰ࿠ ࡀ  ‣ େࡕࡏॅɾѪ஌ग़਎ ‣ ϋϯυϧωʔϜ4"8

    w 4/4ΞΧ΢ϯτ ‣ 5XJUUFS!B[VLJ@FBUFS ‣ (JU)VCB[VLJQFOHVJO w ॴଐגࣜձࣾ.4&/ ೥໨ 2 FreeBSD macOS Linux Vue.js
  3. .BSLEPXOΛ)5.-ʹม׵ͯ͠ૹ৴ w -BSBWFMͰ.BSLEPXOΛ)5.-ʹม׵ͯ͠Ϗϡʔʹ൓ө͍ͨ͠ ‣ ྫ.BSLEPXOͰهࣄΛॻ͚ΔϒϩάγεςϜͷ࡞੒ w 1)1ͷ.BSLEPXOύʔαϥΠϒϥϦcebe/markdownΛಋೖ ‣ ෳ਺ͷ.BSLEPXOॻࣜʹରԠ 

    5SBEJUJPOBM(JU)VC.BSLEPXO&YUSB 3
  4. 1)1.BSLEPXOύʔαϥΠϒϥϦͷൺֱ w DFCFNBSLEPXO ‣ ରԠॻࣜ5SBEJUJPOBM(JU)VC.BSLEPXO&YUSB w 1)1.BSLEPXO ‣ ରԠॻࣜ5SBEJUJPOBM.BSLEPXO&YUSB w

    1BSTFEPXO ‣ ରԠॻࣜ(JU)VC 4
  5. DFCFNBSLEPXOͷಋೖํ๏ w $PNQPTFSܦ༝ͰΠϯετʔϧ ‣ composer require cebe/markdown w .BSLEPXOύʔαΫϥεΛಡΈࠐΈ ‣

    cebe\markdown\Markdown.BSLEPXOͷύʔαΫϥε ‣ cebe\markdown\GithubMarkdown(JU)VCॻࣜͷύʔαΫϥε ‣ cebe\markdown\MarkdownExtra.BSLEPXO&YUSBॻࣜͷύʔαΫϥε 5
  6. DFCFNBSLEPXOͷར༻ํ๏ w .BSLEPXOύʔαΫϥεͷಡΈࠐΈ ‣ use cebe\markdown\GithubMarkdown; w ύʔαΠϯελϯεͷੜ੒ ‣ new

    GithubMarkdown(); w )5.-ܗࣜʹม׵ ‣ $parser->parse($markdown); 6 <?php use cebe\markdown\GithubMarkdown; public class Md2Html { public static function convert($md) { $parser = new GithubMarkdown(); return $parser->parse($md); } }
  7. ม׵݁ՌΛϏϡʔʹ൓ө w #MBEFϑΝΠϧʹม׵಺༰ΛຒΊࠐΈ ‣ $parser->parse($md)͸)5.-ܗࣜͷจࣈྻΛฦ٫ ‣ {!! !!}Ͱ)5.-ͷΤεέʔϓΛ๷ࢭ 7 <div>{!!

    $html !!}</div> <div> <ul> <li>Laravel</li> <li>CakePHP</li> <li>FuelPHP</li> </ul> </div>
  8. .BSLEPXOύʔαͷར༻ྫ w .BSLEPXOͰهࣄΛฤू͢ΔϒϩάγεςϜ 8 Laravel ฤू ౤ߘɾϓϨϏϡʔ อଘ NE औಘ

    NE ม׵ IUNM ౤ߘ಺༰ͷ Ϗϡʔ ΫϥΠΞϯτ %# औಘ NE ฤूը໘
  9. ιʔείʔυྫ 9 class Article extends Model { public function getBodyAttribute($value)

    { $parser = new GithubMarkdown(); $this->attributes[‘body’] = $parser->parse($value); } } class ArticleController extends Controller { public function index(Request $req, $id) { $article = Article::find($id); return view(‘article’, [ ‘article’ => $article ]); } } <h1> {{ $article->title }} </h1> <article> {!! $article->body !!} </article>
  10. ·ͱΊ w 1)1ͷ.BSLEPXOύʔαDFCFNBSLEPXOͷ঺հ ‣ ෳ਺ͷॻࣜʹରԠՄ w -BSBWFM΁ͷಋೖํ๏ͱར༻ํ๏Λઆ໌ ‣ ରԠ͢Δ.BSLEPXOύʔαͷछྨ ‣

    .BSLEPXO͔Β)5.-΁ม׵͢Δํ๏ 10
  11. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠