A quick way to do a Php wiki with Markdown and Git

Published on

I received many email, comments and feedbacks about the need of a central Wiki for the universe of Pepper&Carrot named Hereva. That's why I decided to add a collaborative Wiki to the website. You can find it on the top categorie. That's the start of a new adventure, and I hope I'll use it to add more details, and illustrate it step by step. Corrections and proof-reading needed :)

Technical details:

The wiki is manually sync with the Github wiki . Github wiki is a git repository with markdown files. Here is a screenshot of the folder with the markdown files. It's easy to clone or to edit online with a Github account.

git clone https://github.com/Deevad/peppercarrot.wiki.git  

To translate the markdown files to Html, I used an open library named parsedown.php. I used the static pages of Pluxml ( the CMS I use for running Pepper&Carrot ) to display the markdown pages. I also generated a menu with PHP to wrap-up everything. A Wiki is born!

PHP code for Pluxml

Here is my PHP code to include a markdown page inside a Pluxml static page ( static8/wiki in my case ), I share it because I didn't found anything like this on internet, and I had to do it from scratch. I guess a lot of project would like to have this type of sync between website and a markdown wiki and this code should be pretty standard and easy to tweak because I fully commented it : Loading the Markdown pages :


// get variable 'page' from URL  
$wikipage = htmlspecialchars($_GET["page"]);  

// Security, remove all special characters except A-Z, a-z, 0-9, dots, hyphens, underscore before interpreting something.   
$wikipage = preg_replace('/[^A-Za-z0-9\._-]/', '', $wikipage);  

    // page found  
    // no page found, we propose fallback homepage Home.md  
    $wikipage = "Home";  

// add library to parse markdown Github files  

// display content main page  
      $contents = file_get_contents('path/to/your/folder/'. $wikipage .'.md');  
      $Parsedown = new Parsedown();  
      echo $Parsedown->text($contents);  

Generating the Menu buttons :

            // dynamic menu generation from a folder  

              # we scan all markdown in folder  
              $search = glob("path/to/your/folder/*.md");  

              # we loop on found files  
              if (!empty($search)){   
                foreach ($search as $wikifile) {  

                  // clean path to filename only  
                  $wikifile = basename($wikifile);  

                  // _Footer and _Sidebar are special page, they start with '_' exclude them  
                  if (substr($wikifile, 0, 1) === '_') {  
                    // page starting with '_' found, do nothing.  
                  } else {  
                    // Check if the page is Home, we don't display it here to keep it hardcoded on the top  
                    if ($wikifile === 'Home.md') {  
                    // Home.md found, do nothing  
                    } else {  
                      // Finally! we have a valid markdown page  
                      // Clean filename to get only name without extension :  
                      $wikifile = preg_replace('/\\.[^.\\s]{2,4}$/', '', $wikifile);  
                      // Is there a chance we display the page of this button now? time to set status actif!  
                      if ($wikifile === $wikipage) {  
                      // Ok, we have all, diplay this button now :  
                        echo '<a class="button" href="https://www.davidrevoy.com/';  
                        echo '" title="">'.$wikifile.'</a>';  

I hope it will be useful and you'll enjoy the Wiki !