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

refactor(baker): allow bake dirs for all gdoc types #3310

Merged
merged 1 commit into from
Mar 8, 2024
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
12 changes: 4 additions & 8 deletions baker/SiteBaker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import {
OwidGdocErrorMessageType,
ImageMetadata,
OwidGdoc,
OwidGdocType,
DATA_INSIGHTS_INDEX_PAGE_SIZE,
OwidGdocMinimalPostInterface,
excludeUndefined,
Expand Down Expand Up @@ -104,6 +103,7 @@ import {
getVariableOfDatapageIfApplicable,
} from "../db/model/Variable.js"
import { Knex } from "knex"
import { getBakePath } from "@ourworldindata/components"

type PrefetchedAttachments = {
linkedDocuments: Record<string, OwidGdocMinimalPostInterface>
Expand Down Expand Up @@ -238,13 +238,9 @@ export class SiteBaker {
}

// Bake an individual post/page
private async bakeOwidGdoc(post: OwidGdoc) {
const html = renderGdoc(post)
const dir =
post.content.type === OwidGdocType.DataInsight
? "data-insights/"
: ""
const outPath = path.join(this.bakedSiteDir, `${dir}${post.slug}.html`)
private async bakeOwidGdoc(gdoc: OwidGdoc) {
const html = renderGdoc(gdoc)
const outPath = `${getBakePath(this.bakedSiteDir, gdoc)}.html`
await fs.mkdirp(path.dirname(outPath))
await this.stageWrite(outPath, html)
}
Expand Down
16 changes: 12 additions & 4 deletions packages/@ourworldindata/components/src/GdocsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ export function convertHeadingTextToId(headingText: Span[]): string {
return urlSlug(spansToUnformattedPlainText(headingText))
}

export function getCanonicalUrl(baseUrl: string, gdoc: OwidGdoc): string {
function _getPrefixedPath(prefix: string, gdoc: OwidGdoc): string {
return match(gdoc)
.with(
{
content: { type: OwidGdocType.Homepage },
},
() => baseUrl
() => prefix
)
.with(
{
Expand All @@ -64,13 +64,13 @@ export function getCanonicalUrl(baseUrl: string, gdoc: OwidGdoc): string {
),
},
},
() => `${baseUrl}/${gdoc.slug}`
() => `${prefix}/${gdoc.slug}`
)
.with(
{
content: { type: OwidGdocType.DataInsight },
},
() => `${baseUrl}/data-insights/${gdoc.slug}`
() => `${prefix}/data-insights/${gdoc.slug}`
)
.with(
{
Expand All @@ -81,6 +81,14 @@ export function getCanonicalUrl(baseUrl: string, gdoc: OwidGdoc): string {
.exhaustive()
}

export const getBakePath = (bakedSiteDir: string, gdoc: OwidGdoc): string => {
return _getPrefixedPath(bakedSiteDir, gdoc)
}

export const getCanonicalUrl = (baseUrl: string, gdoc: OwidGdoc): string => {
return _getPrefixedPath(baseUrl, gdoc)
}

export function getPageTitle(gdoc: OwidGdoc) {
return match(gdoc)
.with(
Expand Down
1 change: 1 addition & 0 deletions packages/@ourworldindata/components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export {
getUrlTarget,
checkIsInternalLink,
convertHeadingTextToId,
getBakePath,
getCanonicalUrl,
getPageTitle,
} from "./GdocsUtils.js"
Expand Down
Loading