Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Markdown to HTML from GitHub README.md file

My objective is to,

1) Read the Markdown source from README.md file from my GitHub repo.
2) Convert the Markdown into HTML code.
3) Apply the output HTML to a <div>.

only using Client-side technologies.

I know how to convert Markdown code to HTML using jQuery but don't know how to dynamically read the MD source from README.md file from GitHub repo.

like image 422
Pankaj Parashar Avatar asked Mar 27 '13 07:03

Pankaj Parashar


People also ask

How does GitHub convert Markdown to HTML?

Markdown is converted to HTML by the comrak crate. The default stylesheet (the CSS file) is from sindresorhus/github-markdown-css. If ``` is used in the input Markdown file, the highlight. js will be automatically embedded in the output HTML file.

How do I convert Markdown to HTML?

First, open the Command Palette by pressing CTRL+SHIFT+P . Then, start typing in “Markdown” and click on the Markdown All in One: Print current document to HTML command. The image below shows how the HTML formatting looks like after exporting the Markdown document.

Does readme Md support HTML?

Even though GitHub Readme files (typically ./readme.md ) are Markdown, and although Markdown supports HTML, you can't put <style> or <script> tags init. (Well, you can, they just get stripped.) So you can't apply custom styles there.

Does GitHub Markdown support HTML?

You can either use the syntactic sugar that GFM (or other GitHub-supported markup language you're using) provides or, since Markdown can contain raw HTML, you can enter the HTML tags manually.


3 Answers

  1. Get the raw file url of the README.md file from the GitHub repo.
  2. Format the url in the following format, http://markdown.io/<url_readme_file>. Let's say the url of my readme.md file is http://raw.github.com/pankajparashar/nerdy-css/master/README.md. Hence, the new url becomes http://markdown.io/http://raw.github.com/pankajparashar/nerdy-css/master/README.md
  3. Now use jQuery's load method using this new url and extract the content from #md-content id of the page.

Complete demo of the above steps available here - non-working link removed

like image 137
Pankaj Parashar Avatar answered Sep 25 '22 23:09

Pankaj Parashar


  1. Go to your GitHub project page
  2. Click on your README.md file
  3. Now click on the Raw button

The raw file is the actual URL you'll want to load.

Create a PHP (or whatever server-side language you are using) file called load.php specifically for loading remote files (like your RAW file). The PHP script will accept a $_GET['url'] variable. Pass the variable to file_get_contents(), and output the results. Please note, the code below is an extremely simple example.

echo file_get_contents($_GET['url']);

Now, just use the load function in jQuery to bring in the data contents from the PHP file. Your URL will probably be formatted similar to this...

.load("load.php?url=https://raw.github.com/user/project/master/README.md")

Finally, use whatever means you already described to convert to Markdown.

  • PHP file_get_contents(): http://php.net/manual/en/function.file-get-contents.php
  • jQuery load(): http://api.jquery.com/load/
  • More info about bypassing the Same-origin policy: http://www.mikazo.com/2010/09/how-to-getting-around-ajax-same-origin.html
like image 42
Mike McLin Avatar answered Sep 25 '22 23:09

Mike McLin


You should check out:

https://github.com/zmckinnon/jquery-gh-readme

It uses the GitHub API to retrieve the contents of the readme using this call:

GET /repos/:owner/:repo/readme

Then it converts the base64 encrypted content.

Then it converts the markdown content to html using marked.

like image 39
zmckinnon Avatar answered Sep 22 '22 23:09

zmckinnon