Skip to content
This repository was archived by the owner on May 8, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
74bec6e
Include the link entity as well
Jul 6, 2020
55fd200
Added image and video entities
Jul 8, 2020
34236cc
Change the embed video code to check for youtube watch links and conv…
May 12, 2021
17d25db
Added brackets to check the include part on video src
May 12, 2021
c1c80b5
Add the proper details for the iframe
May 13, 2021
da2f13a
Change the default image CSS styles for exports
Oct 26, 2021
cb9218f
Use target blank for links
Mar 4, 2022
07f397d
Added bonsailink as a new entity we handle when creating the HTML
Mar 29, 2022
70d9edf
Use a more defensive programming to default with fetch
Mar 29, 2022
1123f72
Apply the icon styling on FAQ generated HTMLs
Mar 29, 2022
46cfd9c
Apply the class on the span differently
Mar 30, 2022
c02a750
Added attachment link as an entity to the parser.
Apr 7, 2022
23b7f4d
USe the extension class as well on the generated html link
Apr 8, 2022
d56c2ad
Adding the table generator for draft.js
May 19, 2022
dd95ce8
Return the correct element data.
May 19, 2022
f39456c
Use the tableData element from the data attributes on TABLE
May 19, 2022
de8e6a9
Adding a wrapper div around the table component
May 26, 2022
d4d3ac8
Defensive programming in case the parsing finds an empty NIL value as…
Jul 6, 2022
00dbebc
Add HTTPS scheme if not present on a link
Oct 14, 2022
ed5b463
Use string interpolation instead
Oct 14, 2022
12c60e1
Do not add https for mailto addresses
Oct 14, 2022
0707ecd
Recognise email addresses without the mailto bit, prepend in order to…
Oct 17, 2022
e6dd383
Escape the URI when it comes to parsing it and turning it into HTML
Nov 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/draftjs_exporter/entities/attachmentlink.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true
module DraftjsExporter
module Entities
class AttachmentLink
attr_reader :configuration

def initialize(configuration = { className: nil })
@configuration = configuration
end

def call(parent_element, data)
# TODO add more detail about the link(s) and icons
args = { href: data.fetch(:data, {}).fetch(:url) }
args[:class] = configuration.fetch(:className) if configuration[:className]
args[:target] = "_blank"

element = parent_element.document.create_element('a', args)
# Add the icon part, depending on the article or
extension = data.fetch(:data, {}).fetch(:fileExtension, nil)
icon_args = {}
icon_args[:class] = "extension #{extension}"
icon_element = element.document.create_element('span', extension, icon_args)
element.add_child(icon_element)

# Then append the element as a whole
parent_element.add_child(element)
element
end
end
end
end
30 changes: 30 additions & 0 deletions lib/draftjs_exporter/entities/bonsailink.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true
module DraftjsExporter
module Entities
class Bonsailink
attr_reader :configuration

def initialize(configuration = { className: nil })
@configuration = configuration
end

def call(parent_element, data)
# TODO add more detail about the link(s) and icons
args = { href: data.fetch(:data, {}).fetch(:url) }
args[:class] = configuration.fetch(:className) if configuration[:className]

element = parent_element.document.create_element('a', args)
# Add the icon part, depending on the article or
style = data.fetch(:data, {}).fetch(:article_id, nil) ? "bonsai-content-article" : "bonsai-content-category"
icon_args = {}
icon_args[:class] = style
icon_element = element.document.create_element('span', icon_args)
element.add_child(icon_element)

# Then append the element as a whole
parent_element.add_child(element)
element
end
end
end
end
22 changes: 22 additions & 0 deletions lib/draftjs_exporter/entities/image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true
module DraftjsExporter
module Entities
class Image
attr_reader :configuration

def initialize(configuration = { className: nil })
# Default styles for images
@configuration = configuration.merge({style: "position: relative; cursor: default; max-width: 100%; height: auto;"})
end

def call(parent_element, data)
args = { src: data.fetch(:data, {}).fetch(:src) }
args[:class] = configuration.fetch(:className) if configuration[:className]

