Skip to content

Commit addde41

Browse files
committed
hiding non-localized content
1 parent 4b7cbdd commit addde41

File tree

17 files changed

+96
-131
lines changed

17 files changed

+96
-131
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ A map-centric website backed by a Core Data project and Typesense index.
55
## Getting Started
66

77
#### Requirements
8+
89
- Node 20.x
910
- Netlify CLI
1011
- Core Data Project
@@ -19,6 +20,7 @@ Copy the `.env.example` file to `.env` and enter required variables. For a local
1920
Add a `/public/config.dev.json` file, which will be ignored by Git, to copy local config settings when starting the application. Use the `config.json` table below to configure your project.
2021

2122
To start, run:
23+
2224
```
2325
npm install && netlify dev
2426
```
@@ -31,7 +33,7 @@ npm install && netlify dev
3133

3234
#### Create a content repository
3335

34-
On GitHub, create a new content repository. The posts, paths, and pages records you create will be stored here, as well as any i18n, your project configuration, and default user accounts.
36+
On GitHub, create a new content repository. The posts, paths, and pages records you create will be stored here, as well as any i18n, your project configuration, and default user accounts.
3537

3638
###### Directory Structure
3739

@@ -81,10 +83,10 @@ The `title` attribute will be set as the `<title>` element of the HTML page. Set
8183
Copy the `/public/config.json` file into your content repository to `/content/settings/config.json` and adjust the configuration as desired.
8284

