@levino/shipyard-blog 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,19 +1,32 @@
1
1
  ---
2
2
  import { getCollection, render } from 'astro:content'
3
+ import config from 'virtual:shipyard/config'
3
4
  import { TableOfContents } from '@levino/shipyard-base/components'
4
5
  import Layout from './Layout.astro'
5
6
 
6
7
  export const getStaticPaths = async () => {
7
- const getParams = (slug: string) => {
8
- const [locale, ...rest] = slug.split('/')
9
- return {
10
- slug: rest.join('/'),
11
- locale,
8
+ const blogPosts = await getCollection('blog')
9
+
10
+ // Check if i18n is configured using Shipyard's config
11
+ const hasI18n = Boolean(config.i18n)
12
+
13
+ const getParams = (slug: string, hasI18n: boolean) => {
14
+ if (hasI18n) {
15
+ const [locale, ...rest] = slug.split('/')
16
+ return {
17
+ slug: rest.join('/'),
18
+ locale,
19
+ }
20
+ } else {
21
+ // For non-i18n, treat the entire slug as the path
22
+ return {
23
+ slug: slug,
24
+ }
12
25
  }
13
26
  }
14
- const blogPosts = await getCollection('blog')
27
+
15
28
  return blogPosts.map((entry) => ({
16
- params: getParams(entry.id),
29
+ params: getParams(entry.id, hasI18n),
17
30
  props: { entry },
18
31
  }))
19
32
  }
@@ -4,33 +4,52 @@ import config from 'virtual:shipyard/config'
4
4
  import type { GetStaticPaths } from 'astro'
5
5
  import Layout from './Layout.astro'
6
6
 
7
- export const getStaticPaths = (() =>
8
- config.i18n.locales.map((locale) => {
9
- if (typeof locale !== 'string') {
10
- throw new Error('Shipyard does only support strings as locales.')
11
- }
12
- return {
13
- params: {
14
- locale,
15
- },
16
- }
17
- })) satisfies GetStaticPaths
7
+ export const getStaticPaths = (() => {
8
+ if (config.i18n) {
9
+ return config.i18n.locales.map((locale) => {
10
+ if (typeof locale !== 'string') {
11
+ throw new Error('Shipyard does only support strings as locales.')
12
+ }
13
+ return {
14
+ params: {
15
+ locale,
16
+ },
17
+ }
18
+ })
19
+ } else {
20
+ // For non-i18n, return a single path without locale
21
+ return [{ params: {} }]
22
+ }
23
+ }) satisfies GetStaticPaths
18
24
 
19
25
  const { locale } = Astro.params
20
26
 
21
- const entries = await getCollection('blog').then((posts) =>
22
- posts
23
- .filter(({ id }) => {
24
- const [postLocale] = id.split('/')
25
- return postLocale === locale
26
- })
27
- .toSorted((a, b) => b.data.date.getTime() - a.data.date.getTime()),
28
- )
27
+ const entries = await getCollection('blog').then((posts) => {
28
+ if (config.i18n && locale) {
29
+ // With i18n: filter by locale
30
+ return posts
31
+ .filter(({ id }) => {
32
+ const [postLocale] = id.split('/')
33
+ return postLocale === locale
34
+ })
35
+ .toSorted((a, b) => b.data.date.getTime() - a.data.date.getTime())
36
+ } else {
37
+ // Without i18n: return all posts
38
+ return posts.toSorted(
39
+ (a, b) => b.data.date.getTime() - a.data.date.getTime(),
40
+ )
41
+ }
42
+ })
29
43
 
30
- const getBlogPostLink = (id: string, locale: string) =>
31
- id.replace(`${locale}/`, `${locale}/blog/`)
44
+ const getBlogPostLink = (id: string, locale?: string) => {
45
+ if (config.i18n && locale) {
46
+ return id.replace(`${locale}/`, `${locale}/blog/`)
47
+ } else {
48
+ return `blog/${id}`
49
+ }
50
+ }
32
51
 
33
- const formatDate = new Intl.DateTimeFormat(locale).format
52
+ const formatDate = new Intl.DateTimeFormat(locale || 'en').format
34
53
  ---
35
54
 
36
55
  <Layout title="Blog">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@levino/shipyard-blog",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -17,12 +17,12 @@
17
17
  "author": "",
18
18
  "license": "ISC",
19
19
  "dependencies": {
20
- "@levino/shipyard-base": "^0.5.1"
20
+ "@levino/shipyard-base": "^0.5.3"
21
21
  },
22
22
  "peerDependencies": {
23
- "astro": "^5"
23
+ "astro": "^5.7"
24
24
  },
25
25
  "devDependencies": {
26
- "astro": "^5"
26
+ "astro": "^5.7"
27
27
  }
28
28
  }
package/src/index.ts CHANGED
@@ -10,16 +10,29 @@ export const blogSchema = z.object({
10
10
  export default (blogPaths: string[]): AstroIntegration => ({
11
11
  name: 'shipyard-blog',
12
12
  hooks: {
13
- 'astro:config:setup': ({ injectRoute }) => {
13
+ 'astro:config:setup': ({ injectRoute, config }) => {
14
14
  blogPaths.forEach((path) => {
15
- injectRoute({
16
- pattern: `/[locale]/${path}`,
17
- entrypoint: `@levino/shipyard-blog/astro/BlogIndex.astro`,
18
- })
19
- injectRoute({
20
- pattern: `/[locale]/${path}/[...slug]`,
21
- entrypoint: `@levino/shipyard-blog/astro/BlogEntry.astro`,
22
- })
15
+ if (config.i18n) {
16
+ // With i18n: use locale prefix
17
+ injectRoute({
18
+ pattern: `/[locale]/${path}`,
19
+ entrypoint: `@levino/shipyard-blog/astro/BlogIndex.astro`,
20
+ })
21
+ injectRoute({
22
+ pattern: `/[locale]/${path}/[...slug]`,
23
+ entrypoint: `@levino/shipyard-blog/astro/BlogEntry.astro`,
24
+ })
25
+ } else {
26
+ // Without i18n: direct path
27
+ injectRoute({
28
+ pattern: `/${path}`,
29
+ entrypoint: `@levino/shipyard-blog/astro/BlogIndex.astro`,
30
+ })
31
+ injectRoute({
32
+ pattern: `/${path}/[...slug]`,
33
+ entrypoint: `@levino/shipyard-blog/astro/BlogEntry.astro`,
34
+ })
35
+ }
23
36
  })
24
37
  },
25
38
  },