template-integration.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import sanitizeXss from 'xss';
  2. var Markdown = require('markdown-it')({
  3. html: true,
  4. linkify: true,
  5. typographer: true,
  6. breaks: true,
  7. });
  8. // Additional safeAttrValue function to allow for other specific protocols
  9. // See https://github.com/leizongmin/js-xss/issues/52#issuecomment-241354114
  10. function mySafeAttrValue(tag, name, value, cssFilter) {
  11. // only when the tag is 'a' and attribute is 'href'
  12. // then use your custom function
  13. if (tag === 'a' && name === 'href') {
  14. // only filter the value if starts with 'cbthunderlink:' or 'aodroplink'
  15. if (/^thunderlink:/ig.test(value) ||
  16. /^cbthunderlink:/ig.test(value) ||
  17. /^aodroplink:/ig.test(value) ||
  18. /^onenote:/ig.test(value) ||
  19. /^file:/ig.test(value) ||
  20. /^mailspring:/ig.test(value)) {
  21. return value;
  22. }
  23. else {
  24. // use the default safeAttrValue function to process all non cbthunderlinks
  25. return sanitizeXss.safeAttrValue(tag, name, value, cssFilter);
  26. }
  27. } else {
  28. // use the default safeAttrValue function to process it
  29. return sanitizeXss.safeAttrValue(tag, name, value, cssFilter);
  30. }
  31. };
  32. var emoji = require('markdown-it-emoji');
  33. Markdown.use(emoji);
  34. if (Package.ui) {
  35. const Template = Package.templating.Template;
  36. const UI = Package.ui.UI;
  37. const HTML = Package.htmljs.HTML;
  38. const Blaze = Package.blaze.Blaze; // implied by `ui`
  39. UI.registerHelper('markdown', new Template('markdown', function () {
  40. const self = this;
  41. let text = '';
  42. if (self.templateContentBlock) {
  43. text = Blaze._toText(self.templateContentBlock, HTML.TEXTMODE.STRING);
  44. }
  45. return HTML.Raw(sanitizeXss(Markdown.render(text), { safeAttrValue: mySafeAttrValue }));
  46. }));
  47. }