Get the raw text content from a markdown file to use elsewhere in your code. Made for use in conjunction with the erusev/parsedown composer package.
<?php | |
/** Print Markdown | |
* Author: Jordan Brauer <jbrauer.inc@gmail.com> | |
* Function: print_md() | |
* Description: get the raw text content from a markdown file | |
* to use elsewhere in your code. | |
* | |
* Note: Made for use in conjunction with the erusev/parsedown composer package. | |
*/ | |
function print_md ($file) | |
{ | |
// check if $file exists | |
if (file_exists($file)) { | |
// get file extension of $file and assign to a variable. | |
$info = new SplFileInfo($file); | |
$ext = $info->getExtension(); | |
// supported markdown extensions to check $ext against. | |
$supported_ext = array( | |
'markdown', | |
'mdown', | |
'mkdn', | |
'md', | |
'mkd', | |
'mdwn', | |
'mdtxt', | |
'mdtext', | |
'text', | |
'Rmd', | |
); | |
// check $ext against the $supported_ext array. | |
if (in_array($ext, $supported_ext)) { | |
// if the file is indeed markdown, assign its' contents to a variable for returning. | |
$md = file_get_contents($file); | |
return $md; | |
} | |
// throw error if the extension is not in the list and return false. | |
throw new Exception('The file ' . $file . '\'s extension is not a supported markdown file extension.'); | |
return false; | |
} | |
// throw an error if the file does not exist and return false. | |
throw new Exception('The file ' . $file . ' does not exist.'); | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment