Skip to content

Commit

Permalink
Merge pull request #11 from maicss/auto-page-data
Browse files Browse the repository at this point in the history
feat: add blog auto frontmatter
  • Loading branch information
maicss authored Nov 19, 2023
2 parents d468547 + 468e99f commit a668ca8
Show file tree
Hide file tree
Showing 13 changed files with 362 additions and 111 deletions.
23 changes: 23 additions & 0 deletions .husky/commit-msg
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
4 changes: 4 additions & 0 deletions .husky/pre-commit
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
92 changes: 92 additions & 0 deletions .vitepress/beforeBuild.mjs
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)
7 changes: 0 additions & 7 deletions .vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import svgLoader from 'vite-svg-loader'
export default defineConfig({
title: "Maicss",
description: "Miacss' website pyqt 中文 教程 pyqt5 pyqt6",
// base: '/',
head: [
["link", { rel: "icon", href: "/favicon.ico" }],
["link", { rel: "manifest", href: "/site.webmanifest" }],
// [ 'link', { href: 'https://fonts.googleapis.com/css2?family=Roboto&display=swap', rel: 'stylesheet' } ],
[
"script",
{
Expand Down Expand Up @@ -42,7 +40,6 @@ export default defineConfig({
lang: "zh-CN",
cleanUrls: true,
themeConfig: {
// https://vitepress.dev/reference/default-theme-config、
outline: {
label: "TOC",
},
Expand Down Expand Up @@ -128,10 +125,6 @@ export default defineConfig({
sitemap: {
hostname: "https://maicss.com",
},
transformHead (context) {
// todo
// console.log('transformHead', context)
},
vite: {
plugins: [svgLoader({
defaultImport: 'component'
Expand Down
Loading

0 comments on commit a668ca8

Please sign in to comment.