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

GDoc migration: normalize headings #3013

Merged
merged 4 commits into from
Dec 26, 2023
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
10 changes: 7 additions & 3 deletions db/migrateWpPostsToArchieMl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
withoutEmptyOrWhitespaceOnlyTextBlocks,
convertAllWpComponentsToArchieMLBlocks,
adjustHeadingLevels,
findMinimumHeadingLevel,
} from "./model/Gdoc/htmlToEnriched.js"
import { getRelatedCharts, isPostCitable } from "./wpdb.js"
import { parsePostAuthors } from "./model/Post.js"
Expand Down Expand Up @@ -92,7 +93,7 @@ const migrate = async (): Promise<void> => {
"created_at_in_wordpress",
"updated_at",
"featured_image"
).from(db.knexTable(Post.postsTable)) //.where("id", "=", "24808"))
).from(db.knexTable(Post.postsTable)) //.where("id", "=", "54759"))

for (const post of posts) {
try {
Expand Down Expand Up @@ -138,8 +139,11 @@ const migrate = async (): Promise<void> => {

// Heading levels used to start at 2, in the new layout system they start at 1
// This function iterates all blocks recursively and adjusts the heading levels inline
// If the article is an entry, we also put an <hr /> above and below h1's
adjustHeadingLevels(archieMlBodyElements, isEntry)
// If the article is an entry, we also put an <hr /> above and below h1's. The adjustment
// pulls heading levels up so that entries end up with h1s and others with h2s at the top.
const minHeadingLevel =
findMinimumHeadingLevel(archieMlBodyElements)
adjustHeadingLevels(archieMlBodyElements, minHeadingLevel, isEntry)

if (relatedCharts.length) {
const indexOfFirstHeading = archieMlBodyElements.findIndex(
Expand Down
26 changes: 25 additions & 1 deletion db/model/Gdoc/htmlToEnriched.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,28 @@ export function convertAllWpComponentsToArchieMLBlocks(
})
}

export function findMinimumHeadingLevel(
blocks: OwidEnrichedGdocBlock[]
): number {
let minBlockLevel = 6
for (const block of blocks) {
if (block.type === "heading") {
minBlockLevel = Math.min(block.level, minBlockLevel)
} else if ("children" in block) {
minBlockLevel = Math.min(
findMinimumHeadingLevel(
block.children as OwidEnrichedGdocBlock[]
),
minBlockLevel
)
}
}
return minBlockLevel
}

export function adjustHeadingLevels(
blocks: OwidEnrichedGdocBlock[],
minHeadingLevel: number,
isEntry: boolean
): void {
for (let i = 0; i < blocks.length; i++) {
Expand All @@ -411,10 +431,14 @@ export function adjustHeadingLevels(
blocks.splice(i + 2, 0, { ...hr })
i += 2
}
block.level = Math.max(1, block.level - 1)
const correction = isEntry
? minHeadingLevel - 1
: Math.max(0, minHeadingLevel - 2)
block.level = block.level - correction
} else if ("children" in block) {
adjustHeadingLevels(
block.children as OwidEnrichedGdocBlock[],
minHeadingLevel,
isEntry
)
}
Expand Down
Loading