Skip to content

Commit

Permalink
add page name in collection pages
Browse files Browse the repository at this point in the history
  • Loading branch information
lexoyo committed Dec 7, 2023
1 parent 852dcd6 commit f5e2571
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
19 changes: 10 additions & 9 deletions src/client/publication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { getFrontMatter } from './publication'
//})

test('Front matter of a simple page', () => {
expect(() => getFrontMatter({}, 'page-1')).not.toThrow()
expect(getFrontMatter({}, 'page-1')).toEqual(dedent`
expect(() => getFrontMatter({}, 'page-1', '')).not.toThrow()
expect(getFrontMatter({}, 'page-1', '')).toEqual(dedent`
---
permalink: "/page-1/index.html"
\n---\n`)
Expand All @@ -27,11 +27,12 @@ test('Front matter of a collection page', () => {
const settings = {
eleventyPageData: 'directus.posts',
}
expect(() => getFrontMatter(settings, 'page-1')).not.toThrow()
expect(getFrontMatter(settings, 'page-1')).toEqual(dedent`
expect(() => getFrontMatter(settings, 'page-1', 'collectionTest')).not.toThrow()
expect(getFrontMatter(settings, 'page-1', 'collectionTest')).toEqual(dedent`
---
pagination:
data: directus.posts
collection: "collectionTest"
\n---\n`)
})

Expand All @@ -42,8 +43,8 @@ test('Permalink', () => {
eleventyPageData,
eleventyPermalink,
}
expect(() => getFrontMatter(settings, 'page-1')).not.toThrow()
expect(getFrontMatter(settings, 'page-1')).toEqual(dedent`
expect(() => getFrontMatter(settings, 'page-1', '')).not.toThrow()
expect(getFrontMatter(settings, 'page-1', '')).toEqual(dedent`
---
pagination:
data: ${eleventyPageData}
Expand All @@ -57,11 +58,11 @@ test('With languages', () => {
silexLanguagesList: 'fr,en',
silexLanguagesDefault: 'en',
}
expect(() => getFrontMatter(settings, 'page-1', 'fr')).not.toThrow()
expect(getFrontMatter(settings, 'page-1', 'fr')).toEqual(dedent`
expect(() => getFrontMatter(settings, 'page-1', '', 'fr')).not.toThrow()
expect(getFrontMatter(settings, 'page-1', '', 'fr')).toEqual(dedent`
---
pagination:
data: directus.posts
lang: fr
lang: "fr"
\n---\n`)
})
9 changes: 5 additions & 4 deletions src/client/publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export function getPermalink(settings: Silex11tyPluginWebsiteSettings, slug: str
/**
* Get the front matter for a given page
*/
export function getFrontMatter(settings: Silex11tyPluginWebsiteSettings, slug: string, lang: string = ''): string {
export function getFrontMatter(settings: Silex11tyPluginWebsiteSettings, slug: string, collection, lang = ''): string {
const permalink = getPermalink(settings, slug)
return dedent`---
${settings?.eleventyPageData ? `pagination:
Expand All @@ -127,7 +127,8 @@ export function getFrontMatter(settings: Silex11tyPluginWebsiteSettings, slug: s
${settings.eleventyPageReverse ? 'reverse: true' : ''}
` : ''}
${permalink ? `permalink: "${permalink}"` : ''}
${lang ? `lang: ${lang}` : ''}
${lang ? `lang: "${lang}"` : ''}
${collection ? `collection: "${collection}"` : ''}
${settings?.eleventyNavigationKey ? `eleventyNavigation:
key: ${settings.eleventyNavigationKey}
${settings.eleventyNavigationTitle ? `title: ${settings.eleventyNavigationTitle}` : ''}
Expand Down Expand Up @@ -203,7 +204,7 @@ export function transformFiles(editor: DataSourceEditor, options: EleventyPlugin
if(languages && languages.length > 0) {
const pages: ClientSideFileWithContent[] = languages.flatMap(lang => {
// Change the HTML
const frontMatter = getFrontMatter(settings, slug, lang)
const frontMatter = getFrontMatter(settings, slug, page.getName(), lang)
const bodyStates = getBodyStates(page)
const pageFile = {
type: ClientSideFileType.HTML,
Expand All @@ -230,7 +231,7 @@ export function transformFiles(editor: DataSourceEditor, options: EleventyPlugin
data.files.push(...newPages)
} else {
// Change the HTML
const frontMatter = getFrontMatter(settings, slug)
const frontMatter = getFrontMatter(settings, slug, page.getName())
const bodyStates = getBodyStates(page)

// Update the page before it is published
Expand Down

0 comments on commit f5e2571

Please sign in to comment.