Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adiciona dados estruturados JSON-LD #1785

Closed
wants to merge 1 commit into from
Closed
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
99 changes: 99 additions & 0 deletions models/json-ld.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import webserver from 'infra/webserver';
import removeMarkdown from 'models/remove-markdown';

function getBreadcrumb(items) {
return {
'@context': 'https://schema.org',
'@type': 'BreadcrumbList',
itemListElement: items.map((item, index) => ({
'@type': 'ListItem',
position: index + 1,
name: item.name,
item: item.url,
})),
};
}

function getOrganization() {
return {
'@context': 'https://schema.org',
'@type': 'Organization',
url: webserver.host,
logo: `${webserver.host}/brand/rounded-light-filled.svg`,
name: 'TabNews',
description:
'O TabNews é um site focado na comunidade da área de tecnologia, destinado a debates e troca de conhecimentos por meio de publicações e comentários criados pelos próprios usuários.',
email: '[email protected]',
foundingDate: '2022-05-06T15:20:01.158Z',
};
}

function getPosting(content) {
return {
'@context': 'https://schema.org',
'@type': 'DiscussionForumPosting',
headline: content.title,
sharedContent: content.source_url ? { '@type': 'WebPage', url: content.source_url } : undefined,
...getCommonPostingAndComment(content),
};
}

function getCommonPostingAndComment(content) {
return {
identifier: content.id,
author: {
'@type': 'Person',
name: content.owner_username,
identifier: content.owner_id,
url: `${webserver.host}/${content.owner_username}`,
},
url: `${webserver.host}/${content.owner_username}/${content.slug}`,
datePublished: content.published_at,
dateModified: content.updated_at,
text: removeMarkdown(content.body),
interactionStatistic: [
{
'@type': 'InteractionCounter',
interactionType: 'https://schema.org/LikeAction',
userInteractionCount: content.tabcoins_credit,
},
{
'@type': 'InteractionCounter',
interactionType: 'https://schema.org/DislikeAction',
userInteractionCount: Math.abs(content.tabcoins_debit),
},
{
'@type': 'InteractionCounter',
interactionType: 'https://schema.org/CommentAction',
userInteractionCount: content.children_deep_count,
},
],
comment:
content.children?.map((child) => ({
'@type': 'Comment',
...getCommonPostingAndComment(child),
})) ?? [],
};
}

function getProfile(user) {
return {
'@context': 'https://schema.org',
'@type': 'ProfilePage',
dateCreated: user.created_at,
dateModified: user.updated_at,
mainEntity: {
'@type': 'Person',
name: user.username,
identifier: user.id,
description: user.description ? removeMarkdown(user.description) : undefined,
},
};
}

export default Object.freeze({
getBreadcrumb,
getOrganization,
getPosting,
getProfile,
});
2 changes: 2 additions & 0 deletions pages/[username]/[slug]/index.public.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import webserver from 'infra/webserver.js';
import ad from 'models/advertisement';
import authorization from 'models/authorization.js';
import content from 'models/content.js';
import jsonLd from 'models/json-ld';
import removeMarkdown from 'models/remove-markdown.js';
import user from 'models/user.js';
import { useCollapse } from 'pages/interface';
Expand Down Expand Up @@ -378,6 +379,7 @@ export const getStaticProps = getStaticPropsRevalidate(async (context) => {
canonical: secureContentFound.parent_id
? undefined
: `${webserver.host}/${secureContentFound.owner_username}/${secureContentFound.slug}`,
jsonLd: jsonLd.getPosting(secureContentFound),
};

let secureRootContentFound = null;
Expand Down
19 changes: 18 additions & 1 deletion pages/[username]/classificados/[page]/index.public.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { getStaticPropsRevalidate } from 'next-swr';
import { ContentList, DefaultLayout, UserHeader } from '@/TabNewsUI';
import { FaUser } from '@/TabNewsUI/icons';
import { NotFoundError } from 'errors';
import webserver from 'infra/webserver';
import authorization from 'models/authorization.js';
import content from 'models/content.js';
import jsonLd from 'models/json-ld';
import user from 'models/user.js';
import validator from 'models/validator.js';
import { useUser } from 'pages/interface';
Expand All @@ -14,9 +16,24 @@ export default function RootContent({ contentListFound, pagination, username })
const { push } = useRouter();
const { user, isLoading } = useUser();
const isAuthenticatedUser = user && user.username === username;
const breadcrumbItems = [
{ name: username, url: `${webserver.host}/${username}` },
{ name: 'Classificados', url: `${webserver.host}/${username}/classificados/1` },
];

if (pagination.currentPage > 1) {
breadcrumbItems.push({
name: `Página ${pagination.currentPage}`,
url: `${webserver.host}/${username}/classificados/${pagination.currentPage}`,
});
}

return (
<DefaultLayout metadata={{ title: `Classificados · Página ${pagination.currentPage} · ${username}` }}>
<DefaultLayout
metadata={{
title: `Classificados · Página ${pagination.currentPage} · ${username}`,
jsonLd: jsonLd.getBreadcrumb(breadcrumbItems),
}}>
<UserHeader username={username} adContentCount={pagination.totalRows} />

<ContentList
Expand Down
32 changes: 25 additions & 7 deletions pages/[username]/comentarios/[page]/index.public.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,47 @@ import { getStaticPropsRevalidate } from 'next-swr';
import { ContentList, DefaultLayout, UserHeader } from '@/TabNewsUI';
import { FaUser } from '@/TabNewsUI/icons';
import { NotFoundError } from 'errors';
import webserver from 'infra/webserver';
import authorization from 'models/authorization.js';
import content from 'models/content.js';
import jsonLd from 'models/json-ld';
import removeMarkdown from 'models/remove-markdown.js';
import user from 'models/user.js';
import validator from 'models/validator.js';
import { useUser } from 'pages/interface';

export default function ChildContent({ contentListFound, pagination, username }) {
export default function ChildContent({ contentListFound, pagination, userFound }) {
const { user, isLoading } = useUser();
const isAuthenticatedUser = user && user.username === username;
const isAuthenticatedUser = user && user.username === userFound.username;

const breadcrumbItems = [
{ name: userFound.username, url: `${webserver.host}/${userFound.username}` },
{ name: 'Comentários', url: `${webserver.host}/${userFound.username}/comentarios/1` },
];

if (pagination.currentPage > 1) {
breadcrumbItems.push({
name: `Página ${pagination.currentPage}`,
url: `${webserver.host}/${userFound.username}/comentarios/${pagination.currentPage}`,
});
}

return (
<DefaultLayout metadata={{ title: `Comentários · Página ${pagination.currentPage} · ${username}` }}>
<UserHeader username={username} childContentCount={pagination.totalRows} />
<DefaultLayout
metadata={{
title: `Comentários · Página ${pagination.currentPage} · ${userFound.username}`,
jsonLd: [jsonLd.getBreadcrumb(breadcrumbItems), jsonLd.getProfile(userFound)],
}}>
<UserHeader username={userFound.username} childContentCount={pagination.totalRows} />

<ContentList
contentList={contentListFound}
pagination={pagination}
paginationBasePath={`/${username}/comentarios`}
paginationBasePath={`/${userFound.username}/comentarios`}
emptyStateProps={{
isLoading: isLoading,
title: 'Nenhum comentário encontrado',
description: `${isAuthenticatedUser ? 'Você' : username} ainda não fez nenhum comentário.`,
description: `${isAuthenticatedUser ? 'Você' : userFound.username} ainda não fez nenhum comentário.`,
icon: FaUser,
}}
/>
Expand Down Expand Up @@ -108,7 +126,7 @@ export const getStaticProps = getStaticPropsRevalidate(async (context) => {
props: {
contentListFound: secureContentListFound,
pagination: results.pagination,
username: secureUserFound.username,
userFound: secureUserFound,
},

revalidate: 10,
Expand Down
31 changes: 25 additions & 6 deletions pages/[username]/conteudos/[page]/index.public.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,47 @@ import { getStaticPropsRevalidate } from 'next-swr';
import { ContentList, DefaultLayout, UserHeader } from '@/TabNewsUI';
import { FaUser } from '@/TabNewsUI/icons';
import { NotFoundError } from 'errors';
import webserver from 'infra/webserver';
import authorization from 'models/authorization.js';
import content from 'models/content.js';
import jsonLd from 'models/json-ld';
import user from 'models/user.js';
import validator from 'models/validator.js';
import { useUser } from 'pages/interface';

export default function RootContent({ contentListFound, pagination, username }) {
export default function RootContent({ contentListFound, pagination, userFound }) {
const { push } = useRouter();
const { user, isLoading } = useUser();
const isAuthenticatedUser = user && user.username === username;
const isAuthenticatedUser = user && user.username === userFound.username;

const breadcrumbItems = [
{ name: userFound.username, url: `${webserver.host}/${userFound.username}` },
{ name: 'Publicações', url: `${webserver.host}/${userFound.username}/conteudos/1` },
];

if (pagination.currentPage > 1) {
breadcrumbItems.push({
name: `Página ${pagination.currentPage}`,
url: `${webserver.host}/${userFound.username}/conteudos/${pagination.currentPage}`,
});
}

return (
<DefaultLayout metadata={{ title: `Publicações · Página ${pagination.currentPage} · ${username}` }}>
<UserHeader username={username} rootContentCount={pagination.totalRows} />
<DefaultLayout
metadata={{
title: `Publicações · Página ${pagination.currentPage} · ${userFound.username}`,
jsonLd: [jsonLd.getBreadcrumb(breadcrumbItems), jsonLd.getProfile(userFound)],
}}>
<UserHeader username={userFound.username} rootContentCount={pagination.totalRows} />

<ContentList
contentList={contentListFound}
pagination={pagination}
paginationBasePath={`/${username}/conteudos`}
paginationBasePath={`/${userFound.username}/conteudos`}
emptyStateProps={{
isLoading: isLoading,
title: 'Nenhuma publicação encontrada',
description: `${isAuthenticatedUser ? 'Você' : username} ainda não fez nenhuma publicação.`,
description: `${isAuthenticatedUser ? 'Você' : userFound.username} ainda não fez nenhuma publicação.`,
icon: FaUser,
action: isAuthenticatedUser && {
text: 'Publicar conteúdo',
Expand Down Expand Up @@ -114,6 +132,7 @@ export const getStaticProps = getStaticPropsRevalidate(async (context) => {
contentListFound: secureContentListFound,
pagination: results.pagination,
username: secureUserFound.username,
userFound: secureUserFound,
},

revalidate: 10,
Expand Down
11 changes: 10 additions & 1 deletion pages/[username]/index.public.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import {
} from '@/TabNewsUI';
import { CircleSlashIcon, GearIcon, KebabHorizontalIcon } from '@/TabNewsUI/icons';
import { NotFoundError } from 'errors';
import webserver from 'infra/webserver';
import authorization from 'models/authorization.js';
import content from 'models/content.js';
import jsonLd from 'models/json-ld';
import user from 'models/user.js';
import validator from 'models/validator.js';
import { createErrorMessage, useUser } from 'pages/interface';
Expand All @@ -47,7 +49,14 @@ export default function Page({ userFound: userFoundFallback }) {
}

return (
<DefaultLayout metadata={{ title: `${userFound.username}` }}>
<DefaultLayout
metadata={{
title: `${userFound.username}`,
jsonLd: [
jsonLd.getBreadcrumb([{ name: userFound.username, url: `${webserver.host}/${userFound.username}` }]),
jsonLd.getProfile(userFound),
],
}}>
<UserProfile key={userFound.id} userFound={userFound} onUpdate={onUpdate} />
</DefaultLayout>
);
Expand Down
6 changes: 5 additions & 1 deletion pages/contato/index.public.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Box, DefaultLayout, Heading, Viewer } from '@/TabNewsUI';
import webserver from 'infra/webserver';
import jsonLd from 'models/json-ld';

export default function Page() {
const breadcrumbItems = [{ name: 'Contato', url: `${webserver.host}/contato` }];

const body = `Leia com atenção qual a melhor forma de entrar em contato:

## Anúncios, Publicidade e Patrocínio
Expand All @@ -26,7 +30,7 @@ export default function Page() {
`;

return (
<DefaultLayout metadata={{ title: 'Contato' }}>
<DefaultLayout metadata={{ title: 'Contato', jsonLd: jsonLd.getBreadcrumb(breadcrumbItems) }}>
<Box>
<Heading as="h1">Contato</Heading>
<Viewer value={body} clobberPrefix="" />
Expand Down
6 changes: 5 additions & 1 deletion pages/faq/index.public.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Box, DefaultLayout, Heading, Viewer } from '@/TabNewsUI';
import webserver from 'infra/webserver';
import jsonLd from 'models/json-ld';

export default function Page() {
const breadcrumbItems = [{ name: 'FAQ', url: `${webserver.host}/faq` }];

const faqContent = [
{
id: 'tabnews',
Expand Down Expand Up @@ -152,7 +156,7 @@ Após o fechamento da falha, o TabNews se compromete em criar um Postmortem púb
const content = `${tableOfContents}\n\n${faqMarkdown}`;

return (
<DefaultLayout metadata={{ title: 'FAQ - Perguntas frequentes' }}>
<DefaultLayout metadata={{ title: 'FAQ - Perguntas frequentes', jsonLd: jsonLd.getBreadcrumb(breadcrumbItems) }}>
<Box sx={{ width: '100%' }}>
<Heading as="h1">FAQ - Perguntas Frequentes</Heading>
<Viewer areLinksTrusted value={content} clobberPrefix="" />
Expand Down
3 changes: 2 additions & 1 deletion pages/index.public.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { FaTree } from '@/TabNewsUI/icons';
import ad from 'models/advertisement';
import authorization from 'models/authorization.js';
import content from 'models/content.js';
import jsonLd from 'models/json-ld';
import user from 'models/user.js';
import validator from 'models/validator.js';

export default function Home({ adFound, contentListFound, pagination }) {
return (
<>
<DefaultLayout>
<DefaultLayout metadata={{ jsonLd: jsonLd.getOrganization() }}>
<ContentList
ad={adFound}
contentList={contentListFound}
Expand Down
14 changes: 13 additions & 1 deletion pages/interface/components/Head/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ export function DefaultHead() {
}

export default function Head({ metadata, children }) {
const { type, title, description, image, url, noIndex, author, published_time, modified_time, canonical } =
function stringifyJsonLd(jsonLd) {
return typeof jsonLd === 'string' ? jsonLd : JSON.stringify(jsonLd);
}

const { type, title, description, image, url, noIndex, author, published_time, modified_time, canonical, jsonLd } =
metadata || {};

const canonicalUrl = canonical?.startsWith('http') ? canonical : `${webserverHost}${canonical}`;
Expand Down Expand Up @@ -114,6 +118,14 @@ export default function Head({ metadata, children }) {

{modified_time && <meta property="article:modified_time" content={modified_time} />}

{Array.isArray(jsonLd)
? jsonLd.map((data) => (
<script key={data['@type']} type="application/ld+json">
{stringifyJsonLd(data)}
</script>
))
: jsonLd && <script type="application/ld+json">{stringifyJsonLd(jsonLd)}</script>}

{children}
</NextHead>
);
Expand Down
Loading