File tree Expand file tree Collapse file tree 5 files changed +69
-27
lines changed
project_styleguide/templates/patterns/pages/blog Expand file tree Collapse file tree 5 files changed +69
-27
lines changed Original file line number Diff line number Diff line change @@ -228,6 +228,47 @@ def first_author(self):
228228 return author .author
229229 return None
230230
231+ def get_blog_posting_jsonld (self , request ):
232+ """Build the BlogPosting JSON-LD structured data as a dict."""
233+ data = {
234+ "@context" : "https://schema.org" ,
235+ "@type" : "BlogPosting" ,
236+ "mainEntityOfPage" : {
237+ "@type" : "WebPage" ,
238+ "@id" : self .get_full_url (request ),
239+ },
240+ "headline" : self .title ,
241+ "description" : self .search_description or self .listing_summary or "" ,
242+ "publisher" : {
243+ "@type" : "Organization" ,
244+ "name" : "Torchbox" ,
245+ "logo" : {
246+ "@type" : "ImageObject" ,
247+ "url" : f"{ request .site .root_url } /apple-touch-icon.png" ,
248+ },
249+ },
250+ "datePublished" : self .date .isoformat (),
251+ "dateModified" : (
252+ self .last_published_at .date ().isoformat ()
253+ if self .last_published_at
254+ else self .date .isoformat ()
255+ ),
256+ }
257+
258+ if self .feed_image :
259+ data ["image" ] = self .feed_image .get_rendition ("width-1200|format-webp" ).url
260+
261+ if self .first_author :
262+ author_data = {
263+ "@type" : "Person" ,
264+ "name" : self .first_author .name ,
265+ }
266+ if self .first_author .person_page :
267+ author_data ["url" ] = self .first_author .person_page .get_full_url (request )
268+ data ["author" ] = author_data
269+
270+ return data
271+
231272 @property
232273 def read_time (self ):
233274 if self .body_word_count :
Original file line number Diff line number Diff line change 1+ import json
2+
3+ from django import template
4+ from django .utils .safestring import mark_safe
5+
6+
7+ register = template .Library ()
8+
9+
10+ @register .simple_tag (takes_context = True )
11+ def blog_posting_jsonld (context ):
12+ """Return the BlogPosting JSON-LD structured data as JSON."""
13+ page = context .get ("page" )
14+ request = context .get ("request" )
15+ if page and hasattr (page , "get_blog_posting_jsonld" ) and request :
16+ return mark_safe (json .dumps (page .get_blog_posting_jsonld (request ), indent = 4 )) # noqa: S308
17+ return ""
Original file line number Diff line number Diff line change 1+ import json
2+
13from django import template
4+ from django .utils .safestring import mark_safe
25from django .utils .text import camel_case_to_spaces , slugify
36
47from wagtail .blocks import StreamValue
912register = template .Library ()
1013
1114
15+ @register .filter (name = "json" )
16+ def json_filter (value ):
17+ """Serialize a value to JSON."""
18+ return mark_safe (json .dumps (value , indent = 4 )) # noqa: S308
19+
20+
1221# Social text
1322@register .filter (name = "social_text" )
1423def social_text (page , site ):
Original file line number Diff line number Diff line change 1- {% load wagtailcore_tags wagtailimages_tags %}
1+ {% load blog_tags %}
22< script type ="application/ld+json ">
3- {
4- "@context" : "https://schema.org" ,
5- "@type" : "BlogPosting" ,
6- "mainEntityOfPage" : {
7- "@type" : "WebPage" ,
8- "@id" : "{% fullpageurl page %}"
9- } ,
10- "headline" : "{{ page.title|escapejs }}" ,
11- "description" : "{% if page.search_description %}{{ page.search_description|escapejs }}{% elif page.listing_summary %}{{ page.listing_summary|escapejs }}{% endif %}" { % if page . feed_image % } ,
12- "image" : "{% image page.feed_image format-webp width-1200 as blog_image %}{{ blog_image.url }}" { % endif % } { % if page . first_author % } ,
13- "author" : {
14- "@type" : "Person" ,
15- "name" : "{{ page.first_author.name|escapejs }}" { % if page . first_author . person_page % } ,
16- "url" : "{% fullpageurl page.first_author.person_page %}" { % endif % }
17- } { % endif % } ,
18- "publisher" : {
19- "@type" : "Organization" ,
20- "name" : "Torchbox" ,
21- "logo" : {
22- "@type" : "ImageObject" ,
23- "url" : "{% wagtail_site as current_site %}{{ current_site.root_url }}/apple-touch-icon.png"
24- }
25- } ,
26- "datePublished" : "{{ page.date|date:'Y-m-d' }}" ,
27- "dateModified" : "{% if page.last_published_at %}{{ page.last_published_at|date:'Y-m-d' }}{% else %}{{ page.date|date:'Y-m-d' }}{% endif %}"
28- }
3+ { % blog_posting_jsonld % }
294</ script >
You can’t perform that action at this time.
0 commit comments