05c18f6451
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.
19 lines
645 B
Crystal
19 lines
645 B
Crystal
class PageConverter
|
|
def convert(paragraphs : Array(PostResponse::Paragraph)) : Page
|
|
first_two_paragraphs = paragraphs.first(2)
|
|
first_two_types = first_two_paragraphs.map(&.type)
|
|
if first_two_types == [PostResponse::ParagraphType::H3, PostResponse::ParagraphType::H4]
|
|
Page.new(
|
|
title: first_two_paragraphs[0].text,
|
|
subtitle: first_two_paragraphs[1].text,
|
|
nodes: ParagraphConverter.new.convert(paragraphs[2..]),
|
|
)
|
|
else
|
|
Page.new(
|
|
title: first_two_paragraphs[0].text,
|
|
subtitle: nil,
|
|
nodes: ParagraphConverter.new.convert(paragraphs[1..]),
|
|
)
|
|
end
|
|
end
|
|
end
|