Skip to content

Commit

Permalink
Merge pull request #247 from ATQQ/feature/pagefind-filter-and-sort
Browse files Browse the repository at this point in the history
feat: add shared lib
  • Loading branch information
ATQQ authored Jun 16, 2024
2 parents fe63db1 + 67f9954 commit d32f473
Show file tree
Hide file tree
Showing 39 changed files with 736 additions and 765 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
"build": "pnpm --filter blogpress build",
"buildTheme": "pnpm --filter @sugarat/theme build",
"buildlib": "pnpm run /^build:.*/",
"build:pagefind": "pnpm --filter vitepress-plugin-pagefind build",
"build:rss": "pnpm --filter vitepress-plugin-rss build",
"build:shared": "pnpm --filter @sugarat/theme-shared build",
"build:pagefind": "wait-on packages/shared/dist && pnpm --filter vitepress-plugin-pagefind build",
"build:rss": "wait-on packages/shared/dist && pnpm --filter vitepress-plugin-rss build",
"build:create": "pnpm --filter @sugarat/create-theme build",
"build:theme-only": "wait-on packages/vitepress-plugin-rss/dist packages/vitepress-plugin-pagefind/dist && pnpm --filter @sugarat/theme build:node",
"serve": "pnpm --filter blogpress serve",
Expand Down
15 changes: 6 additions & 9 deletions packages/blogpress/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import process from 'node:process'
import { getThemeConfig } from '@sugarat/theme/node'
import type { Theme } from '@sugarat/theme'
import type { RSSOptions } from 'vitepress-plugin-rss'
import { RssPlugin } from 'vitepress-plugin-rss'
import { defineConfig } from 'vitepress'

