Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/StructuredData.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { structuredData } from 'lib/structuredData'

const StructuredData = () => (
const StructuredData = ({ data = structuredData }) => (
<script
type="application/ld+json"
dangerouslySetInnerHTML={{
__html: JSON.stringify(structuredData),
__html: JSON.stringify(data).replace(/</g, '\\u003c'),
}}
/>
)
Expand Down
82 changes: 82 additions & 0 deletions lib/findingStructuredData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { EXPLORER_BASE_URL, ORGANIZATION_ID } from 'lib/structuredData'

const getAuthor = (reportedBy) => {
if (!reportedBy) {
return { '@id': ORGANIZATION_ID }
}

const authors = (reportedBy.match(/[^,(]+(?:\([^)]*\))?/g) ?? []).reduce(
(names, part) => {
const trimmed = part.trim()

if (trimmed === '') {
return names
}

if (trimmed.toLowerCase() === 'ooni') {
names.push({ '@id': ORGANIZATION_ID })
} else {
names.push({ '@type': 'Person', name: trimmed })
}

return names
},
[],
)

if (!authors.length) {
return { '@id': ORGANIZATION_ID }
}

return authors
}

const getKeywords = (incident) => {
const keywords = new Set()

for (const tag of incident.tags ?? []) {
keywords.add(tag.startsWith('theme-') ? tag.replace('theme-', '') : tag)
}

for (const theme of incident.themes ?? []) {
keywords.add(theme.replace('_', ' '))
}

return [...keywords]
}

export const getFindingStructuredData = (incident, canonicalUrl) => {
const keywords = getKeywords(incident)
const schema = {
'@context': 'https://schema.org',
'@type': 'Article',
'@id': `${canonicalUrl}#article`,
headline: incident.title,
description: incident.short_description,
url: canonicalUrl,
datePublished: incident.create_time,
author: getAuthor(incident.reported_by),
publisher: {
'@id': ORGANIZATION_ID,
},
mainEntityOfPage: {
'@type': 'WebPage',
'@id': canonicalUrl,
},
isPartOf: {
'@id': `${EXPLORER_BASE_URL}/#website`,
},
inLanguage: 'en',
}


if (incident.update_time) {
schema.dateModified = incident.update_time
}

if (keywords.length) {
schema.keywords = keywords.join(', ')
}

return schema
}
4 changes: 2 additions & 2 deletions lib/structuredData.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const EXPLORER_BASE_URL = 'https://explorer.ooni.org'
const ORGANIZATION_ID = 'https://ooni.org/#organization'
export const EXPLORER_BASE_URL = 'https://explorer.ooni.org'
export const ORGANIZATION_ID = 'https://ooni.org/#organization'

const SUPPORTED_LANGUAGES = [
'en',
Expand Down
13 changes: 13 additions & 0 deletions pages/findings/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { apiEndpoints, fetcher } from 'lib/api'

import NotFound from 'components/NotFound'
import FindingDisplay from 'components/findings/FindingDisplay'
import StructuredData from 'components/StructuredData'
import { getFindingStructuredData } from 'lib/findingStructuredData'
import { useMemo } from 'react'
import { useIntl } from 'react-intl'

Expand All @@ -26,6 +28,14 @@ const ReportView = ({ data, canonicalUrl }) => {
const metaTitle = `${!!data?.incident?.title && `${data?.incident?.title} | `}${intl.formatMessage({ id: 'General.OoniExplorer' })}`
const metaDescription = data?.incident?.short_description

const findingStructuredData = useMemo(() => {
if (!data?.incident) {
return null
}

return getFindingStructuredData(data.incident, canonicalUrl)
}, [canonicalUrl, data?.incident])

return (
<>
<Head>
Expand All @@ -44,6 +54,9 @@ const ReportView = ({ data, canonicalUrl }) => {
content={metaDescription}
/>
<link rel="canonical" key="canonical" href={canonicalUrl} />
{findingStructuredData && (
<StructuredData data={findingStructuredData} />
)}
</Head>
<div className="container">
{data ? (
Expand Down
Loading