import QParser from 'q2filter' export function escapeRegExp (s: string) { return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') // $& means the whole matched string } export function normalizeArray (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 { 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 } } }