Sha256: 9d3a7cfb54448ae1fd1965a9b19ac2899884d79b83135dbaa9b58115851045a5

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module RenderEditorjs
  module Blocks
    class Paragraph < Base
      DEFAULT_OPTIONS = {
        tag: "p"
      }.freeze

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

      attr_reader :options

      def initialize(options = DEFAULT_OPTIONS)
        @options = options
        super()
      end

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

        alignment = data["alignment"]
        class_name_str = ""
        if alignment.present?
          class_name_str = [
            class_name_str,
            css_name("__#{alignment}")
          ].join(" ")
        end

        content_tag(options[:tag], class: class_name_str.presence) do
          sanitize(data["text"]).html_safe
        end
      end

      def safe_tags
        {
          "b" => nil,
          "i" => nil,
          "u" => ["class"],
          "del" => ["class"],
          "a" => ["href"],
          "mark" => ["class"],
          "code" => ["class"]
        }
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
render_editorjs-0.1.0 lib/render_editorjs/blocks/paragraph.rb