Sha256: 837acac425a999ffd3490db52a3fbdd047e5638ddbe4115660de073d18a6b2cb

Contents?: true

Size: 1.37 KB

Versions: 18

Compression:

Stored size: 1.37 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() {
    this.easyMDE = new EasyMDE(this.#buildOptions())
    this.element.setAttribute("data-action", "turbo:morph-element->easymde#reconnect")
  }

  disconnect() {
    this.easyMDE.toTextArea()
    this.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

18 entries across 18 versions & 1 rubygems

Version Path
plutonium-0.19.8 src/js/controllers/easymde_controller.js
plutonium-0.19.7 src/js/controllers/easymde_controller.js
plutonium-0.19.6 src/js/controllers/easymde_controller.js
plutonium-0.19.5 src/js/controllers/easymde_controller.js
plutonium-0.19.4 src/js/controllers/easymde_controller.js
plutonium-0.19.3 src/js/controllers/easymde_controller.js
plutonium-0.19.2 src/js/controllers/easymde_controller.js
plutonium-0.19.1 src/js/controllers/easymde_controller.js
plutonium-0.19.0 src/js/controllers/easymde_controller.js
plutonium-0.18.8 src/js/controllers/easymde_controller.js
plutonium-0.18.7 src/js/controllers/easymde_controller.js
plutonium-0.18.6 src/js/controllers/easymde_controller.js
plutonium-0.18.5 src/js/controllers/easymde_controller.js
plutonium-0.18.4 src/js/controllers/easymde_controller.js
plutonium-0.18.3 src/js/controllers/easymde_controller.js
plutonium-0.18.2 src/js/controllers/easymde_controller.js
plutonium-0.18.1 src/js/controllers/easymde_controller.js
plutonium-0.18.0 src/js/controllers/easymde_controller.js