Sha256: c36e89b0d7c3e558feacfb75918befdccdd83175939cdbcedfd9a5ac6dea27c2

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

import { Controller } from "@hotwired/stimulus"
import DOMPurify from 'dompurify';
import { marked } from 'marked';

// Connects to data-controller="easymde"
export default class extends Controller {
  connect() {
    console.log(`easymde connected: ${this.element}`)
    self.easyMDE = new EasyMDE(this.#buildOptions())
    this.element.setAttribute("data-action", "turbo:morph-element->easymde#reconnect")
  }

  disconnect() {
    self.easyMDE.toTextArea()
    self.easyMDE = null
  }

  reconnect() {
    this.disconnect()
    this.connect()
  }

  #buildOptions() {
    let options = {
      element: this.element,
      promptURLs: true,
      spellChecker: false,
      // Override the default preview renderer
      previewRender: (plainText) => {
        // First sanitize the input to remove any undesired HTML
        const cleanedText = DOMPurify.sanitize(plainText, {
          ALLOWED_TAGS: ['strong', 'em', 'sub', 'sup', 'details', 'summary'],
          ALLOWED_ATTR: []
        });

        // Then convert markdown to HTML
        const cleanedHTML = marked(cleanedText);

        // Finally, another pass, since marked does not sanitize html
        return DOMPurify.sanitize(cleanedHTML, { USE_PROFILES: { html: true } })
      }
    }
    if (this.element.attributes.id.value) {
      options.autosave = {
        enabled: true,
        uniqueId: this.element.attributes.id.value,
        delay: 1000,
      }
    }
    return options
  }
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
plutonium-0.16.0 src/js/controllers/easymde_controller.js
plutonium-0.15.24 src/js/controllers/easymde_controller.js