Skip to content

Commit

Permalink
Merge pull request #3013 from owid/gdoc-migration-normalize-headings
Browse files Browse the repository at this point in the history
GDoc migration: normalize headings
  • Loading branch information
danyx23 authored Dec 26, 2023
2 parents 42c97f4 + 82fde64 commit 72667ca
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
10 changes: 7 additions & 3 deletions db/migrateWpPostsToArchieMl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
withoutEmptyOrWhitespaceOnlyTextBlocks,
convertAllWpComponentsToArchieMLBlocks,
adjustHeadingLevels,
findMinimumHeadingLevel,
} from "./model/Gdoc/htmlToEnriched.js"
import { getRelatedCharts, isPostCitable } from "./wpdb.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 postRaw of rawPosts) {
try {
Expand Down Expand Up @@ -143,8 +144,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

0 comments on commit 72667ca

Please sign in to comment.