Files
ghatreh_user/src/views/base/about/aboutContent.ts
T
2026-07-18 11:30:40 +03:30

74 lines
1.9 KiB
TypeScript

export interface AboutSection {
title?: string
paragraphs?: string[]
items?: string[]
/** متن HTML از Setting — در صورت وجود، اولویت با این است */
html?: string
}
const ABOUT_SETTING_KEYS = ['text_about'] as const
export function getAboutSections(projectName: string): AboutSection[] {
return [
{
paragraphs: [`${projectName} وبسایت رسمی است.`],
},
]
}
function looksLikeHtml(value: string) {
return /<\/?[a-z][\s\S]*>/i.test(value)
}
function parseAboutSettingValue(raw: string): AboutSection[] | null {
const value = raw.trim()
if (!value) return null
try {
const decoded = JSON.parse(value)
if (Array.isArray(decoded) && decoded.length > 0) {
return decoded as AboutSection[]
}
if (decoded && typeof decoded === 'object' && typeof decoded.html === 'string') {
const html = decoded.html.trim()
if (html) return [{ html }]
}
if (decoded && typeof decoded === 'object' && typeof decoded.text === 'string') {
return parseAboutSettingValue(decoded.text)
}
} catch {
// plain text / html
}
if (looksLikeHtml(value)) {
return [{ html: value }]
}
const paragraphs = value
.split(/\r?\n+/)
.map((line) => line.trim())
.filter(Boolean)
return [{ paragraphs: paragraphs.length > 0 ? paragraphs : [value] }]
}
export function getAboutSettingText(
getSettingByKey: (key: string) => unknown,
): string {
for (const key of ABOUT_SETTING_KEYS) {
const value = String(getSettingByKey(key) ?? '').trim()
if (value) return value
}
return ''
}
/** اولویت با Setting؛ در صورت خالی بودن، متن پیش‌فرض */
export function resolveAboutSections(
projectName: string,
settingsText?: string | null,
): AboutSection[] {
const fromSettings = parseAboutSettingValue(String(settingsText ?? ''))
if (fromSettings?.length) return fromSettings
return getAboutSections(projectName)
}