aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/MetaItems/Topics
Commit message (Collapse)AuthorAgeFilesLines
* refactor: use formatjs swc pluginArmand Philippot2022-03-231-0/+1
| | | | | I'm not able to configure SWC plugins in Next.js so to make it works, all translation must have an id.
* refactor: split posts meta into smaller componentsArmand Philippot2022-03-011-0/+36
f='#n56'>56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
import { Dates } from '@ts/types/app';
import { settings } from '@utils/config';
import {
  AboutPage,
  Article,
  Blog,
  BlogPosting,
  ContactPage,
  Graph,
  WebPage,
} from 'schema-dts';

export type GetBlogSchemaProps = {
  /**
   * True if the page is part of the blog.
   */
  isSinglePage: boolean;
  /**
   * The page locale.
   */
  locale: string;
  /**
   * The page slug with a leading slash.
   */
  slug: string;
};

/**
 * Retrieve the JSON for Blog schema.
 *
 * @param props - The page data.
 * @returns {Blog} The JSON for Blog schema.
 */
export const getBlogSchema = ({
  isSinglePage,
  locale,
  slug,
}: GetBlogSchemaProps): Blog => {
  return {
    '@id': `${settings.url}/#blog`,
    '@type': 'Blog',
    author: { '@id': `${settings.url}/#branding` },
    creator: { '@id': `${settings.url}/#branding` },
    editor: { '@id': `${settings.url}/#branding` },
    blogPost: isSinglePage ? { '@id': `${settings.url}/#article` } : undefined,
    inLanguage: locale,
    isPartOf: isSinglePage
      ? {
          '@id': `${settings.url}${slug}`,
        }
      : undefined,
    license: 'https://creativecommons.org/licenses/by-sa/4.0/deed.fr',
    mainEntityOfPage: isSinglePage
      ? undefined
      : { '@id': `${settings.url}${slug}` },
  };
};

export type SinglePageSchemaReturn = {
  about: AboutPage;
  contact: ContactPage;
  page: Article;
  post: BlogPosting;
};

export type SinglePageSchemaKind = keyof SinglePageSchemaReturn;

export type GetSinglePageSchemaProps<T extends SinglePageSchemaKind> = {
  /**
   * The number of comments.
   */
  commentsCount?: number;
  /**
   * The page content.
   */
  content?: string;
  /**
   * The url of the cover.
   */
  cover?: string;
  /**
   * The page dates.
   */
  dates: Dates;
  /**
   * The page description.
   */
  description: string;
  /**
   * The page id.
   */
  id: string;
  /**
   * The page kind.
   */
  kind: T;
  /**
   * The page locale.
   */
  locale: string;
  /**
   * The page slug with a leading slash.
   */
  slug: string;
  /**
   * The page title.
   */
  title: string;
};

/**
 * Retrieve the JSON schema depending on the page kind.
 *
 * @param props - The page data.
 * @returns {SinglePageSchemaReturn[T]} - Either AboutPage, ContactPage, Article or BlogPosting schema.
 */
export const getSinglePageSchema = <T extends SinglePageSchemaKind>({
  commentsCount,
  content,
  cover,
  dates,
  description,
  id,
  kind,
  locale,
  title,
  slug,
}: GetSinglePageSchemaProps<T>): SinglePageSchemaReturn[T] => {
  const publicationDate = new Date(dates.publication);
  const updateDate = dates.update ? new Date(dates.update) : undefined;
  const singlePageSchemaType = {
    about: 'AboutPage',
    contact: 'ContactPage',
    page: 'Article',
    post: 'BlogPosting',
  };

  return {
    '@id': `${settings.url}/#${id}`,
    '@type': singlePageSchemaType[kind],
    name: title,
    description,
    articleBody: content,
    author: { '@id': `${settings.url}/#branding` },
    commentCount: commentsCount,
    copyrightYear: publicationDate.getFullYear(),
    creator: { '@id': `${settings.url}/#branding` },
    dateCreated: publicationDate.toISOString(),
    dateModified: updateDate && updateDate.toISOString(),
    datePublished: publicationDate.toISOString(),
    editor: { '@id': `${settings.url}/#branding` },
    headline: title,
    image: cover,
    inLanguage: locale,
    license: 'https://creativecommons.org/licenses/by-sa/4.0/deed.fr',
    thumbnailUrl: cover,
    isPartOf:
      kind === 'post'
        ? {
            '@id': `${settings.url}/blog`,
          }
        : undefined,
    mainEntityOfPage: { '@id': `${settings.url}${slug}` },
  } as SinglePageSchemaReturn[T];
};

export type GetWebPageSchemaProps = {
  /**
   * The page description.
   */
  description: string;
  /**
   * The page locale.
   */
  locale: string;
  /**
   * The page slug.
   */
  slug: string;
  /**
   * The page title.
   */
  title: string;
  /**
   * The page last update.
   */
  updateDate?: string;
};

/**
 * Retrieve the JSON for WebPage schema.
 *
 * @param props - The page data.
 * @returns {WebPage} The JSON for WebPage schema.
 */
export const getWebPageSchema = ({
  description,
  locale,
  slug,
  title,
  updateDate,
}: GetWebPageSchemaProps): WebPage => {
  return {
    '@id': `${settings.url}${slug}`,
    '@type': 'WebPage',
    breadcrumb: { '@id': `${settings.url}/#breadcrumb` },
    lastReviewed: updateDate,
    name: title,
    description: description,
    inLanguage: locale,
    reviewedBy: { '@id': `${settings.url}/#branding` },
    url: `${settings.url}${slug}`,
    isPartOf: {
      '@id': `${settings.url}`,
    },
  };
};

export const getSchemaJson = (graphs: Graph['@graph']): Graph => {
  return {
    '@context': 'https://schema.org',
    '@graph': graphs,
  };
};