Skip to content
Advertisement

How do I parse GitHub markdown to PHP from a URL?

I can already parse a Markdown file to HTML but my problem is getting that markdown file from a URL instead of a local file.

Below is the code I have:

<?php
include('./includes/Parsedown.php');
$Parsedown = new Parsedown();
$text = 'https://raw.githubusercontent.com/dylanwe/How-to-make-a-discord-bot/master/README.md';
$slug = 'How-to-make-a-discord-bot';
$text = str_replace('img', "https://raw.githubusercontent.com/dylanwe/$slug/master/img", $text);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.20.0/themes/prism-coy.min.css">
    <link rel="stylesheet" href="css/prism_github.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/4.0.0/github-markdown.min.css">
    <style>
        .markdown-body {
            max-width: 780px;
            margin: 0 auto;
            padding: 15px;
        }
        pre[class*=language-]>code {
            border: none;
            box-shadow: none;
        }
    </style>
    <title>Document</title>
</head>
<body>
    <div class="markdown-body">
        <?php echo $Parsedown->text($text); ?>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.20.0/prism.min.js"></script>
</body>
</html>

Advertisement

Answer

It’s not very clear what isn’t working as expected, but it looks like you just need to retrieve the contents of the Markdown file, e.g. using file_get_contents():

$text = file_get_contents('https://raw.githubusercontent.com/dylanwe/How-to-make-a-discord-bot/master/README.md');
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement