Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include markdown file contents in HTML for remark.js

Tags:

html

remarkjs

Is there a (preferably light-weight way) to include the raw contents of a markdown file in HTML?

I'm using remark.js to create a slideshow and I'd like to keep the markdown file separate from the HTML, so that the HTML is very simple (and does not change):

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Test</title>
    <link rel="stylesheet" href="style.css">
    <script src="https://gnab.github.io/remark/downloads/remark-latest.min.js"></script>
  </head>
  <body>
    <textarea id="source">
      >>'paste' markdown file test.md<<
    </textarea>
      <script>
        var slideshow = remark.create({
          highlightStyle: 'darkula',
          highlightLines: true
        });
      </script>
  </body>
</html>

Ideally this runs offline and on a local machine (without running a web server). The bit with 'paste' markdown file test.md' obviously does not work (hence my question). I've tried:

  • object data="test.md" but that generates an ugly iframe
  • This solution but I just got an empty page (I used a CDN for jQuery).
like image 657
Ian Avatar asked Jan 12 '17 07:01

Ian


1 Answers

According to remarkjs wiki you need to add below lines to include your markdown file

var slideshow = remark.create({
  sourceUrl: 'markdown.md'
});

Below is a complete example

<!DOCTYPE html>
<html>
  <head>
    <title>A title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <style type="text/css">
      @import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
      @import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
      @import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);

      body { font-family: 'Droid Serif'; }
      h1, h2, h3 {
        font-family: 'Yanone Kaffeesatz';
        font-weight: normal;
      }
      .remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
    </style>
  </head>
  <body>
    <script src="https://remarkjs.com/downloads/remark-latest.min.js" type="text/javascript">
    </script>
    <script type="text/javascript">
      var slideshow = remark.create({
      sourceUrl: 'your_file.md'
      });
    </script>
  </body>
</html>
like image 56
hungptit Avatar answered Sep 18 '22 16:09

hungptit