element = parent_element.document.create_element('img', args)
parent_element.add_child(element)
element
end
end
end
end
12 changes: 11 additions & 1 deletion lib/draftjs_exporter/entities/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ def initialize(configuration = { className: nil })
end

def call(parent_element, data)
args = { href: data.fetch(:data, {}).fetch(:url) }
url = data.fetch(:data, {}).fetch(:url)
if url.present? && !url.start_with?("mailto:") && !(%w{http https}.include?(URI.parse(URI.escape(url)).scheme))
if url =~ URI::MailTo::EMAIL_REGEXP
url = "mailto:#{url}"
else
url = "https://#{url}"
end
end

args = { href: url }
args[:class] = configuration.fetch(:className) if configuration[:className]
args[:target] = "_blank"

element = parent_element.document.create_element('a', args)
parent_element.add_child(element)
Expand Down
42 changes: 42 additions & 0 deletions lib/draftjs_exporter/entities/table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true
module DraftjsExporter
module Entities
class Table
attr_reader :configuration

def initialize(configuration = { className: nil, wrapperClassName: nil })
# Default styles for images
@configuration = configuration.merge({style: "position: relative; cursor: default; max-width: 100%; height: auto;"})
end

def call(parent_element, data)
tableData = data.fetch(:data, {}).fetch(:tableData, [])
args = {}
args[:class] = configuration.fetch(:className) if configuration[:className]

wrapperArgs = {}
wrapperArgs[:class] = configuration.fetch(:wrapperClassName) if configuration[:wrapperClassName]

# Create the wrapper <div>
wrapperElement = parent_element.document.create_element('div', wrapperArgs)
# Create the main <table>
tableElement = wrapperElement.document.create_element('table', args)
# Now, we need to add the rows and columns
tableData.each do |row|
# Create a <tr> for each row
trElement = tableElement.document.create_element('tr')
row.each do |cell|
# Add in the content, if styled, then apply styles
cellContent = cell
tdElement = trElement.document.create_element('td', cellContent)
trElement.add_child(tdElement)
end
tableElement.add_child(trElement)
end
wrapperElement.add_child(tableElement)
parent_element.add_child(wrapperElement)
wrapperElement
end
end
end
end
32 changes: 32 additions & 0 deletions lib/draftjs_exporter/entities/video.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true
module DraftjsExporter
module Entities
class Video
attr_reader :configuration

def initialize(configuration = { className: nil })
# Default styles for images
@configuration = configuration.merge({allowfullscreen: "1", frameborder: "0", allow: "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"})
end

def call(parent_element, data)
# Check for youtube links, and change watch to embed in order to embed correctly
src = data.fetch(:data, {}).fetch(:src)
if src && src.include?("https://www.youtube.com/watch")
video_id = src.split("=")[1]
src = "https://www.youtube.com/embed/#{video_id}"
end

args = { src: src}
args[:class] = configuration.fetch(:className) if configuration[:className]
args[:allowfullscreen] = configuration.fetch(:allowfullscreen) if configuration[:allowfullscreen]
args[:allow] = configuration.fetch(:allow) if configuration[:allow]
args[:frameborder] = configuration.fetch(:frameborder) if configuration[:frameborder]

element = parent_element.document.create_element('iframe', args)
parent_element.add_child(element)
element
end
end
end
end
6 changes: 6 additions & 0 deletions lib/draftjs_exporter/entity_state.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# frozen_string_literal: true
require 'draftjs_exporter/entities/link'
require 'draftjs_exporter/entities/bonsailink'
require 'draftjs_exporter/entities/attachmentlink'
require 'draftjs_exporter/entities/image'
require 'draftjs_exporter/entities/video'
require 'draftjs_exporter/entities/null'
require 'draftjs_exporter/entities/table'
require 'draftjs_exporter/error'

module DraftjsExporter
Expand Down
2 changes: 1 addition & 1 deletion lib/draftjs_exporter/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def block_contents(element, block, entity_map)
style_state.apply(command)
end

add_node(entity_state.current_parent, text, style_state)
add_node(entity_state.current_parent, text || "", style_state)
end
end

Expand Down