Skip to content

Commit

Permalink
Merge pull request #3130 from owid/tablepress-migration
Browse files Browse the repository at this point in the history
(wp) dereference tablespress tables
  • Loading branch information
mlbrgl authored Feb 5, 2024
2 parents cb51bbc + ebba6ec commit 5cb2514
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 22 deletions.
13 changes: 0 additions & 13 deletions baker/formatWordpressPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import urlSlug from "url-slug"
import React from "react"
import ReactDOMServer from "react-dom/server.js"
import { BAKED_BASE_URL, HTTPS_ONLY } from "../settings/serverSettings.js"
import { getTables } from "../db/wpdb.js"
import Tablepress from "../site/Tablepress.js"
import { GrapherExports } from "../baker/GrapherBakingUtils.js"
import { AllCharts, renderAllCharts } from "../site/blocks/AllCharts.js"
import { FormattingOptions } from "@ourworldindata/types"
Expand Down Expand Up @@ -249,17 +247,6 @@ export const formatWordpressPost = async (
}
)

// Insert [table id=foo] tablepress tables
const tables = await getTables()
html = html.replace(/\[table\s+id=(\d+)\s*\/\]/g, (match, tableId) => {
const table = tables.get(tableId)
if (table)
return ReactDOMServer.renderToStaticMarkup(
<Tablepress data={table.data} />
)
return "UNKNOWN TABLE"
})

// Give "Add country" text (and variations) the appearance of "+ Add Country" chart control
html = html.replace(
/(\+ )?[a|A]dd [c|C]ountry/g,
Expand Down
6 changes: 4 additions & 2 deletions baker/postUpdatedHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as wpdb from "../db/wpdb.js"
import * as db from "../db/db.js"
import {
buildReusableBlocksResolver,
buildTablePressResolver,
getLinksToAddAndRemoveForPost,
} from "../db/syncPostsToGrapher.js"
import { postsTable, select } from "../db/model/Post.js"
Expand Down Expand Up @@ -94,6 +95,7 @@ const syncPostToGrapher = async (
[postId, postId, postId, postId]
)
const dereferenceReusableBlocksFn = await buildReusableBlocksResolver()
const dereferenceTablePressFn = await buildTablePressResolver()

const matchingRows = await db.knexTable(postsTable).where({ id: postId })
const existsInGrapher = !!matchingRows.length
Expand Down Expand Up @@ -137,8 +139,8 @@ const syncPostToGrapher = async (
// Delete from grapher
await transaction.table(postsTable).where({ id: postId }).delete()
else if (postRow) {
const contentWithBlocksInlined = dereferenceReusableBlocksFn(
postRow.content
const contentWithBlocksInlined = dereferenceTablePressFn(
dereferenceReusableBlocksFn(postRow.content)
)
postRow.content = contentWithBlocksInlined

Expand Down
19 changes: 18 additions & 1 deletion db/model/Gdoc/htmlToEnriched.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,24 @@ function cheerioToArchieML(
}
)
.with(
{ tagName: P.union("svg", "table", "video") },
{ tagName: "table" },
(): BlockParseResult<EnrichedBlockHtml> => {
return {
errors: [],
content: [
{
type: "html",
value: `<div class="raw-html-table__container">${
context.$.html(element) ?? ""
}</div>`,
parseErrors: [],
},
],
}
}
)
.with(
{ tagName: P.union("svg", "video") },
(): BlockParseResult<EnrichedBlockHtml> => {
return {
errors: [],
Expand Down
47 changes: 46 additions & 1 deletion db/syncPostsToGrapher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import {
} from "@ourworldindata/utils"
import { postsTable, select } from "./model/Post.js"
import { PostLink } from "./model/PostLink.js"
import { renderTablePress } from "../site/Tablepress.js"

const zeroDateString = "0000-00-00 00:00:00"

const blockRefRegex = /<!-- wp:block \{"ref":(?<id>\d+)\} \/-->/g
const tablePressRegex = /\[table id=(?<id>\d+)\s*\/\]/g
const prominentLinkRegex = /"linkUrl":"(?<url>[^"]+)"/g
const anyHrefRegex = /href="(?<url>[^"]+)"/g
const anySrcRegex = /src="(?<url>[^"]+)"/g
Expand Down Expand Up @@ -114,6 +116,46 @@ export async function buildReusableBlocksResolver(): Promise<BlockResolveFunctio
replaceReusableBlocksRecursive(content, replacerFunction)
}

