diff --git a/db/migrateWpPostsToArchieMl.ts b/db/migrateWpPostsToArchieMl.ts index 12bacc45be6..46e5db53ff0 100644 --- a/db/migrateWpPostsToArchieMl.ts +++ b/db/migrateWpPostsToArchieMl.ts @@ -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" @@ -92,7 +93,7 @@ const migrate = async (): Promise => { "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 { @@ -138,8 +139,11 @@ const migrate = async (): Promise => { // 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
above and below h1's - adjustHeadingLevels(archieMlBodyElements, isEntry) + // If the article is an entry, we also put an
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( diff --git a/db/model/Gdoc/htmlToEnriched.ts b/db/model/Gdoc/htmlToEnriched.ts index 138a52dd5c2..cef56ccdcc4 100644 --- a/db/model/Gdoc/htmlToEnriched.ts +++ b/db/model/Gdoc/htmlToEnriched.ts @@ -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++) { @@ -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 ) }