template-integration.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import DOMPurify from 'dompurify';
  2. var Markdown = require('markdown-it')({
  3. html: true,
  4. linkify: true,
  5. typographer: true,
  6. breaks: true,
  7. });
  8. //import markdownItMermaid from "@wekanteam/markdown-it-mermaid";
  9. // Static URL Scheme Listing
  10. var urlschemes = [
  11. "aodroplink",
  12. "thunderlink",
  13. "cbthunderlink",
  14. "onenote",
  15. "file",
  16. "abasurl",
  17. "conisio",
  18. "mailspring"
  19. ];
  20. // Better would be a field in the admin backend to set this dynamically
  21. // instead of putting all known or wanted url schemes here hard into code
  22. // but i was not able to access those settings
  23. // var urlschemes = currentSetting.automaticLinkedUrlSchemes.split('\n');
  24. // put all url schemes into the linkify configuration to automatically make it clickable
  25. for(var i=0; i<urlschemes.length;i++){
  26. Markdown.linkify.add(urlschemes[i]+":",'http:');
  27. }
  28. var emoji = require('markdown-it-emoji');
  29. Markdown.use(emoji);
  30. var mathjax = require('markdown-it-mathjax3');
  31. Markdown.use(mathjax);
  32. // Try to fix Mermaid Diagram error: Maximum call stack size exceeded.
  33. // Added bigger text size for Diagram.
  34. // https://github.com/wekan/wekan/issues/4251
  35. // https://stackoverflow.com/questions/66825888/maximum-text-size-in-diagram-exceeded-mermaid-js
  36. // https://github.com/mermaid-js/mermaid/blob/74b1219d62dd76d98d60abeeb36d4520f64faceb/src/defaultConfig.js#L39
  37. // https://github.com/wekan/cli-table3
  38. // https://www.npmjs.com/package/@wekanteam/markdown-it-mermaid
  39. // https://github.com/wekan/markdown-it-mermaid
  40. //Markdown.use(markdownItMermaid,{
  41. // maxTextSize: 200000,
  42. //});
  43. if (Package.ui) {
  44. const Template = Package.templating.Template;
  45. const UI = Package.ui.UI;
  46. const HTML = Package.htmljs.HTML;
  47. const Blaze = Package.blaze.Blaze; // implied by `ui`
  48. UI.registerHelper('markdown', new Template('markdown', function () {
  49. const self = this;
  50. let text = '';
  51. if (self.templateContentBlock) {
  52. text = Blaze._toText(self.templateContentBlock, HTML.TEXTMODE.STRING);
  53. }
  54. if (text.includes("[]") !== false) {
  55. // Prevent hiding info: https://wekan.github.io/hall-of-fame/invisiblebleed/
  56. // If markdown link does not have description, do not render markdown, instead show all of markdown source code using preformatted text.
  57. // Also show html comments.
  58. return HTML.Raw('<pre style="background-color: red;" title="Warning! Hidden markdown link description!" aria-label="Warning! Hidden markdown link description!">' + DOMPurify.sanitize(text.replace('<!--', '&lt;!--').replace('-->', '--&gt;')) + '</pre>');
  59. } else {
  60. // Prevent hiding info: https://wekan.github.io/hall-of-fame/invisiblebleed/
  61. // If text does not have hidden markdown link, render all markdown.
  62. // Also show html comments.
  63. return HTML.Raw(DOMPurify.sanitize(Markdown.render(text).replace('<!--', '<font color="red" title="Warning! Hidden HTML comment!" aria-label="Warning! Hidden HTML comment!">&lt;!--</font>').replace('-->', '<font color="red" title="Warning! Hidden HTML comment!" aria-label="Warning! Hidden HTML comment!">--&gt;</font>'), {ALLOW_UNKNOWN_PROTOCOLS: true}));
  64. }
  65. }));
  66. }