InputEditorPreview.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <div class='input_editor_preview__markdownHTML'>
  3. <!--
  4. A hack to call the getHTML() function, not sure why
  5. the value watcher function is not working, as it does
  6. in the ThreadNew page
  7. !-->
  8. <div style='display: none;'>{{valueWatch}}</div>
  9. <div v-html='HTML' style='margin-top: -0.5rem;'></div>
  10. <div v-if='!value.trim().length' class='input_editor_preview__markdownHTML--empty'>
  11. Nothing to preview
  12. </div>
  13. </div>
  14. </template>
  15. <script>
  16. import throttle from 'lodash.throttle'
  17. import Marked from 'marked'
  18. Marked.setOptions({
  19. highlight: function (code) {
  20. return require('highlight.js').highlightAuto(code).value;
  21. },
  22. sanitize: true
  23. });
  24. export default {
  25. name: 'InputEditorPreview',
  26. props: ['value', 'mentions'],
  27. data () {
  28. return {
  29. HTML: ''
  30. }
  31. },
  32. watch: {
  33. value: 'getHTML'
  34. },
  35. computed: {
  36. valueWatch () {
  37. this.getHTML();
  38. return '';
  39. }
  40. },
  41. methods: {
  42. getHTML () {
  43. let replacedMd = this.value;
  44. (this.mentions || []).forEach(mention => {
  45. let regexp = new RegExp('(^|\\s)@' + mention + '($|\\s)')
  46. replacedMd = replacedMd.replace(regexp, `$1[@${mention}](/user/${mention})$2`)
  47. })
  48. let HTML = Marked(replacedMd);
  49. this.HTML = HTML;
  50. this.$linkExpander(HTML, v => this.HTML = v);
  51. }
  52. }
  53. }
  54. </script>
  55. <style lang='scss' scoped>
  56. @import '../assets/scss/variables.scss';
  57. .input_editor_preview {
  58. @at-root #{&}__markdownHTML {
  59. height: 8.2rem;
  60. overflow: auto;
  61. word-break: break-word;
  62. padding: 0.5rem;
  63. @at-root #{&}--empty {
  64. @include text($font--role-emphasis, 1rem);
  65. display: flex;
  66. margin-top: 0.5rem;
  67. align-content: center;
  68. @include user-select(none);
  69. cursor: default;
  70. color: $color__gray--darker;
  71. }
  72. }
  73. }
  74. </style>