template-integration.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. // Static URL Scheme Listing
  9. var urlschemes = [
  10. "aodroplink",
  11. "thunderlink",
  12. "cbthunderlink",
  13. "onenote",
  14. "file",
  15. "abasurl",
  16. "conisio",
  17. "mailspring"
  18. ];
  19. // Better would be a field in the admin backend to set this dynamically
  20. // instead of putting all known or wanted url schemes here hard into code
  21. // but i was not able to access those settings
  22. // var urlschemes = currentSetting.automaticLinkedUrlSchemes.split('\n');
  23. // put all url schemes into the linkify configuration to automatically make it clickable
  24. for(var i=0; i<urlschemes.length;i++){
  25. //console.log("adding autolink for "+urlschemes[i]);
  26. Markdown.linkify.add(urlschemes[i]+":",'http:');
  27. }
  28. // Additional safeAttrValue function to allow for other specific protocols
  29. // See https://github.com/leizongmin/js-xss/issues/52#issuecomment-241354114
  30. function mySafeAttrValue(tag, name, value, cssFilter) {
  31. // only when the tag is 'a' and attribute is 'href'
  32. // then use your custom function
  33. if (tag === 'a' && name === 'href') {
  34. // only filter the value if starts with an registered url scheme
  35. urlscheme = value.split(/:/);
  36. //console.log("validating "+urlscheme[0]);
  37. if(urlschemes.includes(urlscheme[0])) return value;
  38. else {
  39. // use the default safeAttrValue function to process all non cbthunderlinks
  40. return sanitizeXss.safeAttrValue(tag, name, value, cssFilter);
  41. }
  42. } else {
  43. // use the default safeAttrValue function to process it
  44. return sanitizeXss.safeAttrValue(tag, name, value, cssFilter);
  45. }
  46. };
  47. var emoji = require('markdown-it-emoji');
  48. Markdown.use(emoji);
  49. if (Package.ui) {
  50. const Template = Package.templating.Template;
  51. const UI = Package.ui.UI;
  52. const HTML = Package.htmljs.HTML;
  53. const Blaze = Package.blaze.Blaze; // implied by `ui`
  54. UI.registerHelper('markdown', new Template('markdown', function () {
  55. const self = this;
  56. let text = '';
  57. if (self.templateContentBlock) {
  58. text = Blaze._toText(self.templateContentBlock, HTML.TEXTMODE.STRING);
  59. }
  60. return HTML.Raw(sanitizeXss(Markdown.render(text), { safeAttrValue: mySafeAttrValue }));
  61. }));
  62. }