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:
parent
f48f7c2932
commit
05c18f6451
5 changed files with 288 additions and 99 deletions
spec/classes
97
spec/classes/page_converter_spec.cr
Normal file
97
spec/classes/page_converter_spec.cr
Normal file
|
@ -0,0 +1,97 @@
|
|||
require "../spec_helper"
|
||||
|
||||
include Nodes
|
||||
|
||||
describe PageConverter do
|
||||
it "sets the title and subtitle if present" do
|
||||
paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON
|
||||
[
|
||||
{
|
||||
"text": "Title",
|
||||
"type": "H3",
|
||||
"markups": [],
|
||||
"href": null,
|
||||
"iframe": null,
|
||||
"layout": null,
|
||||
"metadata": null
|
||||
},
|
||||
{
|
||||
"text": "Subtitle",
|
||||
"type": "H4",
|
||||
"markups": [],
|
||||
"href": null,
|
||||
"iframe": null,
|
||||
"layout": null,
|
||||
"metadata": null
|
||||
}
|
||||
]
|
||||
JSON
|
||||
|
||||
page = PageConverter.new.convert(paragraphs)
|
||||
|
||||
page.title.should eq "Title"
|
||||
page.subtitle.should eq "Subtitle"
|
||||
end
|
||||
|
||||
it "sets the title to the first paragraph if no title" do
|
||||
paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON
|
||||
[
|
||||
{
|
||||
"text": "Not a title",
|
||||
"type": "P",
|
||||
"markups": [],
|
||||
"href": null,
|
||||
"iframe": null,
|
||||
"layout": null,
|
||||
"metadata": null
|
||||
}
|
||||
]
|
||||
JSON
|
||||
page = PageConverter.new.convert(paragraphs)
|
||||
|
||||
page.title.should eq "Not a title"
|
||||
page.subtitle.should eq nil
|
||||
end
|
||||
|
||||
it "calls ParagraphConverter to convert the remaining paragraph content" do
|
||||
paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON
|
||||
[
|
||||
{
|
||||
"text": "Title",
|
||||
"type": "H3",
|
||||
"markups": [],
|
||||
"href": null,
|
||||
"iframe": null,
|
||||
"layout": null,
|
||||
"metadata": null
|
||||
},
|
||||
{
|
||||
"text": "Subtitle",
|
||||
"type": "H4",
|
||||
"markups": [],
|
||||
"href": null,
|
||||
"iframe": null,
|
||||
"layout": null,
|
||||
"metadata": null
|
||||
},
|
||||
{
|
||||
"text": "Content",
|
||||
"type": "P",
|
||||
"markups": [],
|
||||
"href": null,
|
||||
"iframe": null,
|
||||
"layout": null,
|
||||
"metadata": null
|
||||
}
|
||||
]
|
||||
JSON
|
||||
|
||||
page = PageConverter.new.convert(paragraphs)
|
||||
|
||||
page.nodes.should eq [
|
||||
Paragraph.new([
|
||||
Text.new("Content"),
|
||||
] of Child),
|
||||
]
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue