Gwk-Cultural-Park/components/Wordpress/column.tsx

41 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2024-09-07 01:40:25 +00:00
function ColumnLayout({ paragraph }) {
// Split the paragraph into sentences
const sentences = paragraph.split('. ')
// Count the number of sentences that contain images
const imageSentences = sentences.filter((sentence) =>
sentence.includes('<img')
)
let columnCount
if (imageSentences.length === 1) {
columnCount = 1
} else if (imageSentences.length === 2) {
columnCount = 2
} else if (imageSentences.length >= 3) {
columnCount = 3
} else {
columnCount = 1 // Default to one column if no images are found
}
// Generate an array of columns
const columns = Array.from({ length: columnCount }, (_, index) => (
<div key={index} className="column">
{/* Render the sentences for this column */}
{imageSentences
.slice(
index * (imageSentences.length / columnCount),
(index + 1) * (imageSentences.length / columnCount)
)
.map((sentence, i) => (
<p key={i}>{sentence}</p>
))}
</div>
))
return <div className="column-layout">{columns}</div>
}
export default ColumnLayout