-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 💡 move integration to a separate file
- Loading branch information
Showing
2 changed files
with
106 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { | ||
listFilesRecursively, | ||
filterMdFiles, | ||
generateUrlPath, | ||
} from '../scripts/utils'; | ||
import fs from 'fs'; | ||
import { remark } from 'remark'; | ||
import remarkFrontmatter from 'remark-frontmatter'; | ||
import remarkParseFrontmatter from 'remark-parse-frontmatter'; | ||
|
||
const outputDir = './dist/custom-data'; | ||
const outputFilePath = `${outputDir}/latestBlogPosts.json`; | ||
|
||
interface BlogPost { | ||
title: string; | ||
date: string; | ||
url: string; | ||
} | ||
|
||
interface Frontmatter { | ||
title: string; | ||
date: string; | ||
} | ||
|
||
interface ParsedMarkdown { | ||
data?: { | ||
frontmatter: Frontmatter; | ||
}; | ||
} | ||
|
||
const sortByDateDesc = (posts: BlogPost[]): BlogPost[] => { | ||
return posts.sort((a, b) => { | ||
const timestampA = new Date(a.date).getTime(); | ||
const timestampB = new Date(b.date).getTime(); | ||
return timestampB > timestampA | ||
? 1 | ||
: timestampB < timestampA | ||
? -1 | ||
: 0; | ||
}); | ||
}; | ||
|
||
export const getLatestBlogPosts = ({ | ||
targetDir, | ||
}: { | ||
targetDir: string; | ||
}) => ({ | ||
name: 'latestBlogPosts', | ||
hooks: { | ||
'astro:build:done': async () => { | ||
let files = listFilesRecursively({ | ||
directory: targetDir, | ||
}); | ||
|
||
files = filterMdFiles(files); | ||
|
||
const data: BlogPost[] = []; | ||
|
||
for (const filePath of files) { | ||
const fileContent = fs.readFileSync(filePath, 'utf-8'); | ||
|
||
const parsed = await remark() | ||
.use(remarkFrontmatter) | ||
.use(remarkParseFrontmatter) | ||
.process(fileContent) as unknown as ParsedMarkdown; | ||
|
||
if (!parsed.data?.frontmatter?.date) continue; | ||
|
||
data.push({ | ||
title: parsed.data.frontmatter.title, | ||
date: parsed.data.frontmatter.date, | ||
url: generateUrlPath({ | ||
filePath, | ||
}), | ||
}); | ||
} | ||
|
||
const sortedLatestBlogPosts = sortByDateDesc(data) | ||
.map( | ||
({ | ||
title, | ||
date, | ||
url | ||
}) => ({ | ||
title, | ||
date, | ||
path: url, | ||
}) | ||
); | ||
|
||
const jsonStr = JSON.stringify({ | ||
data: sortedLatestBlogPosts.slice(0, 10), | ||
}, null, 2); | ||
|
||
try { | ||
await fs.promises.mkdir(outputDir, { recursive: true }); | ||
await fs.promises.writeFile(outputFilePath, jsonStr); | ||
|
||
console.log(`✅ Generated latest blog posts JSON in ${outputFilePath}`); | ||
} catch (err) { | ||
console.error(`👹 Oops! Failed to generate ${outputFilePath}`); | ||
} | ||
}, | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1 @@ | ||
import { | ||
listFilesRecursively, | ||
filterMdFiles, | ||
generateUrlPath, | ||
} from '../scripts/utils'; | ||
import fs from 'fs'; | ||
import { remark } from 'remark'; | ||
import remarkFrontmatter from 'remark-frontmatter'; | ||
import remarkParseFrontmatter from 'remark-parse-frontmatter'; | ||
|
||
const outputDir = './dist/custom-data'; | ||
const outputFilePath = `${outputDir}/latestBlogPosts.json`; | ||
|
||
interface BlogPost { | ||
title: string; | ||
date: string; | ||
url: string; | ||
} | ||
|
||
interface Frontmatter { | ||
title: string; | ||
date: string; | ||
} | ||
|
||
interface ParsedMarkdown { | ||
data?: { | ||
frontmatter: Frontmatter; | ||
}; | ||
} | ||
|
||
const sortByDateDesc = (posts: BlogPost[]): BlogPost[] => { | ||
return posts.sort((a, b) => { | ||
const timestampA = new Date(a.date).getTime(); | ||
const timestampB = new Date(b.date).getTime(); | ||
return timestampB > timestampA | ||
? 1 | ||
: timestampB < timestampA | ||
? -1 | ||
: 0; | ||
}); | ||
}; | ||
|
||
export const getLatestBlogPosts = ({ | ||
targetDir, | ||
}: { | ||
targetDir: string; | ||
}) => ({ | ||
name: 'latestBlogPosts', | ||
hooks: { | ||
'astro:build:done': async () => { | ||
let files = listFilesRecursively({ | ||
directory: targetDir, | ||
}); | ||
|
||
files = filterMdFiles(files); | ||
|
||
const data: BlogPost[] = []; | ||
|
||
for (const filePath of files) { | ||
const fileContent = fs.readFileSync(filePath, 'utf-8'); | ||
|
||
const parsed = await remark() | ||
.use(remarkFrontmatter) | ||
.use(remarkParseFrontmatter) | ||
.process(fileContent) as unknown as ParsedMarkdown; | ||
|
||
if (!parsed.data?.frontmatter?.date) continue; | ||
|
||
data.push({ | ||
title: parsed.data.frontmatter.title, | ||
date: parsed.data.frontmatter.date, | ||
url: generateUrlPath({ | ||
filePath, | ||
}), | ||
}); | ||
} | ||
|
||
const sortedLatestBlogPosts = sortByDateDesc(data) | ||
.map( | ||
({ | ||
title, | ||
date, | ||
url | ||
}) => ({ | ||
title, | ||
date, | ||
path: url, | ||
}) | ||
); | ||
|
||
const jsonStr = JSON.stringify({ | ||
data: sortedLatestBlogPosts.slice(0, 10), | ||
}, null, 2); | ||
|
||
try { | ||
await fs.promises.mkdir(outputDir, { recursive: true }); | ||
await fs.promises.writeFile(outputFilePath, jsonStr); | ||
|
||
console.log(`✅ Generated latest blog posts JSON in ${outputFilePath}`); | ||
} catch (err) { | ||
console.error(`👹 Oops! Failed to generate ${outputFilePath}`); | ||
} | ||
}, | ||
} | ||
}); | ||
export { getLatestBlogPosts } from './getLatestBlogPosts'; |