1
0
mirror of synced 2025-12-22 11:26:57 -05:00

notice potential repeated children key entries immediately (#31898)

This commit is contained in:
Peter Bengtsson
2022-10-20 18:32:21 +02:00
committed by GitHub
parent 0e03452f9c
commit ff20457d54
2 changed files with 14 additions and 26 deletions

View File

@@ -56,6 +56,7 @@ export default async function createTree(originalPath, langObj) {
// Process frontmatter children recursively.
if (item.page.children) {
assertUniqueChildren(item.page)
item.childPages = (
await Promise.all(
item.page.children.map(
@@ -67,3 +68,15 @@ export default async function createTree(originalPath, langObj) {
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)
}
}

View File

@@ -1,6 +1,6 @@
import { jest } from '@jest/globals'
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 { liquid } from '../../lib/render-content/index.js'
import patterns from '../../lib/patterns.js'
@@ -152,29 +152,4 @@ describe('pages module', () => {
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)
})
})
})