Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include markdown (.md) files inside html files

Suppose I have a README.md file in a github repository. I am then making a website all about this repository that I will host using gh-pages.

What I want is to have a section of my index.html file that gets its content from my README.md file. This way, only one file needs to be updated.

I imagine that the markdown file will first need to be converted to html, and that html can then be put into another html file.

I have looked into HTML5 Imports, but they are only currently supported in Chrome. Using a separate .js file with document.write() could be useful, but is there a simple, clean way?

like image 698
gliemezis Avatar asked Jun 12 '16 04:06

gliemezis


People also ask

How do you integrate a Markdown in HTML?

document. createElement("markdown"); var md_tags = document. getElementsByTagName("markdown"); // Returns array of all markdown tags. for (var i = 0; i < md_tags.

How do I embed Markdown?

Markdown supports HTML, so if you need to, say, embed a YouTube video, you can just copy and paste the embed code from them, drop it into a Markdown document, and you should be good to go.

How do you display Markdown in browser?

To do that, right-click the extension icon in the in the toolbar and then select Manage Extensions. Scroll down until you see Allow Access to File URLs and toggle the switch to the On position. Now that Markdown Preview Plus is fully set up, you can drag any Markdown-formatted file into the browser to preview it.


4 Answers

I am using <zero-md> web component.

<!-- Lightweight client-side loader that feature-detects and load polyfills only when necessary -->
<script src="https://cdn.jsdelivr.net/npm/@webcomponents/webcomponentsjs@2/webcomponents-loader.min.js"></script>

<!-- Load the element definition -->
<script type="module" src="https://cdn.jsdelivr.net/gh/zerodevx/zero-md@1/src/zero-md.min.js"></script>

<!-- Simply set the `src` attribute to your MD file and win -->
<zero-md src="README.md"></zero-md>
like image 50
Constantin De La Roche Avatar answered Oct 02 '22 13:10

Constantin De La Roche


My complete answer using Node.js

1. First install the marked markdown converter:

$ npm install --save-dev marked


2. Then in a new file called generateReadMe.js, compile the markdown to HTML and write it to a new README.html file:

var marked = require('marked');
var fs = require('fs');

var readMe = fs.readFileSync('README.md', 'utf-8');
var markdownReadMe = marked(readMe);

fs.writeFileSync('./site/README.html', markdownReadMe);


3. Then inside the index.html where the README.md content is wanted, add an <object> tag:

<object data="README.html" type="text/html"></object>


4. Then run this on the command line to make it happen:

$ node path/to/generateReadMe.js


The whole process was pretty simple and painless. I added the whole thing to my npm start script. Now any time I make a change to my README.md file, the changes will register on my gh-pages website.

like image 41
gliemezis Avatar answered Oct 02 '22 13:10

gliemezis


You could use a markdown parser such as https://github.com/markdown-it/markdown-it to convert the markdown to html.

You could either convert the markdown on the server and merge it into the HTML delivered to the client, or use it to load the markdown in the browser and convert it there.

like image 25
Sudsy Avatar answered Oct 02 '22 14:10

Sudsy


To convert markdown to html, you can use a conversion library or a command tool. For an example using the Ruby language, visit: https://github.com/github/markup.

Try searching for an appropriate conversion library or command tool for your implementation by visiting: https://www.npmjs.com/search?q=markup. The accepted answer above is an example using the NPM package manager for Node.js.

like image 35
oklas Avatar answered Oct 02 '22 12:10

oklas