Include markdown file contents in HTML for remark.js
Asked Answered
F

1

10

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).
Fraise answered 12/1, 2017 at 7:27 Comment(0)
L
5

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>
Lydgate answered 12/5, 2017 at 16:32 Comment(2)
Good, btu it require viewing from web server. Local view not work.Falcate
Yep, it is still working as of today, even though the remarkjs wiki is no longer having such tip.Electoral

© 2022 - 2024 — McMap. All rights reserved.