- posts.author_id 마이그레이션 및 owner/admin 단일 계정 환경에서만 기존 글 backfill - 공개 상세: 글쓴이 본인일 때만 공유 옆 수정 링크 표시, 수정 시각 제거 - 목록 요약: excerpt 없을 때 본문 fallback, post-summary-clamp로 말줄임 처리 - 회원 세션 API에 isAdmin·role 추가
40 lines
1.3 KiB
JavaScript
40 lines
1.3 KiB
JavaScript
/**
|
|
* 게시물 요약 또는 본문에서 목록·메타용 짧은 텍스트를 만든다.
|
|
* @param {string} excerpt - 게시물 요약
|
|
* @param {string} content - 게시물 본문(마크다운)
|
|
* @param {Object} [options] - 옵션
|
|
* @param {number} [options.maxLength=160] - 최대 글자 수
|
|
* @param {boolean} [options.appendEllipsis=true] - 잘린 문자열 끝에 말줄임 추가 여부
|
|
* @returns {string} 화면 표시용 요약
|
|
*/
|
|
export function createPostSummary(excerpt = '', content = '', options = {}) {
|
|
const maxLength = Number(options.maxLength) > 0 ? Number(options.maxLength) : 160
|
|
const appendEllipsis = options.appendEllipsis !== false
|
|
const source = String(excerpt || '').trim() || String(content || '')
|
|
|
|
const plainText = source
|
|
.replace(/```[\s\S]*?```/g, ' ')
|
|
.replace(/:::[\s\S]*?:::/g, ' ')
|
|
.replace(/<!--[\s\S]*?-->/g, ' ')
|
|
.replace(/!\[[^\]]*]\([^)]*\)/g, ' ')
|
|
.replace(/\[([^\]]+)]\([^)]*\)/g, '$1')
|
|
.replace(/https?:\/\/\S+/g, ' ')
|
|
.replace(/[#>*_`~|-]+/g, ' ')
|
|
.replace(/\s+/g, ' ')
|
|
.trim()
|
|
|
|
if (!plainText) {
|
|
return ''
|
|
}
|
|
|
|
if (plainText.length <= maxLength) {
|
|
return plainText
|
|
}
|
|
|
|
if (!appendEllipsis) {
|
|
return plainText.slice(0, maxLength).trim()
|
|
}
|
|
|
|
return `${plainText.slice(0, maxLength - 3).trim()}...`
|
|
}
|