Parsing Fix: Add H2 Paragraph type

The post id 34dead42a28 contained a new paragraph type: H2. Previously
the only known header types were H3 and H4. In this case, the paragraph
doesn't actually get rendered because it's the page title which is
removed from the page nodes (see commits 6baba803 and then fba87c10).
However, it somehow an author is able to get an H2 paragraph into the
page, it will display as an <h1> just as H3 displays as <h2> and H4
displays as <h3>.
This commit is contained in:
Edward Loveall 2021-10-16 16:19:44 -04:00
parent eaf25ef23a
commit f7ad92f4bf
No known key found for this signature in database
GPG key ID: 789A4AE983AC8901
6 changed files with 38 additions and 0 deletions

View file

@ -190,6 +190,14 @@ describe ParagraphConverter do
it "converts all the tags" do
paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON
[
{
"text": "text",
"type": "H2",
"markups": [],
"iframe": null,
"layout": null,
"metadata": null
},
{
"text": "text",
"type": "H3",
@ -303,6 +311,7 @@ describe ParagraphConverter do
]
JSON
expected = [
Heading1.new([Text.new("text")] of Child),
Heading2.new([Text.new("text")] of Child),
Heading3.new([Text.new("text")] of Child),
Paragraph.new([Text.new("text")] of Child),

View file

@ -162,6 +162,23 @@ describe PageContent do
HTML
end
it "renders an H2" do
page = Page.new(
title: "Title",
author: user_anchor_factory,
created_at: Time.local,
nodes: [
Heading1.new(children: [
Text.new(content: "Title!"),
] of Child),
] of Child
)
html = PageContent.new(page: page).render_to_string
html.should eq %(<h1>Title!</h1>)
end
it "renders an H3" do
page = Page.new(
title: "Title",

View file

@ -10,6 +10,10 @@ class ParagraphConverter
paragraph = paragraphs.shift
children = MarkupConverter.convert(paragraph.text, paragraph.markups)
node = BlockQuote.new(children: children)
when PostResponse::ParagraphType::H2
paragraph = paragraphs.shift
children = MarkupConverter.convert(paragraph.text, paragraph.markups)
node = Heading1.new(children: children)
when PostResponse::ParagraphType::H3
paragraph = paragraphs.shift
children = MarkupConverter.convert(paragraph.text, paragraph.markups)

View file

@ -81,6 +81,10 @@ class PageContent < BaseComponent
script src: child.src
end
def render_child(node : Heading1)
h1 { render_children(node.children) }
end
def render_child(node : Heading2)
h2 { render_children(node.children) }
end

View file

@ -40,6 +40,9 @@ module Nodes
class FigureCaption < Container
end
class Heading1 < Container
end
class Heading2 < Container
end

View file

@ -42,6 +42,7 @@ class PostResponse
enum ParagraphType
BQ
H2
H3
H4
IFRAME