54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
|
|
import { loadPublicSettings } from '../composables/usePublicSettings'
|
|||
|
|
import {
|
|||
|
|
fetchPosts, fetchPost, fetchWorks, fetchCategories,
|
|||
|
|
fetchSnippets, fetchColumns, fetchTags, fetchAboutProfile,
|
|||
|
|
type PublicSettings,
|
|||
|
|
} from '../services/api'
|
|||
|
|
|
|||
|
|
export type DataSource =
|
|||
|
|
| 'posts' | 'post' | 'works' | 'categories'
|
|||
|
|
| 'snippets' | 'columns' | 'tags' | 'about'
|
|||
|
|
|
|||
|
|
// 解析 bindings 中的 settings 值("settings.site_title")
|
|||
|
|
export async function resolveSetting(key: string): Promise<string> {
|
|||
|
|
const s = await loadPublicSettings() as PublicSettings
|
|||
|
|
const v = (s as Record<string, unknown>)[key]
|
|||
|
|
return v == null ? '' : String(v)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 加载数据源
|
|||
|
|
export async function loadDataSource(source: DataSource, params: Record<string, any> = {}): Promise<any> {
|
|||
|
|
switch (source) {
|
|||
|
|
case 'posts': {
|
|||
|
|
const pageSize = Number(params.pageSize) || 10
|
|||
|
|
const res = await fetchPosts(undefined, undefined, undefined, undefined, 1, pageSize)
|
|||
|
|
return res?.list || []
|
|||
|
|
}
|
|||
|
|
case 'post':
|
|||
|
|
return fetchPost(String(params.id ?? ''))
|
|||
|
|
case 'works':
|
|||
|
|
return fetchWorks()
|
|||
|
|
case 'categories':
|
|||
|
|
return fetchCategories()
|
|||
|
|
case 'snippets':
|
|||
|
|
return fetchSnippets()
|
|||
|
|
case 'columns':
|
|||
|
|
return fetchColumns()
|
|||
|
|
case 'tags':
|
|||
|
|
return fetchTags()
|
|||
|
|
case 'about':
|
|||
|
|
return fetchAboutProfile()
|
|||
|
|
default:
|
|||
|
|
return null
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 轻量 HTML 清洗:移除 script/事件属性,防止注入脚本 */
|
|||
|
|
export function sanitizeStaticHtml(html: string): string {
|
|||
|
|
if (!html) return ''
|
|||
|
|
return html
|
|||
|
|
.replace(/<script[\s\S]*?<\/script>/gi, '')
|
|||
|
|
.replace(/\son\w+\s*=\s*("[^"]*"|'[^']*'|[^\s>]+)/gi, '')
|
|||
|
|
.replace(/javascript:/gi, '')
|
|||
|
|
}
|