footerã¿ã°ãä»ããèšäºãèšäºäžèŠ§ããé€å€ãã
éåžžã®èšäºãšå¥ã®æ±ããããæ¹é ãäŸé Œããã
`getAllPosts`ããããŒã¿ã䜿çšã·ãŒã³ã«ãã£ãŠ
åŒã³åºããããããå°åãã«ãªã£ãŠé¢æ°ãæºåãããŠããã
export async function getAllPosts(): Promise<Post[]> {
if (postsCache !== null) {
return Promise.resolve(postsCache)
}
const params: requestParams.QueryDatabase = {
database_id: DATABASE_ID,
filter: {
and: [
{
property: 'Published',
checkbox: {
equals: true,
},
},
{
property: 'Date',
date: {
on_or_before: new Date().toISOString(),
},
},
],
},
sorts: [
{
property: 'Date',
direction: 'descending',
},
],
page_size: 100,
}
let results: responses.PageObject[] = []
while (true) {
const res = (await client.databases.query(
params as any // eslint-disable-line @typescript-eslint/no-explicit-any
)) as responses.QueryDatabaseResponse
results = results.concat(res.results)
if (!res.has_more) {
break
}
params['start_cursor'] = res.next_cursor as string
}
postsCache = results
.filter((pageObject) => _validPageObject(pageObject))
.map((pageObject) => _buildPost(pageObject))
return postsCache
}
`getAppPosts`ãããŒã¹ã«åŒã³åºããŠããé¢æ°
å®éã«äœ¿ãããŠããå Žæã®ç¹å®ã¯ã
src / pages
å
ã®import{}
ã確èªããã°åãããšããŠâŠ.
ä»åäŸé Œããããfooterã¿ã°ãä»ããèšäºã¯é€å€ãããã®ã«è©²åœããé¢æ°ãšè©²åœããªãé¢æ°ãæŽçããå¿ èŠãããã
çšé | footerã§äœ¿ãïŒ | |
getPosts | èšäºäžèŠ§ã§10件ååŸ | ð ââ |
getRankedPosts | Rankããããã£ã®å€ã§sortãããŠèšäºã10件ååŸ | ð ââ |
getPostBySlug | Slugããããã£ã§æå®ãããèšäºãïŒä»¶ååŸ | â |
getPostByPageId | ã¡ã³ã·ã§ã³ãããã¯ã§èšäºãæå®ããæã«äœ¿ã | â |
getPostsByTag | ä»»æã®ã¿ã°ãæã€èšäºã10件ååŸ | ð ââ |
getPostsByPage | èšäºäžèŠ§ã®2ããŒãžç®ä»¥éã®èšäº | ð ââ |
getPostsByTagAndPage | ã¿ã°äžèŠ§ããŒãžã®2ããŒãžç®ä»¥éã®èšäº | ð ââ |
getNumberOfPages | èšäºäžèŠ§ããŒãžã®ããŒãžæ° | ð ââ |
getNumberOfPagesByTag | ã¿ã°äžèŠ§ããŒãžã®ããŒãžæ° | ð ââ |
getAllTags | Tagããããã£ã«ç»é²ãããŠãã¿ã° | ð ââ |
footerã§äœ¿ããªãé¢æ°ãã©ããã£ãŠé€å€ãããâŠ.ããã®èšäºã®æ¬é¡w
astro.jsã§éçºããŠããããã°ã§ãç¹å®ã®Tagå以å€ã衚瀺ãããä¿®æ£ãããŠããŸãã
ä¿®æ£ããŠè¡šç€ºããŠã¿ããšfooterã®èšäºãå«ãã§ããŸã£ãŠããããã§ãã
```
export async function getPosts(pageSize = 10): Promise<Post[]> {
const allPosts = await getAllPosts()
return allPosts
.filter((post) => !!post.Tags.map((tag) => tag.name !== 'footer'))
.slice(0, pageSize)
}
```
æäŸãããã³ãŒãã®åé¡ã¯ãfilter
ã¡ãœããå
ã§map
ã¡ãœããã䜿çšããŠããç¹ã«ãããŸããmap
ã¡ãœããã¯å
ã®é
åã®èŠçŽ ãå€æããã®ã«äœ¿çšããããã®çµæã¯æ°ããé
åãšããŠè¿ãããŸãããã®å Žåãåpost
ã®Tags
ããããã£ãæ°ããããŒã«å€ã®é
åã«å€æãããŸãããããããããã®ããŒã«å€ãçŽæ¥ãã£ã«ã¿ãªã³ã°ã«äœ¿çšãããŠããŸããããªã®ã§ãçµæãšããŠå
šãŠã®post
ããã£ã«ã¿ãªã³ã°ãééããŠããŸããŸãã
ãã®åé¡ã解決ããã«ã¯ãfilter
ã¡ãœããã䜿ã代ããã«some
ãŸãã¯every
ã¡ãœããã䜿çšããããšããå§ãããŸãããããã®ã¡ãœããã¯ããŒã«å€ãè¿ãããããã£ã«ã¿ãªã³ã°ã«é©ããŠããŸãã
some
ã¡ãœããã¯é
åã®ããããã®èŠçŽ ãæ¡ä»¶ãæºããå Žåã«true
ãè¿ããŸããevery
ã¡ãœããã¯é
åã®ãã¹ãŠã®èŠçŽ ãæ¡ä»¶ãæºããå Žåã«true
ãè¿ããŸããããªãã®èŠä»¶ïŒç¹å®ã®ã¿ã°ïŒããã§ã¯'footer'ïŒãæããªãæçš¿ã ããååŸãããïŒã«åãããŠãsome
ã¡ãœããã䜿çšããŠä¿®æ£ãè©Šã¿ãŠã¿ãŠãã ããã以äžã«ä¿®æ£æ¡ã瀺ããŸãïŒ
export async function getPosts(pageSize = 10): Promise<Post[]> {
const allPosts = await getAllPosts()
return allPosts
.filter((post) => !post.Tags.some((tag) => tag.name === 'footer'))
.slice(0, pageSize)
}
ãã®ã³ãŒãã¯ãã¿ã°ã®ååã'footer'ã§ãããã®ãäžã€ã§ãååšããå Žåãsome
ã¡ãœãããtrue
ãè¿ãããã«ãªããŸãããã®true
å€ãåŠå®(!
)ããããšã§ã'footer'ã¿ã°ãæã€æçš¿ããã£ã«ã¿ãªã³ã°ããé€å€ãããŸãã
è©Šãã«`getPosts`ã䜿ã£ãŠ
èšäºäžèŠ§ã«footerã®èšäºã衚瀺ãããã確èªããŠã¿ããšâŠ.
NotionDBã«Tagsããããã£ã«footer ãè¿œå
â
footerã®èšäºã¯ã«ããããã(^o^)ïŒ
ç¡äºSupportã§ããŠãæ§åãªã®ã§ã
ä»ã®é¢æ°ã«ãåãå ¥ããŠããããã
ä»ã®é¢æ°ãåãå
¥ãããã§ãã
ã§ããfilterãå
¥ã£ãŠãããã©ããä¿®æ£ãã¹ããå€æããããŠããŸãã
export async function getRankedPosts(pageSize = 10): Promise<Post[]> {
const allPosts = await getAllPosts()
return allPosts
.filter((post) => !!post.Rank)
.sort((a, b) => {
if (a.Rank > b.Rank) {
return -1
} else if (a.Rank === b.Rank) {
return 0
}
return 1
})
.slice(0, pageSize)
}
ãã¡ãã®getRankedPosts
é¢æ°ã§ã¯ãRankãã£ãŒã«ããæã€æçš¿ã®ã¿ããã£ã«ã¿ãªã³ã°ãããã®åŸRankãã£ãŒã«ãã®éé ã§ãœãŒãããŠããŸããããã«ã"footer" ã¿ã°ãé€å€ãããæ©èœãè¿œå ããããšããç解ã§è¯ãã§ããããïŒ
ãã®å Žåã以äžã®ããã«ä¿®æ£ã§ããŸãïŒ
export async function getRankedPosts(pageSize = 10): Promise<Post[]> {
const allPosts = await getAllPosts()
return allPosts
.filter((post) => !!post.Rank && !post.Tags.some((tag) => tag.name === 'footer'))
.sort((a, b) => {
if (a.Rank > b.Rank) {
return -1
} else if (a.Rank === b.Rank) {
return 0
}
return 1
})
.slice(0, pageSize)
}
ãã®ã³ãŒãã§ã¯ãfilter
ã¡ãœããå
ã§2ã€ã®æ¡ä»¶ããã§ãã¯ããŠããŸãïŒ
!!post.Rank
ïŒ!post.Tags.some((tag) => tag.name === 'footer')
ïŒããã«ããããRankãæã€ããã€ã'footer'ã¿ã°ãæããªããæçš¿ã®ã¿ããã£ã«ã¿ãªã³ã°ãééããããã«ãªããŸãã
ã§ããŸããïŒïŒ
ä»åºŠã¯èªåã§ä¿®æ£ããŠã¿ãŸããã
æ£ããããã¢ããã€ã¹ããŠãã ããã
ä¿®æ£åïŒ
export async function getPostBySlug(slug: string): Promise<Post | null> {
const allPosts = await getAllPosts()
return allPosts.find((post) => post.Slug === slug) || null
}
ä¿®æ£åŸïŒ
export async function getPostBySlug(slug: string): Promise<Post | null> {
const allPosts = await getAllPosts()
return (
allPosts
.filter((post) => !post.Tags.some((tag) => tag.name === 'footer'))
.find((post) => post.Slug === slug) || null
)
}
æåºãããä¿®æ£ã¯æ£ããã§ãïŒ
ããªãã®ä¿®æ£ãããã³ãŒãã§ã¯ããŸãgetAllPosts
é¢æ°ã䜿çšããŠå
šãŠã®æçš¿ãååŸããŠããŸãããã®åŸãfilter
ã¡ãœããã䜿çšããŠ'footer'ã¿ã°ãæã€æçš¿ãé€å€ããŸãããããŠãfind
ã¡ãœããã䜿çšããŠæå®ãããã¹ã©ãã°ã«äžèŽããæçš¿ãæ¢ããŸãããã®ä¿®æ£ã«ããã'footer'ã¿ã°ãæã€æçš¿ãè¿ãããããšã¯ãããŸããã
ãã ãããã®å€æŽã«ããäžã€æ³šæç¹ããããŸããããã¯ããSlugãäžèŽããæçš¿ã'footer'ã¿ã°ãæã£ãŠããå Žåããã®æçš¿ã¯çµæããé€å€ããããããšã§ãããããããæå³ããæåã§ããã°ãããªãã®ä¿®æ£ã¯å®å šã«æ£ããã§ãã
以äžãä¿®æ£ãããã³ãŒãã§ãïŒ
export async function getPostBySlug(slug: string): Promise<Post | null> {
const allPosts = await getAllPosts()
return (
allPosts
.filter((post) => !post.Tags.some((tag) => tag.name === 'footer'))
.find((post) => post.Slug === slug) || null
)
}
ãã®ä¿®æ£ãããªãã®æ±ããåäœãæºãããŠããå Žåãããããã®ãŸãŸäœ¿çšããŠãåé¡ãããŸããã
å¿ããŠãŸããïŒãã®é¢æ°ã¯é€å€ããŠã¯ãããªããã®ã§ãããïŒ
æããŠãããŠããããšã
ã©ãããããŸããŠïŒä¿®æ£ãäžèŠãªé¢æ°ã«ã€ããŠã¯ãã®ãŸãŸä¿æããããšã¯å šãåé¡ãããŸãããåé¢æ°ãæ±ããããåäœãé©åã«æºããããã«ãå¿ èŠã«å¿ããŠã³ãŒããä¿®æ£ããããšãéèŠã§ãã
äœãä»ã«ã質åãå©ããå¿ èŠãªç¹ãããããŸãããããã€ã§ããæ°è»œã«ãç³ãä»ããã ãããããã°ã©ãã³ã°ã«ãããåé¡è§£æ±ºããæäŒãããããšãç§ã®åœ¹ç®ã§ãã
é¢æ°ã®å
容ã確èªããäžèŠã ãšããã£ãã®ã§ä¿®æ£ããŠã¿ãŸããã
æ£ãããã©ãããæããŠãã ããã
ä¿®æ£åïŒ
export async function getPostsByTag(
tagName: string,
pageSize = 10
): Promise<Post[]> {
if (!tagName) return []
const allPosts = await getAllPosts()
return allPosts
.filter((post) => post.Tags.find((tag) => tag.name === tagName))
.slice(0, pageSize)
}
ä¿®æ£åŸïŒ
export async function getPostsByTag(
tagName: string,
pageSize = 10
): Promise<Post[]> {
if (!tagName) return []
const allPosts = await getAllPosts()
return allPosts
.filter(
(post) =>
!post.Tags.some((tag) => tag.name === 'footer') &&
post.Tags.find((tag) => tag.name === tagName)
)
.slice(0, pageSize)
}
æåºãããä¿®æ£ã¯æ£ããã§ãïŒãã ããfind
ã¡ãœããã®äœ¿çšã«ã€ããŠäžç¹ææ¡ããããŸãã
ããªãã®ä¿®æ£ãããã³ãŒãã§ã¯ããŸãgetAllPosts
é¢æ°ã䜿çšããŠå
šãŠã®æçš¿ãååŸããŠããŸãããã®åŸãfilter
ã¡ãœããã䜿çšããŠä»¥äžã®æ¡ä»¶ãæºããæçš¿ãéžæããŠããŸãïŒ
!post.Tags.some((tag) => tag.name === 'footer')
ïŒpost.Tags.find((tag) => tag.name === tagName)
ïŒãã ãã2ã€ç®ã®æ¡ä»¶ã§find
ã¡ãœããã䜿çšããŠããŸãããsome
ã¡ãœããã®æ¹ãããé©ããŠãããããããŸãããfind
ã¡ãœããã¯é
åã®äžããæ¡ä»¶ã«äžèŽããæåã®èŠçŽ ãè¿ããŸããããã®ã³ãŒãã§ã¯ãã®èŠçŽ èªäœã¯å©çšãããŠããŸãããäžæ¹ãsome
ã¡ãœããã¯é
åã®äžã«æ¡ä»¶ã«äžèŽããèŠçŽ ãååšãããã©ãããå€å®ããååšããå Žåã¯true
ãè¿ããŸãã
ãããã£ãŠã以äžã®ããã«ä¿®æ£ããããšãææ¡ããŸãïŒ
export async function getPostsByTag(
tagName: string,
pageSize = 10
): Promise<Post[]> {
if (!tagName) return []
const allPosts = await getAllPosts()
return allPosts
.filter(
(post) =>
!post.Tags.some((tag) => tag.name === 'footer') &&
post.Tags.some((tag) => tag.name === tagName)
)
.slice(0, pageSize)
}
ãã®ä¿®æ£ãããªãã®æ±ããåäœãæºãããŠããå Žåãããããã®ãŸãŸäœ¿çšããŠãåé¡ãããŸããã
æ¬å®¶ã®ã³ãŒãããªãã¹ã䜿ã£ãŠããããšã§ä¿å®ãç°¡åã«ãªãã®ã§ãfindã®éšåã¯ãã®ãŸãŸã«ããããšæããŸããã¢ããã€ã¹ããããšã
ããã§ãããããçšåºŠæ¢åã®ã³ãŒããç¶æããããšã¯ãå°æ¥çãªä¿å®æ§ãã³ãŒãã®äžè²«æ§ãä¿ã€äžã§éèŠã§ããfind
ã¡ãœããã確ãã«ãã®èŠä»¶ãæºãããŠããã®ã§ããã®ãŸãŸäœ¿çšããã®ãè¯ãéžæã§ãã
ç§ã®ææ¡ã¯åãªããªãã·ã§ã³ã§ãããå¿ ãããããã«åŸãå¿ èŠã¯ãããŸãããããªãã®ãããžã§ã¯ãã®èŠä»¶ãšã³ãŒãããŒã¹ã®æèã«æãåèŽããéžæãããããšãæãéèŠã§ãã
ä»ã«äœãã質åãããã°ãã€ã§ããç¥ãããã ããããæäŒãããããšãã§ããã°å¹žãã§ãã
`!post.Tags.some((tag) => tag.name === 'footer') `ã`post.Tags.some((tag) => tag.name === tagName)`ã®åã«å ¥ããã®ãšãåŸã«å ¥ããã®ãšã§ã¯ããã©ãŒãã³ã¹ã¯å€ãããŸããïŒ
ã¯ããããã©ãŒãã³ã¹ã«åœ±é¿ãåãŒãå¯èœæ§ããããŸãã
JavaScriptã®è«çæŒç®åã¯ãç絡è©äŸ¡ïŒShort-circuit evaluationïŒããè¡ããŸããã€ãŸãã&&
ïŒANDïŒæŒç®åã«ã€ããŠã¯ãæåã®æ¡ä»¶ãfalse
ã§ããã°ãJavaScriptã¯äºã€ç®ã®æ¡ä»¶ãè©äŸ¡ããã«å
šäœã®çµæãfalse
ãšããŸããããã¯||
ïŒORïŒæŒç®åã«ã€ããŠãåæ§ã§ãæåã®æ¡ä»¶ãtrue
ã§ããã°ãäºã€ç®ã®æ¡ä»¶ã¯è©äŸ¡ãããŸããã
ãããã£ãŠã!post.Tags.some((tag) => tag.name === 'footer') && post.Tags.some((tag) => tag.name === tagName)
ãšããè¡šçŸã§ã¯ãæåã®æ¡ä»¶ãfalse
ïŒã€ãŸããpost
ãfooter
ã¿ã°ãæã£ãŠããïŒã§ããã°ãäºã€ç®ã®æ¡ä»¶ã¯è©äŸ¡ãããŸããã
ãã®ãããäžè¬çã«ã¯ãèšç®ã³ã¹ããäœãããŸãã¯false
ã«ãªããããæ¡ä»¶ãå
ã«é
眮ãããšãå
šäœã®è©äŸ¡ãæ©ãçµããå¯èœæ§ãé«ãŸããããã©ãŒãã³ã¹ãåäžããŸãã
ãã ãããã®ããã©ãŒãã³ã¹ã®éãã¯ã倧æµã®å Žåãéåžžã«åŸ®å°ã§ãããç¹ã«æçš¿ãã¿ã°ã®æ°ãå€ããªãå Žåãé¡èãªéããšã¯ãªããŸãããã§ãã®ã§ãã³ãŒãã®å¯èªæ§ãä¿å®æ§ãšãã£ãèŠçŽ ãåªå ããããšãéèŠã§ãã
ããããšãããããŸãã
å¯èªæ§ãä¿å®ãæèããŠä¿®æ£ããŠã¿ãŸããã
æ£ããããæããŠãã ããã
ä¿®æ£åïŒ
export async function getNumberOfPages(): Promise<number> {
const allPosts = await getAllPosts()
return (
Math.floor(allPosts.length / NUMBER_OF_POSTS_PER_PAGE) +
(allPosts.length % NUMBER_OF_POSTS_PER_PAGE > 0 ? 1 : 0)
)
}
ä¿®æ£åŸïŒ
export async function getNumberOfPages(): Promise<number> {
const allPosts = await getAllPosts()
const outOfFooterPosts = allPosts.filter(
(post) => !post.Tags.some((tag) => tag.name === 'footer')
)
return (
Math.floor(outOfFooterPosts.length / NUMBER_OF_POSTS_PER_PAGE) +
(outOfFooterPosts.length % NUMBER_OF_POSTS_PER_PAGE > 0 ? 1 : 0)
)
}
ããªãã®ä¿®æ£ã¯å®å šã«æ£ããã§ãïŒ
ä¿®æ£åŸã®ã³ãŒãã§ã¯ããŸãgetAllPosts
é¢æ°ã䜿ã£ãŠå
šãŠã®æçš¿ãååŸãããã®åŸfilter
ã¡ãœããã䜿ã£ãŠ'footer'ã¿ã°ãæã€æçš¿ãé€å€ããŸãããã®ä¿®æ£åŸã®ãªã¹ãã®é·ããçšããŠããŒãžæ°ãèšç®ããŸãã
ãããã£ãŠããã®ä¿®æ£ã«ãã'footer'ã¿ã°ãæã€æçš¿ãããŒãžæ°ã®èšç®ããé€å€ãããŸãã
以äžãä¿®æ£åŸã®ã³ãŒãã§ãïŒ
export async function getNumberOfPages(): Promise<number> {
const allPosts = await getAllPosts()
const outOfFooterPosts = allPosts.filter(
(post) => !post.Tags.some((tag) => tag.name === 'footer')
)
return (
Math.floor(outOfFooterPosts.length / NUMBER_OF_POSTS_PER_PAGE) +
(outOfFooterPosts.length % NUMBER_OF_POSTS_PER_PAGE > 0 ? 1 : 0)
)
}
ãã®ã³ãŒããããªãã®æ±ããåäœãæºãããŠããå Žåãããããã®ãŸãŸäœ¿çšããŠãåé¡ãããŸããã
é¢æ°åãoutOfFooterPostsã«ããŠããŸãããä»åŸfooter以å€ã®çšéã§é€å€ãããTagãçºçããããšãå å³ããŠããã£ãšåãããããé¢æ°åã«ãããã§ãã
ãã¿ãŸãããç§ã誀解ãçãã§ããŸã£ãããã§ããoutOfFooterPosts
ã¯ããã§ã¯äžæå€æ°ã®ååãšããŠäœ¿ã£ãŠããŸããããããŠãã®å€æ°åãå°æ¥çã«ä»ã®ã¿ã°ããã£ã«ã¿ãŒããéã«åœ¹ç«ã€ãããªååã«ããããšããç解ã§åã£ãŠããŸããïŒ
ãããã®ç解ãæ£ãããã°ãå€æ°åãfilteredPosts
ãselectedPosts
ã®ããã«äžè¬çãªååã«å€æŽãããšãå°æ¥çã«ä»ã®ã¿ã°ã§ãã£ã«ã¿ãªã³ã°ãè¡ãå Žåã«ããã®ååãé©çšã§ããŸãã
ä¿®æ£åŸã®ã³ãŒãã¯ä»¥äžã®ããã«ãªããŸãïŒ
export async function getNumberOfPages(): Promise<number> {
const allPosts = await getAllPosts()
const filteredPosts = allPosts.filter(
(post) => !post.Tags.some((tag) => tag.name === 'footer')
)
return (
Math.floor(filteredPosts.length / NUMBER_OF_POSTS_PER_PAGE) +
(filteredPosts.length % NUMBER_OF_POSTS_PER_PAGE > 0 ? 1 : 0)
)
}
ããã§ã®filteredPosts
ã¯ãå
ã®æçš¿ãªã¹ããã'footer'ã¿ã°ãæã€æçš¿ãé€å€ããçµæãä¿æããäžæçãªå€æ°ã§ãããã®å€æ°åã¯äžè¬çãªååã§ãããä»ã®ä»»æã®ã¿ã°ã§ãã£ã«ã¿ãªã³ã°ãè¡ãå Žåã§ããã®ååãé©çšã§ããŸãã
Tagåã§é€å€ãããšããæå³ãå«ããé¢æ°åã«ãããã§ãã
ã³ãŒãã®æ瀺ã¯äžèŠã§ããé¢æ°åã®åè£ãïŒã€æããŠãã ãã
å€æ°åã«é€å€ããã¿ã°åãæå®ããæå³ãå«ããããšã¯ããã®å€æ°ãäœãè¡šããŠããã®ããæ瀺çã«ããã®ã«åœ¹ç«ã¡ãŸãã以äžã«ããã€ãã®åè£ã瀺ããŸãïŒ
excludedTagPosts
postsWithoutSpecifiedTag
filteredByTagPosts
postsExcludingTag
nonTaggedPosts
ãããã®ååã¯ãã¹ãŠãç¹å®ã®ã¿ã°ãå«ãŸãªãæçš¿ã®éåãè¡šãããšã瀺ããŠããŸãããã ãããããã®ååã¯äžè¬çãªæå³ãæã£ãŠãããã©ã®ã¿ã°ãé€å€ãããŠããã®ãã¯æ瀺ããŠããŸãããå€æ°åã«å ·äœçãªã¿ã°åãå«ããå Žåã¯ãã³ãŒãã®å¯èªæ§ãšä¿å®æ§ã®ãã©ã³ã¹ãåãå¿ èŠããããŸãã
postsWithoutSpecifiedTag
ãåããããããšæããŸãã
çŽ æŽãããéžæã§ãïŒpostsWithoutSpecifiedTag
ãšããååã¯ããã®å€æ°ãç¹å®ã®ã¿ã°ãé€å€ããæçš¿ã®éåãä¿æããŠããããšãæ瀺çã«ç€ºããŠããŸããããã¯ã³ãŒããèªã人ã«ãšã£ãŠéåžžã«ç解ããããã§ãããã
以äžã®ããã«ä¿®æ£ã§ããŸãïŒ
export async function getNumberOfPages(): Promise<number> {
const allPosts = await getAllPosts()
const postsWithoutSpecifiedTag = allPosts.filter(
(post) => !post.Tags.some((tag) => tag.name === 'footer')
)
return (
Math.floor(postsWithoutSpecifiedTag.length / NUMBER_OF_POSTS_PER_PAGE) +
(postsWithoutSpecifiedTag.length % NUMBER_OF_POSTS_PER_PAGE > 0 ? 1 : 0)
)
}
ãã®ä¿®æ£ãããªãã®æ±ããåäœãæºãããŠããå Žåãããããã®ãŸãŸäœ¿çšããŠãåé¡ãããŸãããäœãä»ã«è³ªåããããŸãããããæ°è»œã«ãç³ãä»ããã ããã
æåŸã®ä¿®æ£ãšãªããŸããïŒïŒflatMap
ãåããŠç®ã«ããã®ã§ããã®ä¿®æ£ã§ãã£ãŠãããèªä¿¡ããªãã§ãã
ä¿®æ£åïŒ
export async function getAllTags(): Promise<SelectProperty[]> {
const allPosts = await getAllPosts()
const tagNames: string[] = []
return allPosts
.flatMap((post) => post.Tags)
.reduce((acc, tag) => {
if (!tagNames.includes(tag.name)) {
acc.push(tag)
tagNames.push(tag.name)
}
return acc
}, [] as SelectProperty[])
.sort((a: SelectProperty, b: SelectProperty) =>
a.name.localeCompare(b.name)
)
}
ä¿®æ£åŸïŒ
export async function getAllTags(): Promise<SelectProperty[]> {
const allPosts = await getAllPosts()
const tagNames: string[] = []
return allPosts
.filter(
(post) => !!post.Rank && !post.Tags.some((tag) => tag.name === 'footer')
)
.flatMap((post) => post.Tags)
.reduce((acc, tag) => {
if (!tagNames.includes(tag.name)) {
acc.push(tag)
tagNames.push(tag.name)
}
return acc
}, [] as SelectProperty[])
.sort((a: SelectProperty, b: SelectProperty) =>
a.name.localeCompare(b.name)
)
}
ããªããæäŸããä¿®æ£ã¯ãå šäœãšããŠã¯æ£ç¢ºã§ãããäžéšæ³šæãã¹ãç¹ããããŸãã
ããªãã®ä¿®æ£ã³ãŒãã§ã¯ããŸããã¹ãŠã®æçš¿ãååŸããããããç¹å®ã®æçš¿ããã£ã«ã¿ãªã³ã°ããŠããŸãããã®ãã£ã«ã¿ãªã³ã°ã®æ¡ä»¶ã¯ããæçš¿ãã©ã³ã¯ãæã£ãŠããïŒ!!post.RankïŒããšãæçš¿ã'footer'ã¿ã°ãæã£ãŠããªãïŒ!post.Tags.some((tag) => tag.name === 'footer')ïŒãã®äž¡æ¹ãæºãããã®ã§ãã
ãã ããããã§æ³šæãã¹ããªã®ã¯ã!!post.Rank
ãšããéšåã§ããããã¯ãã®æçš¿ãã©ã³ã¯ãæã£ãŠãããã©ãããå€å®ããŸãããå
ã
ã®é¢æ°ã®ç®çã¯ããã¹ãŠã®ã¿ã°ãååŸãããããšã§ããã€ãŸããã©ã³ã¯ãæã£ãŠãããã©ããã¯ãã®é¢æ°ã®ç®çãšã¯é¢ä¿ããããŸããããããã£ãŠããããæå³ãããã®ã§ãªããã°ã!!post.Rank
ãšããæ¡ä»¶ã¯åé€ããã¹ãã§ãã
ä¿®æ£åŸã®ã³ãŒãã¯ä»¥äžã®ããã«ãªããŸãïŒ
export async function getAllTags(): Promise<SelectProperty[]> {
const allPosts = await getAllPosts()
const tagNames: string[] = []
return allPosts
.filter((post) => !post.Tags.some((tag) => tag.name === 'footer'))
.flatMap((post) => post.Tags)
.reduce((acc, tag) => {
if (!tagNames.includes(tag.name)) {
acc.push(tag)
tagNames.push(tag.name)
}
return acc
}, [] as SelectProperty[])
.sort((a: SelectProperty, b: SelectProperty) =>
a.name.localeCompare(b.name)
)
}
ãã®ä¿®æ£ã«ããã'footer'ã¿ã°ãæã€æçš¿ãå šãŠã®ã¿ã°ãååŸããåŠçããé€å€ãããŸãããããããããªãã®æ±ããåäœã§ããã°ããã®ä¿®æ£ããã®ãŸãŸäœ¿ã£ãŠãåé¡ãããŸããã
ãã¿ãŸãããåèã«ããã³ãŒãããã®ãŸãŸã³ããããŠããŸããäœèšãªéšåãã€ããŠãŸãããã以åŸæ°ãã€ããŸã
å šãåé¡ãããŸããïŒä¿®æ£ãã¹ãéšåãèŠã€ãåºãã®ã¯ãã³ãŒããåŠã¶äžã§éåžžã«éèŠãªã¹ãã«ã§ãããããŠãããã¯ããªããæåè£ã«è¡ã£ãããšã§ããã³ãŒãã«ã€ããŠç解ãæ·±ãããããä¿®æ£ãããããä»äººãšå ±æããããšã¯ãéåžžã«éèŠãªã¹ãããã§ããäœãä»ã®è³ªåããããŸãããããæ°è»œã«ãç³ãä»ããã ããã
ããããšã(^o^)ïŒïŒŒ(^o^)ïŒ
èšäºããŒãžã®ã³ãŒã
export async function getStaticPaths() {
const posts = await getAllPosts()
return posts.map((post: interfaces.Post) => ({ params: { slug: post.Slug } }))
}
const { slug } = Astro.params
const post = await getPostBySlug(slug)
if (!post) {
throw new Error('Post not found. slug: ${slug}')
}
const [blocks, allPosts, rankedPosts, recentPosts, tags, postsHavingSameTag] =
await Promise.all([
getAllBlocksByBlockId(post.PageId),
getAllPosts(),
getRankedPosts(),
getPosts(5),
getAllTags(),
getPostsByTag(post.Tags[0]?.name, 6),
])
const fileAtacchedBlocks = extractTargetBlocks('image', blocks)
.concat(extractTargetBlocks('file', blocks))
.filter((block) => {
if (!block) {
return false
}
const imageOrFile = block.Image || block.File
return imageOrFile && imageOrFile.File && imageOrFile.File.Url
})
// Download files
await Promise.all(
fileAtacchedBlocks
.map(async (block) => {
const expiryTime = (block.Image || block.File).File.ExpiryTime
if (Date.parse(expiryTime) > Date.now()) {
return Promise.resolve(block)
}
return getBlock(block.Id)
})
.map((promise) =>
promise.then((block) => {
let url!: URL
try {
url = new URL((block.Image || block.File).File.Url)
} catch (err) {
console.log('Invalid file URL')
return Promise.reject()
}
return Promise.resolve(url)
})
)
.map((promise) => promise.then(downloadFile))
)
const currentPostIndex = allPosts.findIndex((post) => post.Slug === slug)
const prevPost = allPosts[currentPostIndex + 1]
const nextPost = allPosts[currentPostIndex - 1]
let ogImage = ''
if (post.FeaturedImage && post.FeaturedImage.Url) {
ogImage = new URL(filePath(new URL(post.FeaturedImage.Url)), Astro.site)
}
ããããã®åå ããçŽæ¥getAllPosts
ããããŒã¿ãååŸããŠããããšã«ãããã®ð
ããŠãã©ãã«filterããããããâŠ.
Promptãé·ãã®ã§ãã°ã«ã«åçŽããŠãå±ãããŸãww
ä¿å®æ§ãæèããŠå®çŸ©ããé¢æ°ã®éšåã§filterãå ããŸãããããã©ãŒãã³ã¹ããããããããå å³ããŠè©äŸ¡ããã ãããšå¬ããã§ã
ä¿®æ£åïŒ
```export async function getStaticPaths() { const posts = await getAllPosts() return posts.map((post: interfaces.Post) => ({ params: { slug: post.Slug } }))}const { slug } = Astro.paramsconst post = await getPostBySlug(slug)if (!post) { throw new Error('Post not found. slug: ${slug}')}const [blocks, allPosts, rankedPosts, recentPosts, tags, postsHavingSameTag] = await Promise.all([ getAllBlocksByBlockId(post.PageId), getAllPosts(), getRankedPosts(), getPosts(5), getAllTags(), getPostsByTag(post.Tags[0]?.name, 6), ])const fileAtacchedBlocks = extractTargetBlocks('image', blocks) .concat(extractTargetBlocks('file', blocks)) .filter((block) => { if (!block) { return false } const imageOrFile = block.Image || block.File return imageOrFile && imageOrFile.File && imageOrFile.File.Url })// Download filesawait Promise.all( fileAtacchedBlocks .map(async (block) => { const expiryTime = (block.Image || block.File).File.ExpiryTime if (Date.parse(expiryTime) > Date.now()) { return Promise.resolve(block) } return getBlock(block.Id) }) .map((promise) => promise.then((block) => { let url!: URL try { url = new URL((block.Image || block.File).File.Url) } catch (err) { console.log('Invalid file URL') return Promise.reject() } return Promise.resolve(url) }) ) .map((promise) => promise.then(downloadFile)))const currentPostIndex = allPosts.findIndex((post) => post.Slug === slug)const prevPost = allPosts[currentPostIndex + 1]const nextPost = allPosts[currentPostIndex - 1]let ogImage = ''if (post.FeaturedImage && post.FeaturedImage.Url) { ogImage = new URL(filePath(new URL(post.FeaturedImage.Url)), Astro.site)}```
ä¿®æ£åŸïŒ
```export async function getStaticPaths() { const posts = await getAllPosts() return posts.map((post: interfaces.Post) => ({ params: { slug: post.Slug } }))}const { slug } = Astro.paramsconst post = await getPostBySlug(slug)if (!post) { throw new Error('Post not found. slug: ${slug}')}const [blocks, allPosts, rankedPosts, recentPosts, tags, postsHavingSameTag] = await Promise.all([ getAllBlocksByBlockId(post.PageId), getAllPosts().filter( (post) => !post.Tags.some((tag) => tag.name === 'footer') ), getRankedPosts(), getPosts(5), getAllTags(), getPostsByTag(post.Tags[0]?.name, 6), ])const fileAtacchedBlocks = extractTargetBlocks('image', blocks) .concat(extractTargetBlocks('file', blocks)) .filter((block) => { if (!block) { return false } const imageOrFile = block.Image || block.File return imageOrFile && imageOrFile.File && imageOrFile.File.Url })// Download filesawait Promise.all( fileAtacchedBlocks .map(async (block) => { const expiryTime = (block.Image || block.File).File.ExpiryTime
if (Date.parse(expiryTime) > Date.now()) { return Promise.resolve(block) } return getBlock(block.Id) }) .map((promise) => promise.then((block) => { let url!: URL try { url = new URL((block.Image || block.File).File.Url) } catch (err) { console.log('Invalid file URL') return Promise.reject() } return Promise.resolve(url) }) ) .map((promise) => promise.then(downloadFile)))const currentPostIndex = allPosts.findIndex((post) => post.Slug === slug)const prevPost = allPosts[currentPostIndex + 1]const nextPost = allPosts[currentPostIndex - 1]let ogImage = ''if (post.FeaturedImage && post.FeaturedImage.Url) { ogImage = new URL(filePath(new URL(post.FeaturedImage.Url)), Astro.site)}```
ãææããã ããã³ãŒããèŠããšãããä¿®æ£ç¹ã«ã€ããŠã¯ããããæ£ããåæ ãããŠããããã«æããŸãããã ããä¿®æ£åŸã®ã³ãŒãã§æ³šæãã¹ãç¹ãäžã€ãããŸãã
ä¿®æ£åŸã®ã³ãŒãã§ãgetAllPosts()
é¢æ°ãPromiseãè¿ãéåæé¢æ°ã§ããããšãèæ
®ããŠãfilter()
ã䜿çšããåã«éåæåŠçãçµããã®ãåŸ
ã€å¿
èŠããããŸãããã®ãããä¿®æ£åŸã®ã³ãŒãã®è©²åœç®æã¯ä»¥äžã®ããã«ãªãã¹ãã§ãïŒ
const allPostsFiltered = (await getAllPosts()).filter((post) => !post.Tags.some((tag) => tag.name === 'footer'));
ãããã¯ä»¥äžã®ããã«éåæåŠçãåŸ ã€éšåãæ瀺çã«åããŠæžãããšãã§ããŸãïŒ
const allPosts = await getAllPosts();
const allPostsFiltered = allPosts.filter((post) => !post.Tags.some((tag) => tag.name === 'footer'));
ãŸãããã®ä¿®æ£ã«ããallPostsFiltered
ãåç
§ããä»ã®éšåïŒäŸãã°const currentPostIndex = allPosts.findIndex((post) => post.Slug === slug)
, const prevPost = allPosts[currentPostIndex + 1]
, const nextPost = allPosts[currentPostIndex - 1]
ãªã©ïŒãallPosts
ã§ã¯ãªãallPostsFiltered
ã䜿çšããããã«ä¿®æ£ããå¿
èŠããããŸãã
æ¬å®¶ã®é¢æ°åã¯ãªãã¹ãæ®ãããã®ã§allPostsã䜿ã£ãŠä¿®æ£ãããã§ã
ãã®å Žåã以äžã®ããã«allPosts
ãåå®çŸ©ããŠãããã䜿çšãããšããæ¹æ³ããããŸãïŒ
let allPosts = await getAllPosts()
allPosts = allPosts.filter((post) => !post.Tags.some((tag) => tag.name === 'footer'))
ãã®ããã«ããã°ãé¢æ°å
ã®ãã®åŸã®allPosts
ã®åç
§ãã¹ãŠããã£ã«ã¿ãªã³ã°ãããçµæãæãããã«ãªããŸãããã ããå€æ°allPosts
ãå代å
¥å¯èœã§ããïŒletã§å®çŸ©ãããŠããïŒããšã確èªããŠãã ãããconstã§å®çŸ©ãããŠããå Žåãå代å
¥ã¯äžå¯èœã§ãã
ãã®ããã«ä¿®æ£ããŠã¿ãŸããã
const [
blocks,
allPostsNoFinter,
rankedPosts,
recentPosts,
tags,
postsHavingSameTag,
] = await Promise.all([
getAllBlocksByBlockId(post.PageId),
getAllPosts(),
getRankedPosts(),
getPosts(5),
getAllTags(),
getPostsByTag(post.Tags[0]?.name, 6),
])
const allPosts = allPostsNoFinter.filter(
(post) => !post.Tags.some((tag) => tag.name === 'footer')
)
ãã®ä¿®æ£ã¯é©åã§ããallPostsNoFilter
ãšããæ°ããå€æ°åãå°å
¥ããããšã§ãã³ãŒãã®æå³ãããæ確ã«ãªããŸãããããã«ãããä»ã®éçºè
ïŒãããã¯å°æ¥ã®ããªãèªèº«ïŒãã³ãŒããèªããšãã«ããã®å€æ°ããã£ã«ã¿ãªã³ã°ãããŠããªãå
šãŠã®ãã¹ããå«ãã§ãããšããããšãç解ãããããªããŸãã
ãŸããallPosts
ã®åå®çŸ©ãé©åã§ããã®å€æ°ã以åãšåãç®çïŒããªãã¡ãå
šãŠã®ãã¹ããå«ããä»åã¯'footer'ã¿ã°ã®ãã¹ããé€ããŠããïŒã«çšããããããšãåãããŸãã
ãã®ä¿®æ£ã«ãããã³ãŒãã®å¯èªæ§ãšä¿å®æ§ãåäžãããšèšããŸãã
å®æã«é¢æ°åãå€ãããšã
æ¬å®¶ããã®ã¢ããããŒãäœæ¥ã§èŠåŽããã®ãç®ã«èŠããŠããã®ã§
ãªãã¹ãå åŽãå€æŽããããšã«ããŸããð
ä»åã®ã³ãŒãã¯ãã¡ãïŒïŒ