Skip to content

Commit

Permalink
updated multilanguage test
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Dec 2, 2023
1 parent 898673c commit 7530a00
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 7 deletions.
1 change: 1 addition & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@

- Apply the default language to all pages without defined language.
- Removed the ability to insert translations in the middle of the data object.
- The uniqueness of a page is defined by the combination of id + type.

## `sass` Plugin

Expand Down
9 changes: 6 additions & 3 deletions core/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export interface RawData {
/** The date creation of the page */
date?: Date | string | number;

/** To configure the render order of a page */
/** To configure the rendering order of a page */
renderOrder?: number;

/** The raw content of a page */
Expand All @@ -187,7 +187,7 @@ export interface RawData {
mergedKeys?: Record<string, MergeStrategy>;

/** Whether render this page on demand or not */
ondemand?: boolean;
onDemand?: boolean;

// deno-lint-ignore no-explicit-any
[index: string]: any;
Expand All @@ -198,7 +198,10 @@ export interface Data extends RawData {
/** The language of the page */
lang?: string;

/** The id of the page (used to join different pages as versions of the same content) */
/** The type of the page (used to group pages in collections) */
type?: string;

/** The id of the page (used to identify a page in a collection) */
id?: string | number;

/** List of tags assigned to a page or folder */
Expand Down
6 changes: 4 additions & 2 deletions plugins/multilanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ export default function multilanguage(userOptions: Options) {
}

// Create the alternates object if it doesn't exist
const { id } = data;
const { id, type } = data;
if (data.alternates || id === undefined) {
continue;
}

const alternates: Data[] = [];
const alternatePages = allPages.filter((page) => page.data.id == id);
const alternatePages = allPages.filter((page) =>
page.data.id == id && page.data.type === type
);

options.languages.forEach((lang) => {
const page = alternatePages.find((page) => page.data.lang === lang);
Expand Down
179 changes: 177 additions & 2 deletions tests/__snapshots__/multilanguage.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ snapshot[`multilanguage plugin 1`] = `
"/pages/page1_en.md",
"/pages/page1_gl.md",
"/pagination.page.js",
"/types",
"/types/article.md",
"/types/article_gl.md",
"/types/post.md",
"/types/post_gl.md",
],
}
`;
Expand Down Expand Up @@ -408,6 +413,10 @@ snapshot[`multilanguage plugin 3`] = `
<li>page-gl</li>
<li>article 1</li>
<li>post 1</li>
</body></html>',
data: {
alternates: "Array(2)",
Expand Down Expand Up @@ -437,7 +446,7 @@ snapshot[`multilanguage plugin 3`] = `
"previous",
"next",
],
results: "Array(2)",
results: "Array(4)",
search: [],
tags: "Array(0)",
url: "/gl/page-1/",
Expand Down Expand Up @@ -467,6 +476,8 @@ snapshot[`multilanguage plugin 3`] = `
<li>page-en</li>
<li>post 1</li>
</body></html>',
data: {
alternates: "Array(2)",
Expand Down Expand Up @@ -496,7 +507,7 @@ snapshot[`multilanguage plugin 3`] = `
"previous",
"next",
],
results: "Array(3)",
results: "Array(4)",
search: [],
tags: "Array(0)",
url: "/en/page-1/",
Expand All @@ -508,5 +519,169 @@ snapshot[`multilanguage plugin 3`] = `
remote: undefined,
},
},
{
content: \`<!DOCTYPE html>
<html lang="it"><head><link rel="alternate" hreflang="it" href="http://localhost/it/types/article/">
<link rel="alternate" hreflang="gl" href="http://localhost/gl/types/article_gl/">
</head><body><p>Questo è l'articolo 1</p>
</body></html>\`,
data: {
alternates: "Array(2)",
basename: "article",
children: "<p>Questo è l'articolo 1</p>
",
content: "Questo è l'articolo 1
",
date: [],
id: 1,
lang: "it",
mergedKeys: [
"tags",
"en",
"fr",
"it",
"gl",
],
page: [
"src",
"data",
],
paginate: "paginate",
search: [],
tags: "Array(0)",
title: "article 1",
type: "article",
url: "/it/types/article/",
},
src: {
asset: false,
ext: ".md",
path: "/types/article",
remote: undefined,
},
},
{
content: '<!DOCTYPE html>
<html lang="gl"><head><link rel="alternate" hreflang="it" href="http://localhost/it/types/article/">
<link rel="alternate" hreflang="gl" href="http://localhost/gl/types/article_gl/">
</head><body><p>Este é o artigo 1</p>
</body></html>',
data: {
alternates: "Array(2)",
basename: "article_gl",
children: "<p>Este é o artigo 1</p>
",
content: "Este é o artigo 1
",
date: [],
id: 1,
lang: "gl",
mergedKeys: [
"tags",
"en",
"fr",
"it",
"gl",
],
page: [
"src",
"data",
],
paginate: "paginate",
search: [],
tags: "Array(0)",
title: "article 1",
type: "article",
url: "/gl/types/article_gl/",
},
src: {
asset: false,
ext: ".md",
path: "/types/article_gl",
remote: undefined,
},
},
{
content: '<!DOCTYPE html>
<html lang="en"><head><link rel="alternate" hreflang="en" href="http://localhost/en/types/post/">
<link rel="alternate" hreflang="gl" href="http://localhost/gl/types/post_gl/">
</head><body><p>This is the post 1</p>
</body></html>',
data: {
alternates: "Array(2)",
basename: "post",
children: "<p>This is the post 1</p>
",
content: "This is the post 1
",
date: [],
id: 1,
lang: "en",
mergedKeys: [
"tags",
"en",
"fr",
"it",
"gl",
],
page: [
"src",
"data",
],
paginate: "paginate",
search: [],
tags: "Array(0)",
title: "post 1",
type: "post",
url: "/en/types/post/",
},
src: {
asset: false,
ext: ".md",
path: "/types/post",
remote: undefined,
},
},
{
content: '<!DOCTYPE html>
<html lang="gl"><head><link rel="alternate" hreflang="en" href="http://localhost/en/types/post/">
<link rel="alternate" hreflang="gl" href="http://localhost/gl/types/post_gl/">
</head><body><p>Este é o post 1</p>
</body></html>',
data: {
alternates: "Array(2)",
basename: "post_gl",
children: "<p>Este é o post 1</p>
",
content: "Este é o post 1
",
date: [],
id: 1,
lang: "gl",
mergedKeys: [
"tags",
"en",
"fr",
"it",
"gl",
],
page: [
"src",
"data",
],
paginate: "paginate",
search: [],
tags: "Array(0)",
title: "post 1",
type: "post",
url: "/gl/types/post_gl/",
},
src: {
asset: false,
ext: ".md",
path: "/types/post_gl",
remote: undefined,
},
},
]
`;
8 changes: 8 additions & 0 deletions tests/assets/multilanguage/types/article.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
lang: it
type: article
id: 1
title: article 1
---

Questo è l'articolo 1
8 changes: 8 additions & 0 deletions tests/assets/multilanguage/types/article_gl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
lang: gl
type: article
id: 1
title: article 1
---

Este é o artigo 1
8 changes: 8 additions & 0 deletions tests/assets/multilanguage/types/post.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
lang: en
type: post
id: 1
title: post 1
---

This is the post 1
8 changes: 8 additions & 0 deletions tests/assets/multilanguage/types/post_gl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
lang: gl
type: post
id: 1
title: post 1
---

Este é o post 1

0 comments on commit 7530a00

Please sign in to comment.