Sha256: 53cfa3d9afefc2d903615e2a329740960d87cc9b5099ff276fad7ba5b2429f42

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 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_OPTIONS = {
        tag: "p"
      }.freeze

      SAFE_TAGS = {
        "b" => nil,
        "i" => nil,
        "u" => ["class"],
        "del" => ["class"],
        "a" => ["href"],
        "mark" => ["class"],
        "code" => ["class"]
      }.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)
        @options = options
        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: SAFE_TAGS.keys,
          attributes: SAFE_TAGS.select { |_k, v| v },
          remove_contents: true
        )
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
render_editorjs-0.1.2 lib/render_editorjs/blocks/paragraph.rb
render_editorjs-0.1.1 lib/render_editorjs/blocks/paragraph.rb