-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from maicss/auto-page-data
feat: add blog auto frontmatter
- Loading branch information
Showing
13 changed files
with
362 additions
and
111 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,23 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
message="$(cat $1)" | ||
requiredPattern="^(feat|chore|WIP|add|cut|fix|bump|make|start|stop|refactor|reformat|optimise|document|merge): .*$" | ||
if ! [[ $message =~ $requiredPattern ]]; | ||
then | ||
echo "-" | ||
echo "-" | ||
echo "-" | ||
echo "🚨 Wrong commit message! 😕" | ||
echo "The commit message must have this format:" | ||
echo "<verb in imperative mood> <what was done>" | ||
echo "Allowed verbs in imperative mood: add, cut, fix, bump, make, start, stop, refactor, reformat, optimise, document, merge" | ||
echo "Example: add login button" | ||
echo "-" | ||
echo "Your commit message was:" | ||
echo $message | ||
echo "-" | ||
echo "For more information, check script in .husky/commit-msg" | ||
echo "-" | ||
exit 1 | ||
fi |
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,4 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
npm run lint |
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,92 @@ | ||
#!/usr/bin/env node | ||
import { readFile, writeFile, readdir, stat } from 'node:fs/promises' | ||
import { resolve, dirname } from 'node:path' | ||
import { fileURLToPath } from 'url' | ||
import { exit } from 'node:process' | ||
import matter from 'gray-matter' | ||
|
||
/** | ||
* 扫描blog文件夹下所有的blog文件,查看有没有vitepress的Frontmatter,保证有: | ||
* - date 没有的话赋值为时间 | ||
* - image index 文章列表展示用 | ||
* - prev 和 next,所有的文章按照时间排序,最早的prev是index,最晚的next是index,其他互相指 | ||
* - description 文章标题 | ||
*/ | ||
|
||
const PAGE_CONFIG_SYMBOL = '---' | ||
let breakBuildInfo = '' | ||
const DIRNAME = dirname(fileURLToPath(import.meta.url)) | ||
const folder = resolve(DIRNAME, '../pages/blog/') | ||
|
||
const buildFrontMatter = (data) => { | ||
if (!data.image) breakBuildInfo += `${data.file} doesn't have image` | ||
const lines = [PAGE_CONFIG_SYMBOL] | ||
lines.push(`date: ${data.date}`) | ||
lines.push(`description: ${data.description}`) | ||
lines.push(`image: ${data.image}`) | ||
lines.push(`prev:`) | ||
lines.push(` text: ${data.prev.text}`) | ||
lines.push(` link: ${data.prev.link}`) | ||
lines.push(`next:`) | ||
lines.push(` text: ${data.next.text}`) | ||
lines.push(` link: ${data.next.link}`) | ||
lines.push(PAGE_CONFIG_SYMBOL) | ||
return lines.join('\r\n') | ||
} | ||
|
||
readdir(folder) | ||
.then((files) => { | ||
const needHandleFiles = files.filter((file) => file.endsWith('.md') && file !== 'index.md') | ||
return Promise.all( | ||
needHandleFiles.map((file) => { | ||
const filePath = resolve(folder, file) | ||
return Promise.all([ | ||
readFile(filePath), | ||
stat(filePath).then((statValue) => ({ | ||
date: statValue.birthtime.toLocaleDateString('zh-CN'), | ||
ts: statValue.birthtimeMs, | ||
})), | ||
]).then(([fileContent, attr]) => { | ||
const item = { file } | ||
const { data, content } = matter(fileContent) | ||
if (data.date) { | ||
item.date = typeof data.date === 'string' ? data.date : data.date.toLocaleDateString('zh-CN') | ||
item.ts = new Date(data.date).getTime() | ||
} else { | ||
item.date = attr.date | ||
item.ts = attr.ts | ||
} | ||
item.image = data.image || '' | ||
item.content = content | ||
const firstLine = content.trim().split('\r\n')[0].trim() | ||
item.description = firstLine.startsWith('#') ? firstLine.slice(1).trim() : '' | ||
return item | ||
}) | ||
}) | ||
) | ||
}) | ||
.then((items) => { | ||
items.sort((a, b) => a.ts - b.ts) | ||
items.forEach((item, index) => { | ||
if (index === 0) { | ||
item.prev = { text: 'Blog 首页', link: '/blog/' } | ||
item.next = { text: items[index + 1].description, link: '/blog/' + items[index + 1].file.slice(0, '.md'.length * -1) } | ||
} else if (index === items.length - 1) { | ||
item.prev = { text: items[index - 1].description, link: '/blog/' + items[index - 1].file.slice(0, '.md'.length * -1) } | ||
item.next = { text: 'Blog 首页', link: '/blog/' } | ||
} else { | ||
item.prev = { text: items[index - 1].description, link: '/blog/' + items[index - 1].file.slice(0, '.md'.length * -1) } | ||
item.next = { text: items[index + 1].description, link: '/blog/' + items[index + 1].file.slice(0, '.md'.length * -1) } | ||
} | ||
}) | ||
|
||
return Promise.all( | ||
items.map((item) => writeFile(resolve(folder, item.file), buildFrontMatter(item) + '\r\n' + item.content)) | ||
).then(() => { | ||
if (breakBuildInfo.length) { | ||
console.error(`WARNING: [${breakBuildInfo}] build failed, exiting.`) | ||
exit(1) | ||
} | ||
}) | ||
}) | ||
.catch(console.error) |
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
Oops, something went wrong.