Customize {{#markdown}} behavior in Meteor

99封情书 提交于 2019-12-04 20:21:06

Why not implement your very own markdown helper:

// this is based on showdown package in spark branch
Handlebars.registerHelper('markdown', function (options) {
  return UI.block(function () {
    var self = this;
    return function () {
      var renderer = new marked.Renderer();
      var text = UI.toRawText(self.__content, self /* parentComponent */);
      return HTML.Raw(marked(trimIndentation(text), { renderer: renderer }));
    };
  });
});

where trimIndentation can look more or less like this:

function trimIndentation(text) {
  var regexp = null,
      result = "";
  return text.split('\n').map(function (line) {
    var match = (regexp || /(\s*)(\s*[^\s]+.*)/).exec(line);
    if (match) {
      !regexp && (regexp = new RegExp("(\\s{" + match[1].length + "})(.*)"));
      return match[2];
    }
    return line;
  }).join('\n');
}

In the above example I used the syntax of the new templating engine as well as I used the marked library instead of good old showdown (or should I say pagedown?), but you can of course do the same thing with a setup of your own choice.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!