Extract tile and subtitle from initial paragraphs

Medium guides each post to have a Title and Subtitle. They are rendered
as the first two paragraphs: H3 and H4 respectively. If they exist, a
new PageConverter class extracts them and sets them on the page.

However, they aren't required. If the first two paragraphs aren't H3
and H4, the PageConverter falls back to using the first paragraph as
the title, and setting the subtitle to blank.

The remaining paragraphs are passed into the ParagraphConverter as
normal.
This commit is contained in:
Edward Loveall 2021-08-14 17:36:10 -04:00
parent f48f7c2932
commit 05c18f6451
No known key found for this signature in database
GPG key ID: 789A4AE983AC8901
5 changed files with 288 additions and 99 deletions
spec/components

View file

@ -4,11 +4,15 @@ include Nodes
describe PageContent do
it "renders a single parent/child node structure" do
page = Page.new(nodes: [
Paragraph.new(children: [
Text.new(content: "hi"),
] of Child),
] of Child)
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
Paragraph.new(children: [
Text.new(content: "hi"),
] of Child),
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -16,22 +20,26 @@ describe PageContent do
end
it "renders multiple childrens" do
page = Page.new(nodes: [
Paragraph.new(children: [
Text.new(content: "Hello, "),
Emphasis.new(children: [
Text.new(content: "World!"),
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
Paragraph.new(children: [
Text.new(content: "Hello, "),
Emphasis.new(children: [
Text.new(content: "World!"),
] of Child),
] of Child),
] of Child),
UnorderedList.new(children: [
ListItem.new(children: [
Text.new(content: "List!"),
UnorderedList.new(children: [
ListItem.new(children: [
Text.new(content: "List!"),
] of Child),
ListItem.new(children: [
Text.new(content: "Again!"),
] of Child),
] of Child),
ListItem.new(children: [
Text.new(content: "Again!"),
] of Child),
] of Child),
] of Child)
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -39,9 +47,13 @@ describe PageContent do
end
it "renders an anchor" do
page = Page.new(nodes: [
Anchor.new(children: [Text.new("link")] of Child, href: "https://example.com"),
] of Child)
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
Anchor.new(children: [Text.new("link")] of Child, href: "https://example.com"),
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -49,11 +61,15 @@ describe PageContent do
end
it "renders a blockquote" do
page = Page.new(nodes: [
BlockQuote.new(children: [
Text.new("Wayne Gretzky. Michael Scott."),
] of Child),
] of Child)
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
BlockQuote.new(children: [
Text.new("Wayne Gretzky. Michael Scott."),
] of Child),
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -61,11 +77,15 @@ describe PageContent do
end
it "renders code" do
page = Page.new(nodes: [
Code.new(children: [
Text.new("foo = bar"),
] of Child),
] of Child)
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
Code.new(children: [
Text.new("foo = bar"),
] of Child),
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -73,14 +93,18 @@ describe PageContent do
end
it "renders empasis" do
page = Page.new(nodes: [
Paragraph.new(children: [
Text.new(content: "This is "),
Emphasis.new(children: [
Text.new(content: "neat!"),
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
Paragraph.new(children: [
Text.new(content: "This is "),
Emphasis.new(children: [
Text.new(content: "neat!"),
] of Child),
] of Child),
] of Child),
] of Child)
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -88,14 +112,18 @@ describe PageContent do
end
it "renders a figure and figure caption" do
page = Page.new(nodes: [
Figure.new(children: [
Image.new(src: "image.png", originalWidth: 100, originalHeight: 200),
FigureCaption.new(children: [
Text.new("A caption"),
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
Figure.new(children: [
Image.new(src: "image.png", originalWidth: 100, originalHeight: 200),
FigureCaption.new(children: [
Text.new("A caption"),
] of Child),
] of Child),
] of Child),
] of Child)
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -108,11 +136,15 @@ describe PageContent do
end
it "renders an H3" do
page = Page.new(nodes: [
Heading2.new(children: [
Text.new(content: "Title!"),
] of Child),
] of Child)
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
Heading2.new(children: [
Text.new(content: "Title!"),
] of Child),
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -120,11 +152,15 @@ describe PageContent do
end
it "renders an H4" do
page = Page.new(nodes: [
Heading3.new(children: [
Text.new(content: "In Conclusion..."),
] of Child),
] of Child)
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
Heading3.new(children: [
Text.new(content: "In Conclusion..."),
] of Child),
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -132,11 +168,15 @@ describe PageContent do
end
it "renders an image" do
page = Page.new(nodes: [
Paragraph.new(children: [
Image.new(src: "image.png", originalWidth: 100, originalHeight: 200),
] of Child),
] of Child)
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
Paragraph.new(children: [
Image.new(src: "image.png", originalWidth: 100, originalHeight: 200),
] of Child),
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -148,11 +188,15 @@ describe PageContent do
end
it "renders an iframe container" do
page = Page.new(nodes: [
Paragraph.new(children: [
IFrame.new(href: "https://example.com"),
] of Child),
] of Child)
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
Paragraph.new(children: [
IFrame.new(href: "https://example.com"),
] of Child),
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -166,12 +210,16 @@ describe PageContent do
end
it "renders an ordered list" do
page = Page.new(nodes: [
OrderedList.new(children: [
ListItem.new(children: [Text.new("One")] of Child),
ListItem.new(children: [Text.new("Two")] of Child),
] of Child),
] of Child)
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
OrderedList.new(children: [
ListItem.new(children: [Text.new("One")] of Child),
ListItem.new(children: [Text.new("Two")] of Child),
] of Child),
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -179,11 +227,15 @@ describe PageContent do
end
it "renders an preformatted text" do
page = Page.new(nodes: [
Paragraph.new(children: [
Text.new("Hello, world!"),
] of Child),
] of Child)
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
Paragraph.new(children: [
Text.new("Hello, world!"),
] of Child),
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -191,11 +243,15 @@ describe PageContent do
end
it "renders an preformatted text" do
page = Page.new(nodes: [
Preformatted.new(children: [
Text.new("New\nline"),
] of Child),
] of Child)
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
Preformatted.new(children: [
Text.new("New\nline"),
] of Child),
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -203,11 +259,15 @@ describe PageContent do
end
it "renders strong text" do
page = Page.new(nodes: [
Strong.new(children: [
Text.new("Oh yeah!"),
] of Child),
] of Child)
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
Strong.new(children: [
Text.new("Oh yeah!"),
] of Child),
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -215,12 +275,16 @@ describe PageContent do
end
it "renders an unordered list" do
page = Page.new(nodes: [
UnorderedList.new(children: [
ListItem.new(children: [Text.new("Apple")] of Child),
ListItem.new(children: [Text.new("Banana")] of Child),
] of Child),
] of Child)
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
UnorderedList.new(children: [
ListItem.new(children: [Text.new("Apple")] of Child),
ListItem.new(children: [Text.new("Banana")] of Child),
] of Child),
] of Child
)
html = PageContent.new(page: page).render_to_string
@ -228,9 +292,13 @@ describe PageContent do
end
it "renders a user anchor" do
page = Page.new(nodes: [
UserAnchor.new(children: [Text.new("Some User")] of Child, userId: "abc123"),
] of Child)
page = Page.new(
title: "Title",
subtitle: nil,
nodes: [
UserAnchor.new(children: [Text.new("Some User")] of Child, userId: "abc123"),
] of Child
)
html = PageContent.new(page: page).render_to_string