|
| 1 | +import { isValidElement, type ReactNode } from 'react'; |
| 2 | +import type { BlogListItem } from './index'; |
| 3 | + |
| 4 | +const getTextFromReactNode = (value: ReactNode): string => { |
| 5 | + if (value === undefined || value === null || typeof value === 'boolean') { |
| 6 | + return ''; |
| 7 | + } |
| 8 | + |
| 9 | + if (typeof value === 'string' || typeof value === 'number') { |
| 10 | + return String(value); |
| 11 | + } |
| 12 | + |
| 13 | + if (Array.isArray(value)) { |
| 14 | + return value.map(getTextFromReactNode).join(''); |
| 15 | + } |
| 16 | + |
| 17 | + if (isValidElement<{ children?: ReactNode }>(value)) { |
| 18 | + return getTextFromReactNode(value.props.children); |
| 19 | + } |
| 20 | + |
| 21 | + return ''; |
| 22 | +}; |
| 23 | + |
| 24 | +const escapeMarkdownText = (value: string) => { |
| 25 | + return value.replace(/([\\[\]])/g, '\\$1'); |
| 26 | +}; |
| 27 | + |
| 28 | +const normalizeMarkdownText = (value: ReactNode) => { |
| 29 | + return getTextFromReactNode(value).replace(/\s+/g, ' ').trim(); |
| 30 | +}; |
| 31 | + |
| 32 | +const getPostMarkdownTitle = (post: BlogListItem, index: number) => { |
| 33 | + return normalizeMarkdownText(post.title) || post.href || `Post ${index + 1}`; |
| 34 | +}; |
| 35 | + |
| 36 | +export const isSsgMd = () => Boolean(import.meta.env.SSG_MD); |
| 37 | + |
| 38 | +export const renderBlogListSsgMarkdown = ({ |
| 39 | + formatDate, |
| 40 | + posts, |
| 41 | + subtitle, |
| 42 | + title, |
| 43 | +}: { |
| 44 | + formatDate: (value: BlogListItem['date']) => string | undefined; |
| 45 | + posts: BlogListItem[]; |
| 46 | + subtitle?: ReactNode; |
| 47 | + title?: ReactNode; |
| 48 | +}) => { |
| 49 | + const sections: string[] = []; |
| 50 | + const titleText = normalizeMarkdownText(title); |
| 51 | + const subtitleText = normalizeMarkdownText(subtitle); |
| 52 | + |
| 53 | + if (titleText) { |
| 54 | + sections.push(`# ${titleText}`); |
| 55 | + } |
| 56 | + |
| 57 | + if (subtitleText) { |
| 58 | + sections.push(subtitleText); |
| 59 | + } |
| 60 | + |
| 61 | + posts.forEach((post, index) => { |
| 62 | + const postSections: string[] = []; |
| 63 | + const postTitle = getPostMarkdownTitle(post, index); |
| 64 | + const postDate = formatDate(post.date); |
| 65 | + const postDescription = normalizeMarkdownText(post.description); |
| 66 | + const authors = post.authors?.map(author => author.name).join(', '); |
| 67 | + const meta = [postDate, authors].filter(Boolean).join(' · '); |
| 68 | + |
| 69 | + postSections.push( |
| 70 | + post.href |
| 71 | + ? `## [${escapeMarkdownText(postTitle)}](${post.href})` |
| 72 | + : `## ${postTitle}`, |
| 73 | + ); |
| 74 | + |
| 75 | + if (meta) { |
| 76 | + postSections.push(`> ${meta}`); |
| 77 | + } |
| 78 | + |
| 79 | + if (postDescription) { |
| 80 | + postSections.push(postDescription); |
| 81 | + } |
| 82 | + |
| 83 | + sections.push(postSections.join('\n\n')); |
| 84 | + }); |
| 85 | + |
| 86 | + return `${sections.join('\n\n')}\n`; |
| 87 | +}; |
0 commit comments