A web based Markdown file viewer (IIS/PHP)

Firstly, thanks must go to the author of this post.

  • Put markdown.php in your blog site folder.
  • Add a posts folder and place all your md files into it.
  • Create a content folder and download google prettify and bootstrap
  • Create index.php
<html>
<head>
<link href='content/bootstrap.css' type='text/css' rel='stylesheet' />
<style>
* {
 font-family: Verdana;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-10">
<img src='content/logo.png' class='pull-right' />
<h1>Posts</h1>
<?php

$list = glob('posts/*.md');
foreach($list as $post)
{
    $postSansTXT = substr($post, 6);

    $PostMarkdown = file_get_contents($post);
    include_once "markdown.php";
    $PostHTML = Markdown($PostMarkdown);

    $doc = new DOMDocument();
    $doc->loadHTML($PostHTML);
    $h1 = $doc->getElementsByTagName('h1');

    echo "<p><a href='$postSansTXT'>";
    echo $h1->item(0)->nodeValue;
    echo '</a></p>';
}

?>
</div>
</div>
</div>
</body>
  • Create post.php
<html>
<head>
<link href='content/github.css' type='text/css' rel='stylesheet' />
<link href='content/bootstrap.css' type='text/css' rel='stylesheet' />
<script type='text/javascript' src='content/prettify.js'></script>
<style>
* {
 font-family: Verdana;
}
</style>
</head>
<body onload='prettyPrint();'>
<div class="container">
<div class="row">
<div class="col-md-10">
<img src='content/logo.png' class='pull-right' />
<?php 

$post = "C:/inetpub/wwwroot/Capita.E1f.Blog/posts/" . $_GET["file"] . ".md";

$PostMarkdown = file_get_contents($post);

include_once "markdown.php";

$PostHTML = Markdown($PostMarkdown);

echo $PostHTML;

?>
</div>
</div>
</div>
</body>
  • Create web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Imported Rule 1" stopProcessing="true">
                    <match url="^(.*).md$" ignoreCase="false" />
                    <action type="Rewrite" url="post.php?file={R:1}" appendQueryString="false" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
  • Modify markdown.php

Line 42 from false to true:

@define( 'MARKDOWN_CODE_ATTR_ON_PRE',   true );

Line 2971 add prettyprint:

if ($classname != "") {
    if ($classname{0} == '.')
        $classname = substr($classname, 1);
    $attr_str = ' class="prettyprint '.$this->code_class_prefix.$classname.'"';
} else {
    $attr_str = $this->doExtraAttributes($this->code_attr_on_pre ? "pre" : "code", $attrs);
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.