const baseUrl = 'https://sugarat.top'
const weekly = `${baseUrl}/weekly`
const RSSWeekly: RSSOptions = {
const RSSWeekly: Theme.RSSOptions = {
title: '视野修炼 - 技术周刊',
baseUrl,
description: '每周会精选出一些 优质&有趣 的内容做推送(大前端为主),包含但不限于 优质文章,开源库,工具网站,有意思的知识',
Expand Down Expand Up @@ -58,7 +56,7 @@ const blogTheme = getThemeConfig({
]
},
themeColor: 'el-blue',
RSS,
RSS: [RSS, RSSWeekly],
author: '粥里有勺糖',
comment: {
repo: 'ATQQ/sugar-blog',
Expand Down Expand Up @@ -194,7 +192,9 @@ const blogTheme = getThemeConfig({
random: true,
limit: 6,
},
// search: false,
search: {
showDate: true,
},
recommend: {
showSelf: true,
nextText: '下一页',
Expand Down Expand Up @@ -288,10 +288,7 @@ export default defineConfig({
server: {
port: 4000,
host: '0.0.0.0'
},
plugins: [
RssPlugin(RSSWeekly)
]
}
},
vue: {
template: {
Expand Down
5 changes: 5 additions & 0 deletions packages/blogpress/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ publish: false

# CHANGELOG

## 2.0.52
### Patch Changes
- @sugarat/theme@0.4.4

## 2.0.51

### Patch Changes

- Updated dependencies
Expand Down
3 changes: 1 addition & 2 deletions packages/blogpress/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "blogpress",
"type": "module",
"version": "2.0.51",
"version": "2.0.52",
"private": true,
"license": "MIT",
"scripts": {
Expand All @@ -14,7 +14,6 @@
"@sugarat/theme": "workspace:*",
"element-plus": "^2.7.2",
"vitepress": "1.2.3",
"vitepress-plugin-rss": "workspace:*",
"vue": "^3.4.26"
},
"devDependencies": {
Expand Down
6 changes: 6 additions & 0 deletions packages/create-theme/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @sugarat/create-theme

## 0.0.67

### Patch Changes

- chore: update deps

## 0.0.66

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/create-theme/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sugarat/create-theme",
"version": "0.0.66",
"version": "0.0.67",
"description": "简约风的 Vitepress 博客主题,sugarat vitepress blog theme",
"author": "粥里有勺糖",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-theme/public/template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"@element-plus/icons-vue": "^2.3.1",
"@sugarat/theme": "^0.4.3",
"@sugarat/theme": "^0.4.4",
"element-plus": "^2.7.2",
"vue": "3.4.26"
},
Expand Down
7 changes: 7 additions & 0 deletions packages/shared/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @sugarat/theme-shared

## 0.0.1

### Patch Changes

- feat: init shared package
3 changes: 3 additions & 0 deletions packages/shared/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @sugarat/theme-shared

Internal utility functions and constants shared across VitePress sources: [@sugarat/theme](https://www.npmjs.com/package/@sugarat/theme), [vitepress-plugin-rss](https://www.npmjs.com/package/vitepress-plugin-rss), [vitepress-plugin-pagefind](https://www.npmjs.com/package/vitepress-plugin-pagefind).
44 changes: 44 additions & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "@sugarat/theme-shared",
"version": "0.0.1",
"description": "@sugarat/theme-shared",
"author": "sugar",
"license": "MIT",
"homepage": "https://github.com/ATQQ/sugar-blog/tree/master/packages/shared",
"repository": {
"type": "git",
"url": "git+https://github.com/ATQQ/sugar-blog.git"
},
"bugs": {
"url": "https://github.com/ATQQ/sugar-blog/issues"
},
"keywords": [
"shared",
"utils",
"vitepress"
],
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js"
}
},
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"dev": "npx tsup src/index.ts --dts --watch --format esm,cjs",
"build": "npx tsup src/index.ts --dts --format esm,cjs --silent"
},
"dependencies": {
"cross-spawn": "^7.0.3",
"gray-matter": "^4.0.3"
},
"devDependencies": {
"@types/cross-spawn": "^6.0.6",
"p-limit": "4"
}
}
54 changes: 54 additions & 0 deletions packages/shared/src/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export function formatDate(d: any, fmt = 'yyyy-MM-dd hh:mm:ss') {
if (!(d instanceof Date)) {
d = new Date(d)
}
const o: any = {
'M+': d.getMonth() + 1, // 月份
'd+': d.getDate(), // 日
'h+': d.getHours(), // 小时
'm+': d.getMinutes(), // 分
's+': d.getSeconds(), // 秒
'q+': Math.floor((d.getMonth() + 3) / 3), // 季度
'S': d.getMilliseconds() // 毫秒
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(
RegExp.$1,
`${d.getFullYear()}`.substr(4 - RegExp.$1.length)
)
}
// eslint-disable-next-line no-restricted-syntax
for (const k in o) {
if (new RegExp(`(${k})`).test(fmt))
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length === 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length)
)
}
return fmt
}

export function formatShowDate(date: Date | string) {
const source = +new Date(date)
const now = +new Date()
const diff = now - source
const oneSeconds = 1000
const oneMinute = oneSeconds * 60
const oneHour = oneMinute * 60
const oneDay = oneHour * 24
const oneWeek = oneDay * 7
if (diff < oneMinute) {
return `${Math.floor(diff / oneSeconds)}秒前`
}
if (diff < oneHour) {
return `${Math.floor(diff / oneMinute)}分钟前`
}
if (diff < oneDay) {
return `${Math.floor(diff / oneHour)}小时前`
}
if (diff < oneWeek) {
return `${Math.floor(diff / oneDay)}天前`
}

return formatDate(new Date(date), 'yyyy-MM-dd')
}
127 changes: 127 additions & 0 deletions packages/shared/src/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import process from 'node:process'
import { spawn } from 'cross-spawn'
import matter from 'gray-matter'
import pLimit from 'p-limit'

const timeLimit = pLimit(+(process.env.P_LIMT_MAX || os.cpus().length))

/**
* 获取 markdown 内容中的标题
*/
export function getDefaultTitle(content: string) {
const match = content.match(/^(#+)\s+(.+)/m)
return match?.[2] || ''
}

const cache = new Map<string, Date | undefined>()
/**
* 获取文件最后修改时间
* 优先使用 git 命令获取,如果失败则使用 fs.stat 获取
*/
export async function getFileLastModifyTime(url: string) {
const cached = cache.get(url)
if (cached) {
return cached
}
let date = await timeLimit(() => getFileLastModifyTimeByGit(url))
if (!date) {
date = await getFileLastModifyTimeByFs(url)
}
if (date) {
cache.set(url, date)
}
return date
}

export function getFileLastModifyTimeByGit(url: string): Promise<Date | undefined> {
return new Promise((resolve) => {
// 使用异步回调
const child = spawn('git', ['log', '-1', '--pretty="%ai"', url])
let output = ''
child.stdout.on('data', d => (output += String(d)))
child.on('close', async () => {
let date: Date | undefined
if (output.trim()) {
date = new Date(output)
}
resolve(date)
})
child.on('error', async () => {
resolve(undefined)
})
})
}

export async function getFileBirthTimeByFs(url: string) {
try {
const fsStat = await fs.promises.stat(url)
return fsStat.birthtime
}
catch {
return undefined
}
}

export async function getFileLastModifyTimeByFs(url: string) {
try {
const fsStat = await fs.promises.stat(url)
return fsStat.mtime
}
catch {
return undefined
}
}

export const EXTERNAL_URL_RE = /^[a-z]+:/i

/**
* Join two paths by resolving the slash collision.
*/
export function joinPath(base: string, path: string): string {
return `${base}${path}`.replace(/\/+/g, '/')
}

export function withBase(base: string, path: string) {
return EXTERNAL_URL_RE.test(path) || path.startsWith('.')
? path
: joinPath(base, path)
}

export const grayMatter = matter

export function getTextSummary(text: string, count = 100) {
return (
text
// 首个标题
?.replace(/^#+\s+.*/, '')
// 除去标题
?.replace(/#/g, '')
// 除去图片
?.replace(/!\[.*?\]\(.*?\)/g, '')
// 除去链接
?.replace(/\[(.*?)\]\(.*?\)/g, '$1')
// 除去加粗
?.replace(/\*\*(.*?)\*\*/g, '$1')
?.split('\n')
?.filter(v => !!v)
?.join('\n')
?.replace(/>(.*)/, '')
?.replace(/</g, '&lt;').replace(/>/g, '&gt;')
?.trim()
?.slice(0, count)
)
}

const windowsSlashRE = /\\/g
export const isWindows = os.platform() === 'win32'

export function slash(p: string): string {
return p.replace(windowsSlashRE, '/')
}

export function normalizePath(id: string): string {
return path.posix.normalize(isWindows ? slash(id) : id)
}
2 changes: 2 additions & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './fs'
export * from './date'
17 changes: 17 additions & 0 deletions packages/shared/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "esnext",
"lib": ["ESNext", "DOM"],
"jsx": "preserve",
"module": "esnext",
"moduleResolution": "node",
"baseUrl": ".",
"resolveJsonModule": true,
"allowJs": true,
"outDir": "dist",
"esModuleInterop": true,
"strict": true,
"noUnusedLocals": true,
"skipLibCheck": true
}
}
11 changes: 11 additions & 0 deletions packages/theme/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @sugarat/theme

## 0.4.4

### Patch Changes

- feat: RSS 生成支持配置多个
- feat: pagefind 配置复用插件配置
- chore: 分离 shared 方法库
- Updated dependencies
- [email protected]
- [email protected]

## 0.4.3

### Patch Changes
Expand Down
Loading

0 comments on commit d32f473

Please sign in to comment.