Reputation: 21510
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?
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")
});
}
});
}
Upvotes: 2
Views: 571
Reputation: 1502
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>
Upvotes: 2
Reputation: 22696
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;
}
});
Upvotes: 3