import { Article, Post } from '../config/baseConfig' import { useData, withBase } from 'vitepress' export function getTags(post: Post[]) { let data: Array = [] for (let index = 0; index < post.length; index++) { const element = post[index] const tags = element.frontMatter.tags if (tags && tags.length > 0) { tags.forEach((item) => { data.push(item) }) } } // data = [...new Set(data)]; return data.sort() } export function initTags(post: Post[]) { const data: any = {} for (let index = 0; index < post.length; index++) { const element = post[index] const tags = element.frontMatter.tags if (tags && tags.length > 0) { tags.forEach((item) => { if (data[item]) { data[item].push(element) } else { data[item] = [] data[item].push(element) } }) } } return data } export function searchData(post: Post[], keyword: string) { const data: Array
= [] for (let index = 0; index < post.length; index++) { const element = post[index] let hasArticle = false if (element.frontMatter.title.includes(keyword)) { hasArticle = true } if (element.frontMatter.category.includes(keyword)) { hasArticle = true } if (element.frontMatter.description.includes(keyword)) { hasArticle = true } const tags = element.frontMatter.tags if (tags && tags.length > 0) { tags.forEach((tag) => { if (tag && tag.includes(keyword)) { hasArticle = true } }) } if (hasArticle) { data.push(element) } } return data } export function initLists(post: Post[], category: string) { const data: Array
= [] for (let index = 0; index < post.length; index++) { const element = post[index] if (element.frontMatter.category == category) { data.push(element) } } return data } export function useYearSort(post: Post[]) { const data = [] let year = '0' let num = -1 for (let index = 0; index < post.length; index++) { const element = post[index] if (element.frontMatter.date) { const y = element.frontMatter.date.split('-')[0] if (y === year) { data[num].push(element) } else { num++ data[num] = [] as any data[num].push(element) year = y } } } return data } export function getWithBase(url: string) { let baseUrl = url const { theme } = useData() if (theme.value.docRoot) { baseUrl = url.replace('/' + theme.value.docRoot + '/', '') } return withBase(baseUrl) }