scribe/spec/classes/markup_converter_spec.cr

100 wiersze
2.3 KiB
Crystal
Czysty Zwykły widok Historia

First step rendering a page The API responds with a bunch of paragraphs which the client converts into Paragraph objects. This turns the paragraphs in a PostResponse's Paragraph objects into the form needed to render them on a page. This includes converting flat list elements into list elements nested by a UL. And adding a limited markups along the way. The array of paragraphs is passed to a recursive function. The function takes the first paragraph and either wraps the (marked up) contents in a container tag (like Paragraph or Heading3), and then moves onto the next tag. If it finds a list, it starts parsing the next paragraphs as a list instead. Originally, this was implemented like so: ```crystal paragraph = paragraphs.shift if list? convert_list([paragraph] + paragraphs) end ``` However, passing the `paragraphs` after adding it to the already shifted `paragraph` creates a new object. This means `paragraphs` won't be mutated and once the list is parsed, it starts with the next element of the list. Instead, the element is `shift`ed inside each converter. ```crystal if paragraphs.first == list? convert_list(paragraphs) end def convert_list(paragraphs) paragraph = paragraphs.shift # ... end ``` When rendering, there is an Empty and Container object. These represent a kind of "null object" for both leafs and parent objects respectively. They should never actually render. Emptys are filtered out, and Containers are never created explicitly but this will make the types pass. IFrames are a bit of a special case. Each IFrame has custom data on it that this system would need to be aware of. For now, instead of trying to parse the seemingly large number of iframe variations and dealing with embedded iframe problems, this will just keep track of the source page URL and send the user there with a link.
2021-05-16 18:14:25 +00:00
require "../spec_helper"
include Nodes
describe MarkupConverter do
it "returns just text with no markups" do
json = <<-JSON
{
"text": "Hello, world",
"type": "P",
"markups": [],
"href": null,
"iframe": null,
"layout": null,
"metadata": null
}
JSON
paragraph = PostResponse::Paragraph.from_json(json)
result = MarkupConverter.convert(text: paragraph.text, markups: paragraph.markups)
result.should eq([Text.new(content: "Hello, world")])
end
it "returns just text with multiple markups" do
json = <<-JSON
{
"text": "strong and emphasized only",
"type": "P",
"markups": [
{
"title": null,
"type": "STRONG",
"href": null,
"start": 0,
"end": 6,
"rel": null,
"anchorType": null
},
{
"title": null,
"type": "EM",
"href": null,
"start": 11,
"end": 21,
"rel": null,
"anchorType": null
}
],
"href": null,
"iframe": null,
"layout": null,
"metadata": null
}
JSON
paragraph = PostResponse::Paragraph.from_json(json)
result = MarkupConverter.convert(text: paragraph.text, markups: paragraph.markups)
result.should eq([
Strong.new(children: [Text.new(content: "strong")] of Child),
Text.new(content: " and "),
Emphasis.new(children: [Text.new(content: "emphasized")] of Child),
Text.new(content: " only"),
])
end
it "returns just text with a code markup" do
json = <<-JSON
{
"text": "inline code",
"type": "P",
"markups": [
{
"title": null,
"type": "CODE",
"href": null,
"start": 7,
"end": 11,
"rel": null,
"anchorType": null
}
],
"href": null,
"iframe": null,
"layout": null,
"metadata": null
}
JSON
paragraph = PostResponse::Paragraph.from_json(json)
result = MarkupConverter.convert(text: paragraph.text, markups: paragraph.markups)
result.should eq([
Text.new(content: "inline "),
Code.new(children: [Text.new(content: "code")] of Child),
])
end
end