diff --git a/datapage/Datapage.ts b/datapage/Datapage.ts index 34d5b965919..2bc25e83e4e 100644 --- a/datapage/Datapage.ts +++ b/datapage/Datapage.ts @@ -36,9 +36,13 @@ export const getDatapageDataV2 = async ( let nextUpdate = undefined if (variableMetadata.updatePeriodDays) { const date = dayjs(version) - nextUpdate = date - .add(variableMetadata.updatePeriodDays, "day") - .format("MMMM YYYY") + const nextUpdateDate = date.add( + variableMetadata.updatePeriodDays, + "day" + ) + if (nextUpdateDate.isBefore(dayjs())) + nextUpdate = dayjs().add(1, "month").format("MMMM YYYY") + else nextUpdate = nextUpdateDate.format("MMMM YYYY") } const datapageJson: DataPageDataV2 = { status: "draft", diff --git a/site/DataPageV2Content.tsx b/site/DataPageV2Content.tsx index 289585757f3..62ec0550a27 100644 --- a/site/DataPageV2Content.tsx +++ b/site/DataPageV2Content.tsx @@ -30,6 +30,50 @@ declare global { } export const OWID_DATAPAGE_CONTENT_ROOT_ID = "owid-datapageJson-root" +const getDateRange = (dateRange: string): string | null => { + // This regex matches: + // Beginning of string + // Ignore whitespace + // a named group called start that matches: + // normal dash aka minus OR en dash + // 1 or more digits + // Ignore whitespace + // normal dash aka minus OR en dash + // Ignore whitespace + // a named group called end that matches: + // normal dash aka minus OR en dash + // 1 or more digits + // Ignore whitespace + // End of string + const dateRangeRegex = + /^\s*(?(-|–)?\d+)\s*(-|–)\s*(?(-|–)?\d+)\s*$/ + const match = dateRange.match(dateRangeRegex) + if (match) { + const firstYearString = match.groups?.start + const lastYearString = match.groups?.end + if (!firstYearString || !lastYearString) return null + + const firstYear = parseInt(firstYearString, 10) + const lastYear = parseInt(lastYearString, 10) + let formattedFirstYear + + // if start year is before year 0, add BC to the end + if (firstYear < 0) formattedFirstYear = `${Math.abs(firstYear)} BCE` + else formattedFirstYear = firstYear + + // if end year is before year 0, add BC to the end or, if start year is after year 0, add AD to the end + let formattedLastYear + if (lastYear < 0) formattedLastYear = `${Math.abs(lastYear)} BCE` + else if (firstYear < 0) formattedLastYear = `${lastYear} CE` + else formattedLastYear = lastYear + + if (lastYear < 0 || firstYear < 0) + return `${formattedFirstYear} – ${formattedLastYear}` + else return `${formattedFirstYear}–${formattedLastYear}` + } + return null +} + export const DataPageV2Content = ({ datapageData, grapherConfig, @@ -90,14 +134,10 @@ export const DataPageV2Content = ({ // by Joe. Also, we need the dataset title. const producers = datapageData.origins.map((o) => o.producer).join("; ") const processedAdapted = - datapageData.owidProcessingLevel === "minor" - ? `Processed by` - : `Adapted by` - const yearOfUpdate = dayjs(datapageData.lastUpdated, [ - "YYYY", - "YYYY-MM-DD", - ]).year() - const citationShort = `${producers} — ${processedAdapted} OWID (${yearOfUpdate})` + datapageData.owidProcessingLevel === "minor" ? `minor` : `major` + const lastUpdated = dayjs(datapageData.lastUpdated, ["YYYY", "YYYY-MM-DD"]) + const yearOfUpdate = lastUpdated.year() + const citationShort = `${producers} — with ${processedAdapted} processing by Our World In Data (${yearOfUpdate})` const originsLong = datapageData.origins .map((o) => `${o.producer}, ${o.title ?? o.titleSnapshot}`) .join("; ") @@ -112,10 +152,6 @@ export const DataPageV2Content = ({ datapageData.origins.length && datapageData.origins[0].urlDownload const citationLong = `${citationShort}. ${datapageData.title}. ${originsLong}, ${processedAdapted} by Our World In Data. Retrieved ${dateAccessed} from ${urlAccessed}` - const processedAdaptedText = - datapageData.owidProcessingLevel === "minor" - ? `Processed by Our World In Data` - : `Adapted by Our World In Data` const { linkedDocuments = {}, @@ -153,6 +189,8 @@ export const DataPageV2Content = ({ /> ) : null + const dateRange = getDateRange(datapageData.dateRange) + const citationDatapage = `Our World In Data (${yearOfUpdate}). Data Page: ${datapageData.title} – ${producers}. Retrieved from {url} [online resource]` return ( {datapageData.attribution} {datapageData.owidProcessingLevel && (
+ with{" "} - {processedAdaptedText} - + {processedAdapted} + {" processing"} + {" "} + by Our World In Data
)} @@ -304,13 +345,15 @@ export const DataPageV2Content = ({
Date range
-
{datapageData.dateRange}
+
{dateRange}
Last updated
-
{datapageData.lastUpdated}
+
+ {lastUpdated.format("MMMM D, YYYY")} +
{datapageData.nextUpdate && (
@@ -790,8 +833,9 @@ export const DataPageV2Content = ({ limited space (e.g. in data visualizations, - on Twitter), you - can use this + on social + media), you can + use this abbreviated in-line citation: