notice potential repeated children key entries immediately (#31898)
This commit is contained in:
@@ -56,6 +56,7 @@ export default async function createTree(originalPath, langObj) {
|
|||||||
|
|
||||||
// Process frontmatter children recursively.
|
// Process frontmatter children recursively.
|
||||||
if (item.page.children) {
|
if (item.page.children) {
|
||||||
|
assertUniqueChildren(item.page)
|
||||||
item.childPages = (
|
item.childPages = (
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
item.page.children.map(
|
item.page.children.map(
|
||||||
@@ -67,3 +68,15 @@ export default async function createTree(originalPath, langObj) {
|
|||||||
|
|
||||||
return item
|
return item
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function assertUniqueChildren(page) {
|
||||||
|
if (page.children.length !== new Set(page.children).size) {
|
||||||
|
const count = {}
|
||||||
|
page.children.forEach((entry) => (count[entry] = 1 + (count[entry] || 0)))
|
||||||
|
let msg = `${page.relativePath} has duplicates in the 'children' key.`
|
||||||
|
for (const [entry, times] of Object.entries(count)) {
|
||||||
|
if (times > 1) msg += ` '${entry}' is repeated ${times} times. `
|
||||||
|
}
|
||||||
|
throw new Error(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { jest } from '@jest/globals'
|
import { jest } from '@jest/globals'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { loadPages, loadPageMap, correctTranslationOrphans } from '../../lib/page-data.js'
|
import { loadPages, correctTranslationOrphans } from '../../lib/page-data.js'
|
||||||
import libLanguages from '../../lib/languages.js'
|
import libLanguages from '../../lib/languages.js'
|
||||||
import { liquid } from '../../lib/render-content/index.js'
|
import { liquid } from '../../lib/render-content/index.js'
|
||||||
import patterns from '../../lib/patterns.js'
|
import patterns from '../../lib/patterns.js'
|
||||||
@@ -152,29 +152,4 @@ describe('pages module', () => {
|
|||||||
expect(liquidErrors.length, failureMessage).toBe(0)
|
expect(liquidErrors.length, failureMessage).toBe(0)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('loadPageMap', () => {
|
|
||||||
let pageMap
|
|
||||||
beforeAll(async () => {
|
|
||||||
pageMap = await loadPageMap(pages)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('yields a non-empty object with more unique entries than pages', async () => {
|
|
||||||
// Why does it contain MORE unique entries, you ask?
|
|
||||||
// TL;DR: The pages array contains one item per Page + language, with a `permalinks` array
|
|
||||||
// property for each product version supported (free-pro-team, enterprise-server@3.0, etc.)
|
|
||||||
// The pageMap, on the other hand, is keyed by unique URLs, so it has 1-N (where N is the
|
|
||||||
// number of product versions supported) keys pointing to the same Page + language object
|
|
||||||
|
|
||||||
expect(Array.isArray(pageMap)).toBe(false)
|
|
||||||
expect(Object.keys(pageMap).length).toBeGreaterThan(pages.length)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('has an identical key list to the deep permalinks of the array', async () => {
|
|
||||||
const allPermalinks = pages.flatMap((page) => page.permalinks.map((pl) => pl.href)).sort()
|
|
||||||
const allPageUrls = Object.keys(pageMap).sort()
|
|
||||||
|
|
||||||
expect(allPageUrls).toEqual(allPermalinks)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user