markdown格式wiki文档

Index.php 1.6KB

    <?php namespace app; use app\View; use app\Route; class Index extends View { public function __construct(){ // $this->route = new Route(); } public function index() { $doc = isset($_GET['doc']) ? $_GET['doc'] : 'index.md'; if (pathinfo($doc,PATHINFO_EXTENSION) != 'md') { $doc = $doc . ".md"; } $filename = DATA_PATH . DS . $doc; $text = ""; $toc = ""; $content = ""; if ($is_file_exists = file_exists($filename)) { $parsedownExtension = new \ParsedownExtension(); $parsedownExtension->setTocEnabled(true); $text = file_get_contents($filename); $res = $parsedownExtension->text($text); $toc = $res['toc']; $content = $res['content']; } // var_dump($toc); // exit; $this->assign('doc',$doc); $this->assign('is_file_exists', $is_file_exists); $this->assign('text',$text); $this->assign('toc',$toc); $this->assign('content',$content); $this->fetch(); } public function save() { $doc = $_POST['doc']; $content = $_POST['content']; $filename = DATA_PATH . DS . $doc; // var_dump($filename); // exit; $res = file_put_contents($filename, $content); if ($res === false) { echo json_encode(['code'=>2, 'msg'=>'保存失败']); } else { echo json_encode(['code'=>0, 'msg'=>'保存成功, 字数:' . $res]); } die(); } }