8385
| Key | Type | Description |
84-
|----------------------------------------|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
86+
| -------------------------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
8587
| content | Object | TinaCMS content collections configuration |
8688
| content.collections | Array | Array of content keys to allow for data entry and routing: "posts", "paths" |
87-
| content.localize_pages | Boolean | If `true` pages content will be pulled from a locale directory (e.g. `/en/My-Awesome-Page.mdx`) |
89+
| content.localize_content | Boolean | If `true` pages and other content will be pulled from a locale directory (e.g. `/en/My-Awesome-Page.mdx`) |
8890
| core_data | Object | Core Data configuration |
8991
| core_data.url | String | URL of the Core Data application |
9092
| core_data.project_ids | Array | Array of Core Data project IDs as strings |
@@ -99,11 +101,11 @@ Copy the `/public/config.json` file into your content repository to `/content/se
99101
| layers.overlay | Boolean | If `true`, map layer will be rendered as overlay layer. If `false`, map layer will be rendered as base layer |
100102
| result_filtering | Object | Lists of fields and relationships to exclude per model. |
101103
| result_filtering.events.exclude | Array | List of attributes, user defined fields and associations to be excluded from the search detail panel and detail pages. See [Search detail filtering](search-detail-filtering.md) |
102-
| result_filtering.instances.exclude | Array | List of attributes, user defined fields and associations to be excluded from the search detail panel and detail pages. See [Search detail filtering](search-detail-filtering.md) |
104+
| result_filtering.instances.exclude | Array | List of attributes, user defined fields and associations to be excluded from the search detail panel and detail pages. See [Search detail filtering](search-detail-filtering.md) |
103105
| result_filtering.items.exclude | Array | List of attributes, user defined fields and associations to be excluded from the search detail panel and detail pages. See [Search detail filtering](search-detail-filtering.md) |
104-
| result_filtering.organizations.exclude | Array | List of attributes, user defined fields and associations to be excluded from the search detail panel and detail pages. See [Search detail filtering](search-detail-filtering.md) |
105-
| result_filtering.people.exclude | Array | List of attributes, user defined fields and associations to be excluded from the search detail panel and detail pages. See [Search detail filtering](search-detail-filtering.md) |
106-
| result_filtering.places.exclude | Array | List of attributes, user defined fields and associations to be excluded from the search detail panel and detail pages. See [Search detail filtering](search-detail-filtering.md) |
106+
| result_filtering.organizations.exclude | Array | List of attributes, user defined fields and associations to be excluded from the search detail panel and detail pages. See [Search detail filtering](search-detail-filtering.md) |
107+
| result_filtering.people.exclude | Array | List of attributes, user defined fields and associations to be excluded from the search detail panel and detail pages. See [Search detail filtering](search-detail-filtering.md) |
108+
| result_filtering.places.exclude | Array | List of attributes, user defined fields and associations to be excluded from the search detail panel and detail pages. See [Search detail filtering](search-detail-filtering.md) |
107109
| result_filtering.works.exclude | Array | List of attributes, user defined fields and associations to be excluded from the search detail panel and detail pages. See [Search detail filtering](search-detail-filtering.md) |
108110
| search | Array | Search UI configuration |
109111
| search.facets | Array | Search facets configuration. |
@@ -208,4 +210,4 @@ An `x-typesense-api-key` header will need to be added to the request.
208210

209211
## Core Data Fields and Relationships
210212

211-
To find the UUID values for Core Data user-defined fields and relationships, the "Identifier" column can be added to the respective lists within the Core Data application under Project Settings > Configure.
213+
To find the UUID values for Core Data user-defined fields and relationships, the "Identifier" column can be added to the respective lists within the Core Data application under Project Settings > Configure.

docs/configuration-schema.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Array<"paths" | "posts">
1818

1919
Required: No
2020

21-
### localize_pages
21+
### localize_content
2222

2323
If set to `true`, pages will be nested within a localized directory (e.g `/pages/en/Home.mdx`). If `false`, pages will not be localized and placed in the root `/pages` directory.
2424

src/backend/tina/i18n.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import _ from 'underscore';
88
* @param items
99
*/
1010
export const filterAll = async (locale: string, items: Array<any>) => {
11-
if (!config.content.localize_pages) {
11+
if (!config.content.localize_content) {
1212
return items;
1313
}
1414

@@ -35,11 +35,8 @@ export const filterAll = async (locale: string, items: Array<any>) => {
3535

3636
if (localized[locale]) {
3737
filtered.push(localized[locale]);
38-
} else if (localized[config.i18n.default_locale]) {
39-
filtered.push(localized[config.i18n.default_locale])
4038
}
4139
});
42-
4340
return filtered;
4441
};
4542

@@ -53,7 +50,7 @@ export const filterAll = async (locale: string, items: Array<any>) => {
5350
export const fetchOne = async (locale: string, slug: string, query: any) => {
5451
let response;
5552

56-
if (!config.content.localize_pages) {
53+
if (!config.content.localize_content) {
5754
response = await query({ relativePath: `${slug}.mdx` });
5855
} else {
5956
response = await query({ relativePath: `${locale}/${slug}.mdx` });

src/backend/tina/index.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,26 @@ export const fetchPages = async (locale: string, params?: any) => {
2828
return filterAll(locale, pages);
2929
};
3030

31-
export const fetchPath = async (slug: string) => {
32-
const response = await client.queries.path({ relativePath: `${slug}.mdx`});
31+
export const fetchPath = async (locale: string, slug: string) => {
32+
const response = await fetchOne(locale, slug, client.queries.path);
3333
return response.data?.path;
3434
};
3535

36-
export const fetchPaths = async () => {
36+
export const fetchPaths = async (locale: string) => {
3737
const response = await client.queries.pathConnection();
38-
return response.data?.pathConnection?.edges?.map((item) => item?.node);
38+
const paths = response.data?.pathConnection?.edges?.map((item) => item?.node);
39+
40+
return filterAll(locale, paths)
3941
};
4042

41-
export const fetchPost = async (slug: string) => {
42-
const response = await client.queries.post({ relativePath: `${slug}.mdx`});
43+
export const fetchPost = async (locale: string, slug: string) => {
44+
const response = await fetchOne(locale, slug, client.queries.post);
4345
return response.data?.post;
4446
};
4547

46-
export const fetchPosts = async () => {
48+
export const fetchPosts = async (locale: string) => {
4749
const response = await client.queries.postConnection();
48-
return response.data?.postConnection?.edges?.map((item) => item?.node);
50+
const posts = response.data?.postConnection?.edges?.map((item) => item?.node);
51+
52+
return filterAll(locale, posts)
4953
};
Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
---
22
import PathViewer from "@apps/paths/PathViewer";
3-
import { fetchPath, fetchPaths } from '@backend/tina';
4-
import config from '@config';
5-
import { getTranslations } from '@backend/i18n';
3+
import { fetchPath, fetchPaths } from "@backend/tina";
4+
import config from "@config";
5+
import { getTranslations } from "@backend/i18n";
66
import Layout from "@layouts/Layout.astro";
7-
import { hasPathsContent } from '@utils/config';
8-
import _ from 'underscore';
7+
import { hasPathsContent } from "@utils/config";
8+
import _ from "underscore";
99
1010
const { slug } = Astro.params;
11-
const path = await fetchPath(slug);
11+
const locale = Astro.currentLocale;
12+
const path = await fetchPath(locale, slug);
1213
13-
const { t } = await getTranslations(Astro.currentLocale);
14-
const title = path?.title || t('paths');
14+
const { t } = await getTranslations(locale);
15+
const title = path?.title || t("paths");
1516
1617
export const getStaticPaths = async () => {
1718
if (!hasPathsContent(config)) {
@@ -20,33 +21,18 @@ export const getStaticPaths = async () => {
2021
2122
const staticPaths = [];
2223
23-
const locales = config.i18n.locales;
24-
const paths = await fetchPaths();
24+
for (const lang of config.i18n.locales) {
25+
const paths = await fetchPaths(lang);
2526
26-
_.each(locales, (lang) => {
27-
_.each(paths, (path) => {
28-
staticPaths.push({ params: {
29-
lang,
30-
slug: path?._sys.filename
31-
}})
32-
});
33-
});
27+
for (const path of paths) {
28+
paths.push({ params: { lang, slug: path._sys.filename } });
29+
}
30+
}
3431
35-
return staticPaths;
32+
return paths;
3633
};
37-
3834
---
3935

40-
<Layout
41-
fullscreen
42-
t={t}
43-
tab='paths'
44-
title={title}
45-
>
46-
{ path && (
47-
<PathViewer
48-
path={path}
49-
client:only='react'
50-
/>
51-
)}
52-
</Layout>
36+
<Layout fullscreen t={t} tab="paths" title={title}>
37+
{path && <PathViewer path={path} client:only="react" />}
38+
</Layout>

src/pages/[lang]/paths/index.astro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { getRelativeLocaleUrl } from 'astro:i18n';
1111
import _ from 'underscore';
1212
1313
const { t } = await getTranslations(Astro.currentLocale);
14-
const paths = await fetchPaths();
14+
const paths = await fetchPaths(Astro.currentLocale);
1515
1616
export const getStaticPaths = () => {
1717
if (!hasPathsContent(config)) {
@@ -38,7 +38,7 @@ export const getStaticPaths = () => {
3838
{ t('paths') }
3939
</h1>
4040
<Cards>
41-
{ _.map(paths, (path) => (
41+
{ paths?.length ? _.map(paths, (path) => (
4242
<Card
4343
imageUrl={path?.image}
4444
alt={path?.imageAlt}
@@ -48,7 +48,7 @@ export const getStaticPaths = () => {
4848
author={path?.author}
4949
date={path?.date}
5050
/>
51-
))}
51+
)) : <p class="italic text-sm">({ t('none') })</p> }
5252
</Cards>
5353
</Container>
5454
</Layout>

src/pages/[lang]/posts/[slug].astro

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ import { hasPostsContent } from "@utils/config";
1010
import _ from "underscore";
1111
1212
const { slug } = Astro.params;
13-
const post = await fetchPost(slug);
1413
1514
const locale = Astro.currentLocale;
1615
17-
const localePost = getLocalizedContent(post, locale.replaceAll("-", "_")); //this is to deal with Tina's field naming restrictions
18-
16+
const post = await fetchPost(locale, slug);
1917
const { t } = await getTranslations(locale);
2018
2119
export const getStaticPaths = async () => {
@@ -24,22 +22,17 @@ export const getStaticPaths = async () => {
2422
}
2523
2624
const staticPaths = [];
25+
const paths = [];
26+
27+
for (const lang of config.i18n.locales) {
28+
const posts = await fetchPosts(lang);
29+
30+
for (const post of posts) {
31+
paths.push({ params: { lang, slug: post._sys.filename } });
32+
}
33+
}
2734
28-
const locales = config.i18n.locales;
29-
const posts = await fetchPosts();
30-
31-
_.each(locales, (lang) => {
32-
_.each(posts, (post) => {
33-
staticPaths.push({
34-
params: {
35-
lang,
36-
slug: post?._sys.filename,
37-
},
38-
});
39-
});
40-
});
41-
42-
return staticPaths;
35+
return paths;
4336
};
4437
---
4538

src/pages/[lang]/posts/index.astro

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ import Card from '@components/Card.astro';
55
import Cards from '@components/Cards.astro';
66
import Container from '@components/Container.astro';
77
import config from '@config';
8-
import { getLocalizedContent } from '@i18n/utils';
98
import Layout from '@layouts/Layout.astro';
109
import { hasPostsContent } from '@utils/config';
1110
import { getRelativeLocaleUrl } from 'astro:i18n';
1211
import _ from 'underscore';
1312
1413
const { t } = await getTranslations(Astro.currentLocale);
15-
const posts = await fetchPosts();
14+
const posts = await fetchPosts(Astro.currentLocale);
1615
1716
export const getStaticPaths = () => {
1817
if (!hasPostsContent(config)) {
@@ -22,10 +21,6 @@ export const getStaticPaths = () => {
2221
return _.map(config.i18n.locales, (lang) => ({ params: { lang }}));
2322
}
2423
25-
//Localize the post titles
26-
const localeField = Astro.currentLocale.replaceAll('-', '_') //this is to deal with Tina's field naming conventions
27-
const localizedPosts = _.map(posts, (post: any) => (getLocalizedContent(post, localeField)))
28-
2924
---
3025

3126
<Layout
@@ -43,7 +38,7 @@ const localizedPosts = _.map(posts, (post: any) => (getLocalizedContent(post, lo
4338
{ t('posts') }
4439
</h1>
4540
<Cards>
46-
{ _.map(localizedPosts, (post) => (
41+
{ posts?.length ? _.map(posts, (post) => (
4742
<Card
4843
imageUrl={post?.cardImage}
4944
alt={post?.imageAlt}
@@ -53,7 +48,7 @@ const localizedPosts = _.map(posts, (post: any) => (getLocalizedContent(post, lo
5348
author={post.author}
5449
date={post.date}
5550
/>
56-
))}
51+
)) : <p class="italic text-sm">({ t('none') })</p>}
5752
</Cards>
5853
</Container>
5954
</Layout>

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export interface SearchConfig {
5757
export interface Configuration {
5858
content?: {
5959
collections?: Array<String>,
60-
localize_pages?: boolean
60+
localize_content?: boolean
6161
};
6262

6363
core_data: {

src/utils/nav.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import { fetchPages } from '@backend/tina';
2+
import config from '@config';
23
import _ from 'underscore';
34

45
/**
5-
* Returns the first page flagged as "home_page".
6+
* Returns the first page flagged as "home_page". If none exists for the current locale, will default to the configured `default_locale` if possible.
67
*
78
* @param locale
89
*/
910
export const getHomepage = async (locale: string) => {
1011
const pages = await fetchPages(locale, { filter: { home_page: { eq: true } } });
11-
const [page, ] = pages;
12+
let page: any;
13+
if (pages && pages.length) {
14+
page = pages[0];
15+
} else if (config.i18n.default_locale) {
16+
const defaultPages = await fetchPages(config.i18n.default_locale, { filter: { home_page: { eq: true } } });
17+
if (defaultPages && defaultPages.length) {
18+
page = defaultPages[0];
19+
}
20+
}
1221

1322
return page;
1423
};

0 commit comments

Comments
 (0)