2

I've installed markdown package on Meteor:

meteor add markdown

And test it successfully:

<body>
  {{#markdown}}
     #Hello world!#
  {{/markdown}}
</body>

Ok!
Now I would like to import a markdown from a file and I had try in this way:

if (Meteor.isClient) {
  Session.set("markdown_data","MDFile.md");

  Template.myTemplate.helpers({
    markdown_data: function() {
      return Session.get("markdown_data");
    }
  });
}

And in html:

<body>
  {{#markdown}}{{{markdown_data}}}{{/markdown}}
</body>

But nothing appears, neither on webpage or in web-console or terminal.

Where I'm wrong?

Update - working Code

if (Meteor.isClient) {

  Markdown = new Mongo.Collection("markdown");
  
  Template.myTemplate.helpers({
    markdown_data: function() {
      var markdown = Markdown.findOne();
      return markdown && markdown.data;
    }
  });

 }

if (Meteor.isServer) {
    Markdown = new Mongo.Collection("markdown");
    Meteor.startup(function () {
      if(Markdown.find().count()===0){
        Markdown.insert({
               data: Assets.getText("teamProgramming.md")
        });
      }
  });
}
Community
  • 1
  • 1
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146

2 Answers2

3

It's not going to work this way, you should put your markdown file under the private directory, load it as an asset server-side and send it to the client using a collection :

private/MDFile.md

#Hello world!#

lib/collections/markdown.js

Markdown=new Mongo.Collection("markdown");

server/startup.js

Meteor.startup(function(){
  if(Markdown.find().count()===0){
    Markdown.insert({
      data: Assets.getText("MDFile.md");
    });
  }
});

server/collections/markdown.js

Meteor.publish(function(){
  return Markdown.find();
});

client/views/main.html

<body>
  {{#markdown}}
    {{markdownData}}
  {{/markdown}}
</body>

client/views/main.js

Template.body.helpers({
  markdownData:function(){
    var markdown=Markdown.findOne();
    return markdown && markdown.data;
  }
});
saimeunt
  • 22,666
  • 2
  • 56
  • 61
2

It is possible to do more elegantly without database. Just with usage of Meteor methods.

Suppose you have test.md markdown file in your /private folder

In server/methods.js

Meteor.methods({
    'getMarkdown'(markdownFile) {
        return Assets.getText(`markdownFiles/${markdownFile}`);
    }
});    

In client/helpers.js

Template.registerHelper('getMarkdown', (markdownFile) => {
    // if there is first no empty line in markdownFile
    // meteor's markdown helper renders <h1> as <pre>
    return '\n' + ReactiveMethod.call('getMarkdown', markdownFile);
});

To call meteor method from helper I used this package simple:reactive-method from this solution Is it a bad idea to call Meteor method from a helper? I hope not in this case (which simplifies loading of *md files)

Finally in some template of yours

<template name="loadMarkdown">
    {{#markdown}}
        {{getMarkdown 'test.md'}}
    {{/markdown}}
</template>
Jirik
  • 1,435
  • 11
  • 18
  • Just note the method described here will expose every file in the /private folder to a malicious client. – Thaum Rystra Aug 31 '22 at 09:12
  • 1
    @ThaumRystra thanks for your comment I added a directory prefix in `Assets.getText`. Now only this directory is accessible. Maybe there might be some param validation, with whitelisted markdown filenames. – Jirik Sep 01 '22 at 15:35