-
Notifications
You must be signed in to change notification settings - Fork 65
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 #247 from ATQQ/feature/pagefind-filter-and-sort
feat: add shared lib
- Loading branch information
Showing
39 changed files
with
736 additions
and
765 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
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
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
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
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,5 +1,11 @@ | ||
# @sugarat/create-theme | ||
|
||
## 0.0.67 | ||
|
||
### Patch Changes | ||
|
||
- chore: update deps | ||
|
||
## 0.0.66 | ||
|
||
### Patch Changes | ||
|
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
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
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,7 @@ | ||
# @sugarat/theme-shared | ||
|
||
## 0.0.1 | ||
|
||
### Patch Changes | ||
|
||
- feat: init shared package |
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,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). |
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,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" | ||
} | ||
} |
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,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') | ||
} |
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,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, '<').replace(/>/g, '>') | ||
?.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) | ||
} |
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,2 @@ | ||
export * from './fs' | ||
export * from './date' |
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,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 | ||
} | ||
} |
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,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 | ||
|
Oops, something went wrong.