Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/apps/web/lib/structured-data.ts
blob: 4f574ee8ad6678cda4be88fcb6921e6e020acb82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later

import { catalogMessages } from "./catalog-messages.ts";
import type { Locale } from "./localization";
import { productSlugs, products, type ProductSlug } from "./products.ts";

/**
 * schema.org data that search engines read from the pages, as JSON-LD.
 *
 * Progmasoft is described as an `Organization`, the general type; the
 * projects as `SoftwareSourceCode`, since each is open source with a public
 * repository. A project's text comes in the language of the page.
 */

const organizationId = "https://progmasoft.com/#organization";

/** Progmasoft itself, for progmasoft.com. */
export function organizationData() {
  return {
    "@context": "https://schema.org",
    "@type": "Organization",
    "@id": organizationId,
    name: "Progmasoft",
    url: "https://progmasoft.com/",
    logo: "https://progmasoft.com/apple-icon.png",
    email: "support@progmasoft.com",
    sameAs: ["https://github.com/Progmasoft"],
  };
}

/** The site progmasoft.com, published by Progmasoft. */
export function websiteData(locale: Locale) {
  return {
    "@context": "https://schema.org",
    "@type": "WebSite",
    name: "Progmasoft",
    url: "https://progmasoft.com/",
    inLanguage: locale,
    publisher: { "@id": organizationId },
  };
}

/**
 * The URLs of the licenses of an SPDX expression: one per license it names.
 * `A AND B` and `A OR B` name two; `A WITH E` names A.
 */
export function licenseUrls(expression: string): string[] {
  return expression
    .split(/\s+(?:AND|OR)\s+/)
    .map((term) =>
      term
        .replace(/[()]/g, "")
        .split(/\s+WITH\s+/)[0]
        ?.trim(),
    )
    .filter((id): id is string => Boolean(id))
    .map((id) => `https://spdx.org/licenses/${id}.html`);
}

/** One project, for its page on product.progmasoft.com. */
export function productData(slug: ProductSlug, locale: Locale) {
  const product = products[slug];
  const text = catalogMessages[locale].products[slug];
  const licenses = licenseUrls(product.license);
  return {
    "@context": "https://schema.org",
    "@type": "SoftwareSourceCode",
    "@id": `https://product.progmasoft.com/${slug}#project`,
    name: product.name,
    alternateName: `Progmasoft ${product.name}`,
    description: text.description,
    abstract: text.tagline,
    url: `https://product.progmasoft.com/${slug}`,
    ...(product.site ? { sameAs: [product.site] } : {}),
    codeRepository: product.source,
    discussionUrl: product.issues,
    programmingLanguage: product.language,
    license: licenses.length === 1 ? licenses[0] : licenses,
    dateCreated: product.started,
    creativeWorkStatus:
      product.status === "released" ? "Released" : "In development",
    ...(product.latestRelease
      ? {
          version: product.latestRelease.version,
          dateModified: product.latestRelease.date,
        }
      : {}),
    keywords: text.features.join(", "),
    inLanguage: locale,
    author: { "@id": organizationId },
    publisher: {
      "@type": "Organization",
      "@id": organizationId,
      name: "Progmasoft",
      url: "https://progmasoft.com/",
    },
  };
}

/** The catalog of product.progmasoft.com: every project, in catalog order. */
export function catalogData(locale: Locale) {
  return {
    "@context": "https://schema.org",
    "@type": "ItemList",
    name: "Progmasoft projects",
    url: "https://product.progmasoft.com/",
    numberOfItems: productSlugs.length,
    itemListElement: productSlugs.map((slug, index) => ({
      "@type": "ListItem",
      position: index + 1,
      url: `https://product.progmasoft.com/${slug}`,
      name: products[slug].name,
      description: catalogMessages[locale].products[slug].tagline,
    })),
  };
}

/**
 * The text of a JSON-LD script element. `<` is escaped so that no string in
 * the data can close the element.
 */
export function jsonLd(data: unknown): string {
  return JSON.stringify(data).replace(/</g, "\\u003c");
}