template-integration.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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) || /^cbthunderlink:/ig.test(value) || /^aodroplink:/ig.test(value)) {
  16. return value;
  17. }
  18. else {
  19. // use the default safeAttrValue function to process all non cbthunderlinks
  20. return sanitizeXss.safeAttrValue(tag, name, value, cssFilter);
  21. }
  22. } else {
  23. // use the default safeAttrValue function to process it
  24. return sanitizeXss.safeAttrValue(tag, name, value, cssFilter);
  25. }
  26. };
  27. var emoji = require('markdown-it-emoji');
  28. Markdown.use(emoji);
  29. if (Package.ui) {
  30. const Template = Package.templating.Template;
  31. const UI = Package.ui.UI;
  32. const HTML = Package.htmljs.HTML;
  33. const Blaze = Package.blaze.Blaze; // implied by `ui`
  34. UI.registerHelper('markdown', new Template('markdown', function () {
  35. const self = this;
  36. let text = '';
  37. if (self.templateContentBlock) {
  38. text = Blaze._toText(self.templateContentBlock, HTML.TEXTMODE.STRING);
  39. }
  40. return HTML.Raw(sanitizeXss(Markdown.render(text), { safeAttrValue: mySafeAttrValue }));
  41. }));
  42. }