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.
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.
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.
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.
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.
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
#md-content
id of the page.Complete demo of the above steps available here - non-working link removed
README.md
fileThe 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.
file_get_contents()
: http://php.net/manual/en/function.file-get-contents.php
load()
: http://api.jquery.com/load/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With