How to Set Markdown Environment on Matrix

Feb. 1, 2017 | smlee36 | #seneca, #matrix, #markdown, #apache, #rewrite

Matrix does not support markdown format (xxxx.md). These are some settings to work markdown files.

TOC

  1. Install markdown parser
  2. Make index.php
  3. Rewrite URL
  4. Verify Markdown File
  5. Apply Markdown theme (Optional)

1. Install markdown parser.

Login on the matrix and move to home directory.

cd ~/

Download cebe/markdown parser

composer require cebe/markdown "~1.0.1"

2. Make index.php

Create ~/public_html/index.php
<?php
require_once dirname(__FILE__) . '/../vendor/autoload.php';

$path = (isset($_REQUEST['p'])) ? $_REQUEST['p'] : false;
if ($path) {
    $file = dirname(__FILE__) . '/' . $path;

    if (!file_exists($file)) {
        header('HTTP/1.0 403 Forbidden');
        exit;
    }
} else {
    $file = dirname(__FILE__) . '/index.md';
}

$markdown = file_get_contents($file);

$_BASE = (isset($_SERVER['PHP_SELF'])) ? str_replace('/index.php', '', $_SERVER['PHP_SELF']) : '';

//include __DIR__ . '/header.html';

// use github markdown
$parser = new \cebe\markdown\GithubMarkdown();
$parser->html5 = true;
echo $parser->parse($markdown);

//include __DIR__ . '/footer.html';


3. Rewrite URL

Create ~/public_html/.htaccess

Options -Indexes
DirectoryIndex index.php index.html index.md

# 404 Not found for .git
RedirectMatch 404 /\.git

RewriteEngine On
RewriteCond  %{REQUEST_FILENAME} -f
RewriteRule ^(.*\.md)$ /~smlee36/?p=$1 [QSA,L]

(!) Replace 'smlee36' to your matrix id.

4. Verify Markdown file

Now all settings finish. Create a test file '~/public_html/test.md'

# This is a test markdown file

Open a web browser, go 'http://martrix.senecac.on.ca/~smlee36/test.md'

(!) Replace 'smlee36' to your matrix id.

If you can see the page like this:

This is a test markdown file

Every things works fine.

5. Apply Markdown theme (Optional)

Install Markdown Theme from (http://markedstyle.com/styles/amblin)

copy the source and paste to ~/public_html/css/markdown.css

Uncomment header.html/footer.html in ~/public_html/index.php


//...

$_BASE = (isset($_SERVER['PHP_SELF'])) ? str_replace('/index.php', '', $_SERVER['PHP_SELF']) : '';

include __DIR__ . '/header.html';       // <-- uncomment this

// use github markdown
$parser = new \cebe\markdown\GithubMarkdown();
$parser->html5 = true;
echo $parser->parse($markdown);

include __DIR__ . '/footer.html';       // <-- uncomment this

//...


Create ~/public_html/header.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Markdown Page</title>
    <link rel="stylesheet" href="<?php echo $_BASE;?>/css/markdown.css">
</head>
<body>
<div id="wrapper">

Create ~/public_html/footer.html

<hr/>
</div>
</body>
</html>

Links