You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.6 KiB
TypeScript

import { Article, Post } from '../config/baseConfig'
import { useData, withBase } from 'vitepress'
export function getTags(post: Post[]) {
let data: Array<string> = []
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<Article> = []
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<Article> = []
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)
}