add blog
This commit is contained in:
1 parent
36338ed0d7
commit
7fe8f5d154
96 files changed
+21326
No files matched your search
@@ -0,0 +1,65 @@
|
||||
import QParser from 'q2filter'
|
||||
|
||||
export function escapeRegExp (s: string) {
|
||||
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') // $& means the whole matched string
|
||||
}
|
||||
|
||||
export function normalizeArray<T> (it: T | T[]): T | undefined {
|
||||
if (Array.isArray(it)) {
|
||||
return it[0]
|
||||
}
|
||||
|
||||
return it
|
||||
}
|
||||
|
||||
export function searchPosts (options: {
|
||||
q?: any
|
||||
isBlog?: boolean
|
||||
current?: boolean
|
||||
posts: any[]
|
||||
}) {
|
||||
let posts: any[] = options.posts
|
||||
|
||||
if (options.isBlog) {
|
||||
posts = posts.filter((p) => p.path.startsWith('blog/'))
|
||||
}
|
||||
|
||||
if (options.current) {
|
||||
const now = new Date().toISOString()
|
||||
posts = posts.filter((h) => h.date && h.date <= now)
|
||||
}
|
||||
|
||||
const parser = new QParser(options.q || {}, {
|
||||
isString: ['title', 'author', 'tag'],
|
||||
isDate: ['date'],
|
||||
filters: {
|
||||
'is:reversed': (items) => {
|
||||
return items.reverse()
|
||||
},
|
||||
},
|
||||
sortBy: {
|
||||
key: 'date',
|
||||
desc: true,
|
||||
},
|
||||
})
|
||||
|
||||
return parser.parse(posts)
|
||||
}
|
||||
|
||||
export async function apiHash (u: string, h: string): Promise<string> {
|
||||
if (typeof location !== 'undefined') {
|
||||
const url = new URL(u, location.origin)
|
||||
url.searchParams.set('h', h)
|
||||
return fetch(url.href, {
|
||||
mode: 'no-cors',
|
||||
}).then((r) => r.text())
|
||||
} else {
|
||||
if (/\.json$/i.test(u)) {
|
||||
const req = (require as any).context('@/static', true, /\.json$/i)
|
||||
return JSON.stringify(req(`.${u}`))
|
||||
} else {
|
||||
const req = (require as any).context('@/static', true, /\.(html?|md|txt|ya?ml)$/i)
|
||||
return req(`.${u}`).default
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user