@levino/shipyard-blog 0.4.2 → 0.4.3
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.
- package/astro/BlogEntry.astro +4 -8
- package/astro/BlogIndex.astro +8 -10
- package/astro/Layout.astro +28 -15
- package/package.json +2 -2
- package/src/index.ts +22 -24
package/astro/BlogEntry.astro
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
1
1
|
---
|
|
2
|
+
import { i18n } from 'astro:config/server'
|
|
2
3
|
import { getCollection, render } from 'astro:content'
|
|
3
|
-
import config from 'virtual:shipyard/config'
|
|
4
4
|
import { TableOfContents } from '@levino/shipyard-base/components'
|
|
5
5
|
import Layout from './Layout.astro'
|
|
6
6
|
|
|
7
7
|
export const getStaticPaths = async () => {
|
|
8
8
|
const blogPosts = await getCollection('blog')
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const hasI18n = Boolean(config.i18n)
|
|
12
|
-
|
|
13
|
-
const getParams = (slug: string, hasI18n: boolean) => {
|
|
14
|
-
if (hasI18n) {
|
|
9
|
+
const getParams = (slug: string) => {
|
|
10
|
+
if (i18n) {
|
|
15
11
|
const [locale, ...rest] = slug.split('/')
|
|
16
12
|
return {
|
|
17
13
|
slug: rest.join('/'),
|
|
@@ -26,7 +22,7 @@ export const getStaticPaths = async () => {
|
|
|
26
22
|
}
|
|
27
23
|
|
|
28
24
|
return blogPosts.map((entry) => ({
|
|
29
|
-
params: getParams(entry.id
|
|
25
|
+
params: getParams(entry.id),
|
|
30
26
|
props: { entry },
|
|
31
27
|
}))
|
|
32
28
|
}
|
package/astro/BlogIndex.astro
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
---
|
|
2
|
+
import { i18n } from 'astro:config/server'
|
|
2
3
|
import { getCollection } from 'astro:content'
|
|
3
|
-
import config from 'virtual:shipyard/config'
|
|
4
4
|
import type { GetStaticPaths } from 'astro'
|
|
5
5
|
import Layout from './Layout.astro'
|
|
6
6
|
|
|
7
7
|
export const getStaticPaths = (() => {
|
|
8
|
-
if (
|
|
9
|
-
return
|
|
8
|
+
if (i18n) {
|
|
9
|
+
return i18n.locales.map((locale) => {
|
|
10
10
|
if (typeof locale !== 'string') {
|
|
11
11
|
throw new Error('Shipyard does only support strings as locales.')
|
|
12
12
|
}
|
|
@@ -22,15 +22,13 @@ export const getStaticPaths = (() => {
|
|
|
22
22
|
}
|
|
23
23
|
}) satisfies GetStaticPaths
|
|
24
24
|
|
|
25
|
-
const { locale } = Astro.params
|
|
26
|
-
|
|
27
25
|
const entries = await getCollection('blog').then((posts) => {
|
|
28
|
-
if (
|
|
26
|
+
if (i18n) {
|
|
29
27
|
// With i18n: filter by locale
|
|
30
28
|
return posts
|
|
31
29
|
.filter(({ id }) => {
|
|
32
30
|
const [postLocale] = id.split('/')
|
|
33
|
-
return postLocale ===
|
|
31
|
+
return postLocale === Astro.currentLocale
|
|
34
32
|
})
|
|
35
33
|
.toSorted((a, b) => b.data.date.getTime() - a.data.date.getTime())
|
|
36
34
|
} else {
|
|
@@ -42,21 +40,21 @@ const entries = await getCollection('blog').then((posts) => {
|
|
|
42
40
|
})
|
|
43
41
|
|
|
44
42
|
const getBlogPostLink = (id: string, locale?: string) => {
|
|
45
|
-
if (
|
|
43
|
+
if (i18n && locale) {
|
|
46
44
|
return id.replace(`${locale}/`, `${locale}/blog/`)
|
|
47
45
|
} else {
|
|
48
46
|
return `blog/${id}`
|
|
49
47
|
}
|
|
50
48
|
}
|
|
51
49
|
|
|
52
|
-
const formatDate = new Intl.DateTimeFormat(
|
|
50
|
+
const formatDate = new Intl.DateTimeFormat(Astro.currentLocale || 'en').format
|
|
53
51
|
---
|
|
54
52
|
|
|
55
53
|
<Layout title="Blog">
|
|
56
54
|
<div class="mx-auto max-w-prose px-8">
|
|
57
55
|
{
|
|
58
56
|
entries.map(async (entry) => (
|
|
59
|
-
<a class="block my-8" href={`/${getBlogPostLink(entry.id,
|
|
57
|
+
<a class="block my-8" href={`/${getBlogPostLink(entry.id, Astro.currentLocale)}`}>
|
|
60
58
|
<div class="prose">
|
|
61
59
|
<div class="text-sm">{formatDate(entry.data.date)}</div>
|
|
62
60
|
<h2 class="my-0">{entry.data.title}</h2>
|
package/astro/Layout.astro
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
+
import { i18n } from 'astro:config/server'
|
|
2
3
|
import { getCollection } from 'astro:content'
|
|
3
4
|
import type { NavigationTree } from '@levino/shipyard-base'
|
|
4
5
|
import { Page as BaseLayout } from '@levino/shipyard-base/layouts'
|
|
@@ -9,21 +10,33 @@ type Props = {
|
|
|
9
10
|
|
|
10
11
|
const locale = Astro.currentLocale
|
|
11
12
|
const { title } = Astro.props
|
|
12
|
-
const entry = await getCollection('blog').then((posts) =>
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
acc
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
)
|
|
13
|
+
const entry = await getCollection('blog').then((posts) => {
|
|
14
|
+
if (i18n) {
|
|
15
|
+
return posts
|
|
16
|
+
.filter(({ id }) => {
|
|
17
|
+
const [postLocale] = id.split('/')
|
|
18
|
+
return postLocale === locale
|
|
19
|
+
})
|
|
20
|
+
.toSorted((a, b) => b.data.date.getTime() - a.data.date.getTime())
|
|
21
|
+
.reduce((acc, { id, data: { title } }, key) => {
|
|
22
|
+
acc[key] = {
|
|
23
|
+
href: `/${locale}/blog/${id.slice(3)}`,
|
|
24
|
+
label: title,
|
|
25
|
+
}
|
|
26
|
+
return acc
|
|
27
|
+
}, {} as NavigationTree)
|
|
28
|
+
} else {
|
|
29
|
+
return posts
|
|
30
|
+
.toSorted((a, b) => b.data.date.getTime() - a.data.date.getTime())
|
|
31
|
+
.reduce((acc, { id, data: { title } }, key) => {
|
|
32
|
+
acc[key] = {
|
|
33
|
+
href: `/blog/${id}`,
|
|
34
|
+
label: title,
|
|
35
|
+
}
|
|
36
|
+
return acc
|
|
37
|
+
}, {} as NavigationTree)
|
|
38
|
+
}
|
|
39
|
+
})
|
|
27
40
|
---
|
|
28
41
|
|
|
29
42
|
<BaseLayout title={title} sidebarNavigation={entry}>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@levino/shipyard-blog",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"author": "",
|
|
18
18
|
"license": "ISC",
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@levino/shipyard-base": "^0.5.
|
|
20
|
+
"@levino/shipyard-base": "^0.5.4"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"astro": "^5.7"
|
package/src/index.ts
CHANGED
|
@@ -7,33 +7,31 @@ export const blogSchema = z.object({
|
|
|
7
7
|
description: z.string(),
|
|
8
8
|
})
|
|
9
9
|
|
|
10
|
-
export default (
|
|
10
|
+
export default (): AstroIntegration => ({
|
|
11
11
|
name: 'shipyard-blog',
|
|
12
12
|
hooks: {
|
|
13
13
|
'astro:config:setup': ({ injectRoute, config }) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
})
|
|
14
|
+
if (config.i18n) {
|
|
15
|
+
// With i18n: use locale prefix
|
|
16
|
+
injectRoute({
|
|
17
|
+
pattern: `/[locale]/blog`,
|
|
18
|
+
entrypoint: `@levino/shipyard-blog/astro/BlogIndex.astro`,
|
|
19
|
+
})
|
|
20
|
+
injectRoute({
|
|
21
|
+
pattern: `/[locale]/blog/[...slug]`,
|
|
22
|
+
entrypoint: `@levino/shipyard-blog/astro/BlogEntry.astro`,
|
|
23
|
+
})
|
|
24
|
+
} else {
|
|
25
|
+
// Without i18n: direct path
|
|
26
|
+
injectRoute({
|
|
27
|
+
pattern: `/blog`,
|
|
28
|
+
entrypoint: `@levino/shipyard-blog/astro/BlogIndex.astro`,
|
|
29
|
+
})
|
|
30
|
+
injectRoute({
|
|
31
|
+
pattern: `/blog/[...slug]`,
|
|
32
|
+
entrypoint: `@levino/shipyard-blog/astro/BlogEntry.astro`,
|
|
33
|
+
})
|
|
34
|
+
}
|
|
37
35
|
},
|
|
38
36
|
},
|
|
39
37
|
})
|