【问题标题】:How do I parse GitHub markdown to PHP from a URL?如何从 URL 将 GitHub markdown 解析为 PHP?
【发布时间】:2020-11-06 12:33:42
【问题描述】:

我已经可以将 Markdown 文件解析为 HTML,但我的问题是从 URL 而不是本地文件获取该 Markdown 文件。

下面是我的代码:

<?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>

【问题讨论】:

  • 究竟是什么没有按预期工作?

标签: php github markdown


【解决方案1】:

不是很清楚什么没有按预期工作,但看起来您只需要检索 Markdown 文件的内容,例如使用file_get_contents():

$text = file_get_contents('https://raw.githubusercontent.com/dylanwe/How-to-make-a-discord-bot/master/README.md');

【讨论】:

  • 当我尝试这个时,我得到这个错误:警告:在 C:\xampp\htdocs\ 中加载 cafile 流失败:`C:\xampp\apache\bin\curl-ca-bundle.crt'第 5 行的 md_parser\index.php
  • @DylanWeijgertze,请参阅 this answer 以解决您的证书问题。
猜你喜欢
  • 2014-02-17
  • 2017-01-26
  • 2023-03-03
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 2013-05-25
相关资源
最近更新 更多