Skip to content

Commit

Permalink
Added support for headings and hyperlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
gdevxy committed Nov 28, 2024
1 parent 86164c4 commit 3dfce20
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@ToString
public class Data {

private String uri;
private Target target;

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ <h1 class="card-title">{blogPost.title}</h1>
{#when block.node}
{#is EMBEDDED_ENTRY}
{#include blog/blog-image image=block.image /}
{#is HEADING_1}
{#include blog/blog-header heading=1 value=block.blocks[0].value}{/include}
{#is HEADING_2}
{#include blog/blog-header heading=2 value=block.blocks[0].value}{/include}
{#is HEADING_3}
{#include blog/blog-header heading=3 value=block.blocks[0].value}{/include}
{#is HEADING_4}
{#include blog/blog-header heading=4 value=block.blocks[0].value}{/include}
{#is HEADING_5}
{#include blog/blog-header heading=5 value=block.blocks[0].value}{/include}
{#is HEADING_6}
{#include blog/blog-header heading=6 value=block.blocks[0].value}{/include}
{#is PARAGRAPH}
{#include blog/blog-paragraph blocks=block.blocks /}
{#is UL_LIST}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p class="h{heading} px-5">{value}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="{hyperlink}" class="link-underline link-underline-opacity-0" target="_blank">{value}</a>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<li>
{item.value}
{#include blog/blog-text blocks=blocks}{/include}
</li>
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
<div class="py-3 px-5 fs-5">
{#for block in blocks}
{#when block.node}
{#is TEXT}
{#include blog/blog-marks marks=block.marks value=block.value}{/}
{#else}
{block.value}
{/when}
{/for}
{#include blog/blog-text blocks=blocks}{/include}
</div>
10 changes: 10 additions & 0 deletions component/src/main/resources/templates/blog/blog-text.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{#for block in blocks}
{#when block.node}
{#is HYPERLINK}
{#include blog/blog-hyperlink hyperlink=block.value value=block.blocks[0].value}{/include}
{#is TEXT}
{#include blog/blog-marks marks=block.marks value=block.value}{/include}
{#else}
{block.value}
{/when}
{/for}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<ul class="ms-5 py-3 fs-5">
{#for block in blocks}
{#include blog/blog-list-item item=block.blocks[0].blocks[0] /}
{#include blog/blog-list-item blocks=block.blocks[0].blocks}{/include}
{/for}
</ul>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum Node {
HEADING_4("heading-4"),
HEADING_5("heading-5"),
HEADING_6("heading-6"),
HYPERLINK("hyperlink"),
OL_LIST("ordered-list"),
UL_LIST("unordered-list"),
LIST_ITEM("list-item"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,28 @@ private List<BlogPost.ContentBlock> toContentBlocks(@Nullable ContentfulClient c

private BlogPost.ContentBlock toContent(@Nullable ContentfulClient client, RichContent content) {

var nodeType = Node.of(content.getNodeType());

return BlogPost.ContentBlock.builder()
.node(Node.of(content.getNodeType()))
.value(content.getValue())
.node(nodeType)
.value(toValue(nodeType, content))
.marks(content.getMarks().stream().map(Mark::getType).map(com.gdevxy.blog.model.contentful.Mark::of).collect(Collectors.toUnmodifiableSet()))
.blocks(toContentBlocks(client, content.getContent()))
.image(toImage(client, content.getData()).orElse(null))
.image(toImage(client, nodeType, content.getData()).orElse(null))
.build();
}

private Optional<Image> toImage(@Nullable ContentfulClient client, Data embeddedEntry) {
private String toValue(Node node, RichContent content) {

return switch(node) {
case HYPERLINK -> content.getData().getUri();
default -> content.getValue();
};
}

private Optional<Image> toImage(@Nullable ContentfulClient client, Node nodeType, Data embeddedEntry) {

if (embeddedEntry.getTarget() == null || client == null) {
if (nodeType != Node.EMBEDDED_ENTRY || client == null) {
return Optional.empty();
}

Expand Down

0 comments on commit 3dfce20

Please sign in to comment.