centos中nginx配置markdown 編輯器例子

來源:互聯網
上載者:User

先說一下實現的原理,當然此文章主要講了我的實現,當然還有其他的實現方式,原理都是差不多的。
首先,我們需要安裝kramdown解譯器,這個很好理解吧,和php一樣,他的特點標籤能夠實現渲染肯定需要解譯器來解析,這裡kramdown解譯器的官網為http://kramdown.gettalong.org/installation.html,這是安裝介面,根據自己的系統安裝即可,不過他需要安裝ruby依賴。然後我通過nginx的url轉寄,將.md的文檔轉寄給一個php檔案來處理,這個php檔案,就調用kramdown解譯器來解析對應文檔即可。
原理很簡單吧,看下具體實現步驟吧,

1、安裝kramdown解譯器

 代碼如下 複製代碼

sudo aptitude install ruby rubygems
sudo gem install kramdown

安裝完,(ruby可以直接yum安裝),可以寫一個md的測試檔案,使用kramdown命令試一下。
2、製作kramdown解析程式
這裡我用的是php,當然也可以用其他語言,只要可以調用linux的system命令即可,當然php安裝有的時候會屏蔽這些敏感函數,注意要釋放一下。
檔案發一下

 代碼如下 複製代碼
<?php
   $file = @$_GET['file'];
   if(!is_file($file))
   header("HTTP/1.0 404 Not Found");
   header("Content-type: text/html; charset=utf-8");
   date_default_timezone_set('Asia/Shanghai');
   $filename = strrchr($file,'/');
   $filename = ltrim($filename,'/');
   $odir = rtrim($file,$filename);
?>
<head>
<title><?php echo $filename;?></title>
<link href="/github-59da74dcbe2f1d555e306461652274f8741238a64e7b1fe8cc5a286232044835.css" media="all" rel="stylesheet" type="text/css">
<link href="/github2-22a0054564248c6dd87336e91bca068b1ab49e28ee1062519b3a0722d29da804.css" media="all" rel="stylesheet" type="text/css">
</head>
<body>
<div class="header header-logged-in true" role="banner">
  <div class="container clearfix">
    <a class="header-logo-invertocat" href="/" data-hotkey="g d" aria-label="Homepage" ga-data-click="Header, go to dashboard, icon:logo">
</a>
 
      <ul class="header-nav left" role="navigation">
        <li class="header-nav-item explore">
          <a class="header-nav-link" href="/" data-ga-click="Header, go to explore, text:explore">網站首頁</a>
        </li>
        <li class="header-nav-item">
            <a class="header-nav-link" href="/archives/category/lovephp/" data-ga-click="Header, go to gist, text:gist">我愛php</a>
          </li>
          <li class="header-nav-item">
            <a class="header-nav-link" href="/archives/category/tj/" data-ga-click="Header, go to gist, text:gist">推薦文章</a>
          </li>
          <li class="header-nav-item">
            <a class="header-nav-link" href="/xiangce/" target="_blank" data-ga-click="Header, go to blog, text:blog">相簿</a>
          </li>
          <li class="header-nav-item">
            <a class="header-nav-link" href="/flink/" data-ga-click="Header, go to blog, text:blog">兄弟鏈</a>
          </li>
        <li class="header-nav-item">
          <a class="header-nav-link" href="/message/" data-ga-click="Header, go to help, text:help">關於我</a>
        </li>
      </ul>
  </div>
</div>
<div class="container">
    <div id="js-repo-pjax-container" class="repository-content context-loader-container" data-pjax-container="">
    <div class="file-wrap">
      <table class="files" data-pjax="">
        <tbody>
        <?php
            $dir = opendir($odir);
            while (($f = readdir($dir)) !== false){
                if('.' !=$f && '..' != $f){
                $href = $f;
                $type = 'File';
                if(is_dir($odir.$f)){
                    $href = $f.'/index.md';//目錄的話預設訪問裡面的index.md
                    $type = 'Dir';
                }
                $time = date('Y-m-d H:i:s',filemtime($odir.$f));
        ?>
            <tr>
              <td class="icon">
              </td>
              <td class="content">
                <span class="css-truncate css-truncate-target"><a href="<?php echo $href;?>" class="js-directory-link" title=""><?php echo $f;?></a></span>
              </td>
              <td class="message">
                <span class="css-truncate css-truncate-target">
                <?php echo $type;?>
                </span>
              </td>
              <td class="age">
                <span class="css-truncate css-truncate-target"><?php echo $time;?></span>
              </td>
            </tr>
            <?php
                }
            }
            ?>
        </tbody>
      </table>
    </div>
        <div class="file-box">
           <div class="file">
            <div class="meta clearfix">
              <div class="info file-name">
                  <span><?php echo $filename;?></span>
              </div>
            </div>
            <div id="readme" class="blob instapaper_body">
                <article class="markdown-body entry-content" itemprop="mainContentOfPage">
                <?php
                   echo system('/usr/bin/kramdown  '.$file);
                ?>
                </article>
            </div>
           </div>
        </div>
    </div>
</div>
</body>

實現了一些簡單的檔案遍曆和樣式,這個可以隨意製作,我是用的github的樣式

3、nginx配置url重寫

當然這裡就根據自己的喜好了,當然也可以製作soket服務,不過php畢竟做服務的話不太好,還有配合shell監控,所以不如直接使用url重寫了

 代碼如下 複製代碼

location ~ ^(.+\.md)(.*) {
          rewrite .* /md.php?file=$document_root$fastcgi_script_name last;
}

4、另外

最後在nginx的web服務加上個預設頁

index index.html index.htm index.php index.md default.html default.htm default.php;

這樣就可以預設訪問index.md檔案了
這樣就實現了自己網站的markdown檔案解析了,以後可以寫md文檔了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.