Skip to content

Commit

Permalink
Merge pull request #2 from CloudCannon/multilingual
Browse files Browse the repository at this point in the history
Support for defaultContentLanguageInSubdir
  • Loading branch information
NJKode authored Dec 13, 2022
2 parents 1ae50ea + 1f81996 commit ba780a9
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ export async function getConfig(hugoConfig) {
...file,
base_url: baseUrl === '/' ? '' : baseUrl,
source: file.source || paths.source || '',
multilingual: {
languages: hugoConfig.languages || [],
defaultContentLanguage: hugoConfig.defaultContentLanguage || '',
defaultContentLanguageInSubdir: hugoConfig.defaultContentLanguageInSubdir || false,
disableLanguages: hugoConfig.disableLanguages || [],
},
paths: {
...paths,
...file.paths
Expand Down
25 changes: 19 additions & 6 deletions src/generators/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,36 @@ function isIndex(path) {
return !!path.match(/\/_?index\.(md|html?)$/);
}

export function getPageUrlFromPath(path, contentDir) {
export function getPageUrlFromPath(path, contentDir, multilingualOptions = {}) {
if (!isIndex(path)) {
return;
}

return path
const languageCodes = pathHelper.getSupportedLanguages();
const defaultLanguage = multilingualOptions.defaultContentLanguage;
const prependLanguageCode = multilingualOptions.defaultContentLanguageInSubdir === true;

let unprocessedUrl = path
.replace(`${contentDir || ''}/`, '/')
.replace(/\/_?index\.(md|html?)/, '/')
.replace(/\/_?index\.(md|html?)/, '/');

if (!!defaultLanguage && prependLanguageCode) {
const rootFolder = unprocessedUrl.split('/')?.[1] || '/';
if (rootFolder && !(languageCodes.includes(rootFolder))) {
unprocessedUrl = `/${defaultLanguage}${unprocessedUrl}`;
}
}

return unprocessedUrl
.replace(/\/+/g, '/');
}

async function processItem(path, collectionKey, hugoUrls) {
async function processItem({ path, collectionKey, hugoUrls, multilingual }) {
const paths = pathHelper.getPaths();
const parsed = await parseFile(join(paths.source, path));

const item = {
url: hugoUrls[path] || getPageUrlFromPath(path, paths.content) || '',
url: hugoUrls[path] || getPageUrlFromPath(path, paths.content, multilingual) || '',
path,
collection: collectionKey,
...parsed
Expand Down Expand Up @@ -190,7 +203,7 @@ export async function getCollectionsAndConfig(config, hugoUrls) {
}

log(`Parsing ${chalk.green(path)} into ${chalk.bold(collectionKey)} collection`, 'debug');
const item = await processItem(path, collectionKey, hugoUrls);
const item = await processItem({ path, collectionKey, hugoUrls, multilingual: config.multilingual });
collections[collectionKey] = collections[collectionKey] || [];
collections[collectionKey].push(item);

Expand Down
3 changes: 3 additions & 0 deletions src/generators/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export async function getInfo(hugoConfig, options) {
const data = await getData(config);
const generator = await getGenerator(hugoConfig);

// multlingual not needed in info.json
delete config.multilingual;

return {
...config,
time: new Date().toISOString(),
Expand Down
13 changes: 12 additions & 1 deletion src/helpers/paths.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirname, join } from 'path';
import { join } from 'path';
import { getGlob } from './globs.js';

let cachedPaths;
Expand Down Expand Up @@ -46,6 +46,15 @@ export function getSupportedLanguages(hugoConfig = {}) {
return cachedLanguages;
}

export function clearAllCachedItems() {
clearCachedLanguages();
clearCachedLayouts();
}

export function clearCachedLanguages() {
cachedLanguages = null;
}

export function clearCachedLayouts() {
cachedLayouts = null;
}
Expand Down Expand Up @@ -117,6 +126,8 @@ export default {
getPaths,
getSupportedLanguages,
clearCachedLayouts,
clearCachedLanguages,
clearAllCachedItems,
generatePaths,
getDataPaths,
getLayoutTree,
Expand Down
7 changes: 4 additions & 3 deletions test/generators/collections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('collections generator', function () {

describe('getLayout', function () {
before(function () {
pathHelper.clearCachedLayouts();
pathHelper.clearAllCachedItems();
mock(testFileStructure);
pathHelper.getSupportedLanguages({ languages: { en: {} } });
});
Expand Down Expand Up @@ -175,7 +175,7 @@ describe('collections generator', function () {

after(function () {
mock.restore();
pathHelper.clearCachedLayouts();
pathHelper.clearAllCachedItems();
});
});

Expand All @@ -185,7 +185,7 @@ describe('collections generator', function () {
...collectionFiles,
'data/staff_members': { 'anna.yml': '' }
};

pathHelper.clearAllCachedItems();
mock(fileStructure);
});

Expand Down Expand Up @@ -306,6 +306,7 @@ describe('collections generator', function () {

after(function () {
mock.restore();
pathHelper.clearAllCachedItems();
});
});
});
2 changes: 1 addition & 1 deletion test/helpers/paths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('pathHelper', function () {

describe('getLayoutTree', function () {
before(function () {
pathHelper.clearCachedLayouts();
pathHelper.clearAllCachedItems();
});

it('should create a layout tree using layout file structure', async function () {
Expand Down
4 changes: 4 additions & 0 deletions test/integration/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ describe('integration should generate info.json', function () {
await run('collections-config-override');
});

it('with multilingual pages', async function () {
await run('multilingual');
});

it('with legacy config', async function () {
await run('legacy', ['--config', 'cloudcannon.toml,config.toml']);
});
Expand Down
49 changes: 49 additions & 0 deletions test/integration/multilingual.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"base_url": "",
"source": "",
"paths": {
"source": "",
"archetypes": "archetypes",
"assets": "assets",
"content": "content",
"pages": "content",
"data": "data",
"layouts": "layouts",
"publish": "public",
"static": "static",
"uploads": "static/uploads",
"config": ""
},
"time": "UNCHECKED",
"version": "0.0.3",
"cloudcannon": "UNCHECKED",
"generator": "UNCHECKED",
"collections_config": {
"pages": {
"output": true,
"filter": "strict",
"parse_branch_index": true,
"auto_discovered": true,
"path": "content"
}
},
"collections": {
"pages": [
{
"url": "/en/",
"path": "content/_index.md",
"collection": "pages",
"title": "index",
"content_path": "_index.md",
"layout": "index"
},
{
"url": "/en/about/",
"path": "content/about.md",
"collection": "pages",
"title": "About",
"content_path": "about.md"
}
]
}
}
6 changes: 6 additions & 0 deletions test/integration/multilingual/archetypes/default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
---

16 changes: 16 additions & 0 deletions test/integration/multilingual/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
baseURL = 'http://example.org/'
languageCode = 'en-us'
title = 'My New Hugo Site'
disableKinds = ['term', 'taxonomy', 'section']

defaultContentLanguage = "en"
defaultContentLanguageInSubdir = true

[languages]
[languages.de]
title = 'Mein blog'
contentDir = 'content/de/nested'
weight = 2
[languages.en]
title = 'My blog'
weight = 1
5 changes: 5 additions & 0 deletions test/integration/multilingual/content/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: index
---

Home page content.
5 changes: 5 additions & 0 deletions test/integration/multilingual/content/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: About
---

We are a company.
5 changes: 5 additions & 0 deletions test/integration/multilingual/layouts/_default/_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{ define "main" }}
<main>
{{ .Content }}
</main>
{{ end }}
10 changes: 10 additions & 0 deletions test/integration/multilingual/layouts/_default/baseof.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>

</head>

<body>
{{ block "main" . }} {{ end }}
</body>
</html>
3 changes: 3 additions & 0 deletions test/integration/multilingual/layouts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<main>
{{ .Content }}
</main>

0 comments on commit ba780a9

Please sign in to comment.