markdown格式wiki文档

Index.php 1.9KB

    <?php namespace app; use app\View; use app\Route; class Index extends View { // public function __construct(){ // // $this->route = new Route(); // } public function index() { $doc = $GLOBALS['doc']; $doc = ltrim($doc, '/'); if (substr($doc, -1) === '/') { $doc = $doc . "index.md"; } $doc = empty($doc) ? 'index.md' : $doc; if (pathinfo($doc,PATHINFO_EXTENSION) != 'md') { $doc = $doc . ".md"; } $filename = DATA_PATH . $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']; $pathinfo = pathinfo($doc); $dir_name = DATA_PATH . $pathinfo['dirname']; if (!is_dir($dir_name) && !mkdir($dir_name, '0755', true)) { die(json_encode(['code'=>2, 'msg'=>'目录创建失败'])); } $filename = DATA_PATH . $doc; $res = file_put_contents($filename, $content); if ($res === false) { echo json_encode(['code'=>2, 'msg'=>'保存失败']); } else { echo json_encode(['code'=>0, 'msg'=>'保存成功, 字数:' . $res]); } die(); } }