在我的网站上可视化README.md文件

14

我想在我的网站上展示来自Github项目的README.md文件,如何做到最好?是获取Markdown代码并在本地处理Markdown,还是有一种方法可以从Github获取已经处理过的Markdown内容?


一个回答建议使用 JavaScript,但这略过了它作为后端服务器进程的整个概念,这可能是最合适的解决方案。 - yourcomputergenius
4个回答

7
一种可能的解决方案是使用基于JavaScript的Markdown解析器,例如https://github.com/evilstreak/markdown-js
该库可以从浏览器加载,并且可以显示Markdown。在此示例中(取自上述网站),您需要获取并插入站点输出中的Markdown内容:
<!DOCTYPE html>
<html>
  <body>
    <textarea id="text-input" oninput="this.editor.update()"
              rows="6" cols="60">Insert proxied **Markdown** here.</textarea>
    <div id="preview"> </div>
    <script src="lib/markdown.js"></script>
    <script>
      function Editor(input, preview) {
        this.update = function () {
          preview.innerHTML = markdown.toHTML(input.value);
        };
        input.editor = this;
        this.update();
      }
      var $ = function (id) { return document.getElementById(id); };
      new Editor($("text-input"), $("preview"));
    </script>
  </body>
</html>

请注意,Markdown-js已不再维护。该Github页面重定向到其他项目:Markdown-It、Showdown、Marked。 - JM Lord

3

2

这里有一种更好的方法,似乎更符合问题要求,并且它确实适合我的需求。

这个方法采用了一个服务端的后端处理器,可以动态地将Markdown转换为HTML并提供给客户端。

以下是PHP的代码示例,但是其他语言也被支持并在链接中有文档说明:

PHP

  1. Download PHP Markdown (or PHP Markdown Extra) and PHP SmartyPants from Michel Fortin.

  2. Put markdown.php and smartypants.php somewhere in PHP's include path (or in the same directory as render.php).

  3. Add an alias in your Apache config:

Alias /markdown/ "/var/www/support/markdown/"

  1. Add rewrite rules. This can be done in the .htaccess file for a specific folder, or in the global Apache config. Some common extensions are included, but you can adjust them to your needs. (You might want to process all text as Markdown by adding "txt".)
# display Markdown as HTML by default 
RewriteEngine on 
RewriteRule .+\.(markdown|mdown|md|mkd)$ /markdown/render.php 
RewriteRule .+\.(markdown|mdown|md|mkd)\-text$ /markdown/render.php [L]

Firefox正在警告我该顶部链接存在安全风险。 - Rolv Apneseth
1
哦,看起来链接现在有问题了...我会去查找存档... - yourcomputergenius
1
链接已修复,现在可以在GitHub上找到。 - yourcomputergenius

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接