document.addEventListener("DOMContentLoaded", () => {
// Function to extract the first sentence
function getFirstSentence(text) {
const sentences = text.match(/[^.!?]+[.!?]/); // Match first sentence ending with ., !, or ?
return sentences ? sentences[0].trim() : ''; // Return first sentence or empty string if not found
}
// Select the main content element (adjust the selector based on your Webflow structure)
const contentElement = document.querySelector(".main-content"); // Replace '.main-content' with your specific class or ID
const titleElement = document.querySelector(".page-title"); // Replace '.page-title' with your specific class or ID for the page title
// Set the meta description dynamically
if (contentElement) {
const textContent = contentElement.textContent.trim(); // Get text content of the main content
const firstSentence = getFirstSentence(textContent);
let metaDescription = document.querySelector("meta[name='description']");
if (!metaDescription) {
// If a meta description tag doesn't exist, create one
metaDescription = document.createElement("meta");
metaDescription.setAttribute("name", "description");
document.head.appendChild(metaDescription);
}
metaDescription.setAttribute("content", firstSentence);
console.log("Meta description set to:", firstSentence); // Log for debugging
} else {
console.warn("Main content element not found. Ensure the selector is correct.");
}
// Set the title dynamically
if (titleElement) {
const pageTitle = titleElement.textContent.trim(); // Get text content of the page title
document.title = pageTitle; // Set the title tag dynamically
console.log("Page title set to:", pageTitle); // Log for debugging
} else {
console.warn("Page title element not found. Ensure the selector is correct.");
}
});