How to get the local markdown content synchronously?


#1
Template.docItem.helpers({
	doc: function() {
		var docId = FlowRouter.getParam('docId');
		var doc = Docs.findOne({_id: docId}) || {};
		return doc;
	},
	md_content: function() {
		var docId = FlowRouter.getParam('docId');
		var doc = Docs.findOne({_id: docId}) || {};
		var file_name = doc.md_path;
		var content = Meteor.call("getMarkdownContent", file_name);
		return content;
	}
});
Meteor.methods({
	getMarkdownContent: function(file_name) {
		check(file_name, String);
		var base_path = "documentation/";
		var file_path = base_path + file_name;
		var content = Assets.getText(file_path);
		return content;
	}
})
<template name="docItem">
  {{#markdown}}{{md_content}}{{/markdown}}
</template>

The content is always undefined. And how to let client side “lazy load” the local mardown files? Thanks!


#2

put your code from your md_content helper into template.docItem.oncreated

and in the meteor call, use the callback to set a Session.var (or reactive var)

Meteor.call("getMarkdownContent", function(err, res){
   if (err) return null // do something handle any errors

   Session.set('md_content', res);
});

and then, in the helper, return Session.get('md_content')


#3
Template.docItem.onCreated(function() {
  var self = this;
  // Subscribe
  self.autorun(function() {
    var docId = FlowRouter.getParam('docId');
    self.subscribe('docById', docId);
  });

  // Get markdown
  self.md_content = new ReactiveVar("##loading...");

  var file_name = "1._product_features.md";
  console.log(file_name);

  Meteor.call("getMarkdownContent", file_name, function(err, res) {
	if (!err) {
		// console.log(res);
		self.md_content.set(res);
	} else {
		console.log(err);
		self.md_content.set("##loading failure!");
	};
  });
});
Template.docItem.helpers({
	doc: function() {
		var docId = FlowRouter.getParam('docId');
		var doc = Docs.findOne({_id: docId}) || {};
		return doc;
	},

	md_content: function() {
		return Template.instance().md_content.get();
	}
});

I got this right by setting the hard code file name. How can I get the filename with less redundant code? Thanks a lot!


#4
Template.docItem.onCreated(function() {
  var self = this;
  var docId = "";

  // Subscribe
  self.autorun(function() {
    docId = FlowRouter.getParam('docId');
    self.subscribe('docById', docId);
  });

  // Get markdown
  self.md_content = new ReactiveVar("##loading...##");

  Meteor.call("getMarkdownContent", docId, function(err, res) {
	if (!err) {
		self.md_content.set(res);
	} else {
		console.log(err);
		self.md_content.set("##loading failure!##");
	};
  });
});

Get almost everything right, thanks!