/**
* This function takes all tables from the tablepress plugin and returns a
* function that can be used as a replacer when using string.replaceAll(regex,
* replacer_function) to replace all tables with their rendered HTML
* representation.
*/
export function buildTablePressReplacerFunction(
tables: Map<string, wpdb.TablepressTable>
): ReplacerFunction {
return (
_match: string,
_firstPattern: string,
_offset: number,
_fullString: string,
matches: Record<string, string> // capturing groups from the regex
) => {
const table = tables.get(matches["id"].toString())
if (!table) {
console.log(`Table ${matches["id"]} not found`)
return ""
}

return renderTablePress(table.data)
}
}

function replaceTablePressShortcodes(
content: string,
replacerFunction: ReplacerFunction
): string {
return content.replaceAll(tablePressRegex, replacerFunction)
}

export async function buildTablePressResolver(): Promise<BlockResolveFunction> {
const allTables = await wpdb.getTables()
const replacerFunction = buildTablePressReplacerFunction(allTables)
return (content: string) =>
replaceTablePressShortcodes(content, replacerFunction)
}

export const postLinkCompareStringGenerator = (item: PostLink): string =>
`${item.linkType} - ${item.target} - ${item.hash} - ${item.queryString}`

Expand Down Expand Up @@ -188,6 +230,7 @@ export function getLinksToAddAndRemoveForPost(

const syncPostsToGrapher = async (): Promise<void> => {
const dereferenceReusableBlocksFn = await buildReusableBlocksResolver()
const dereferenceTablePressFn = await buildTablePressResolver()

const rows = await wpdb.singleton.query(
`-- sql
Expand Down Expand Up @@ -298,7 +341,9 @@ const syncPostsToGrapher = async (): Promise<void> => {
slug: post.post_name.replace(/__/g, "/"),
type: post.post_type,
status: post.post_status,
content: dereferenceReusableBlocksFn(content),
content: dereferenceTablePressFn(
dereferenceReusableBlocksFn(content)
),
featured_image: post.featured_image || "",
published_at:
post.post_date_gmt === zeroDateString
Expand Down
2 changes: 1 addition & 1 deletion db/wpdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ export const getTopics = async (cursor: string = ""): Promise<Topic[]> => {
}
}

interface TablepressTable {
export interface TablepressTable {
tableId: string
data: string[][]
}
Expand Down
26 changes: 23 additions & 3 deletions site/Tablepress.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react"
import ReactDOMServer from "react-dom/server.js"

interface Cell {
data: string
Expand All @@ -15,6 +16,7 @@ function cell(data?: string) {
}

const ROWSPAN_TOKEN = "#rowspan#"
const COLSPAN_TOKEN = "#colspan#"

function parseTable(table: string[][]): Cell[][] {
const resultTable: Cell[][] = []
Expand All @@ -31,6 +33,16 @@ function parseTable(table: string[][]): Cell[][] {
} else {
resultRow.push(cell())
}
} else if (data === COLSPAN_TOKEN) {
let j = c - 1
while (j >= 0 && row[j] === COLSPAN_TOKEN) {
j--
}
if (j >= 0) {
resultRow[j].colspan++
} else {
resultRow.push(cell())
}
} else {
resultRow.push(cell(data))
}
Expand All @@ -40,31 +52,39 @@ function parseTable(table: string[][]): Cell[][] {
return resultTable
}

export const renderTablePress = (table: string[][]) => {
return ReactDOMServer.renderToStaticMarkup(<Tablepress data={table} />)
}

export default function Tablepress(props: { data: string[][] }) {
const { data } = props
const table = parseTable(data)
const [headerRow, ...body] = table
return (
<table className="tablepress">
<table>
<thead>
<tr>
{headerRow.map((cell, i) => (
<th
key={i}
scope="col"
colSpan={cell.colspan}
dangerouslySetInnerHTML={{ __html: cell.data }}
/>
))}
</tr>
</thead>
<tbody className="row-hover">
<tbody>
{body.map((row, i) => (
<tr key={i}>
{row.map((cell, j) => (
<td
key={j}
colSpan={cell.colspan}
rowSpan={cell.rowspan}
dangerouslySetInnerHTML={{ __html: cell.data }}
dangerouslySetInnerHTML={{
__html: cell.data,
}}
/>
))}
</tr>
Expand Down
7 changes: 6 additions & 1 deletion site/gdocs/components/centered-article.scss
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,8 @@ h3.article-block__heading.has-supertitle {
}

div.article-block__table--narrow,
div.article-block__table--wide {
div.article-block__table--wide,
div.raw-html-table__container {
overflow-x: auto;
table {
min-width: 100%;
Expand Down Expand Up @@ -651,6 +652,10 @@ div.article-block__table--wide {
}
}

div.raw-html-table__container {
@include body-3-medium;
}

.article-block__pull-quote {
@include h1-bold-italic;
border-bottom: 1px solid #dbe5f0;
Expand Down

0 comments on commit 5cb2514

Please sign in to comment.