Sha256: ee0e13c67e8891d2baea924bf55f587205b594e91110c25c59acd09cc864487d

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module RenderEditorjs
  module Blocks
    # Compatible with default Paragraph and paragraph-with-aligment
    # https://github.com/kaaaaaaaaaaai/paragraph-with-alignment
    class Paragraph < Base
      DEFAULT_SAFE_TAGS = {
        "b" => nil,
        "i" => nil,
        "u" => ["class"],
        "del" => ["class"],
        "a" => ["href"],
        "mark" => ["class"],
        "code" => ["class"]
      }.freeze

      DEFAULT_OPTIONS = {
        tag: "p"
      }.freeze

      SCHEMA = YAML.safe_load(<<~YAML)
        type: object
        additionalProperties: false
        properties:
          text:
            type: string
          alignment:
            type: string
            enum:
              - left
              - center
              - right
      YAML

      attr_reader :options

      def initialize(options = DEFAULT_OPTIONS.dup)
        @options = options
        @options[:safe_tags] ||= DEFAULT_SAFE_TAGS
        super()
      end

      def render(data)
        return unless valid?(data)

        alignment = data["alignment"]
        css_class = alignment ? "align-#{alignment}" : nil
        content_tag(options[:tag], class: css_class) do
          sanitize(data["text"]).html_safe
        end
      end

      def sanitize(text)
        Sanitize.fragment(
          text,
          elements: options[:safe_tags].keys,
          attributes: options[:safe_tags].select { |_k, v| v },
          remove_contents: true
        )
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
render_editorjs-0.1.5 lib/render_editorjs/blocks/paragraph.rb
render_editorjs-0.1.4 lib/render_editorjs/blocks/paragraph.rb
render_editorjs-0.1.3 lib/render_editorjs/blocks/paragraph.rb