document.addEventListener("DOMContentLoaded", function () {
function cleanText(text) {
return text
.replace(/
/gi, "\n")
.replace(/<\/p>/gi, "\n")
.replace(/<[^>]*>/g, "")
.replace(/ /g, " ")
.trim();
}
function processAllContent() {
document.querySelectorAll("main, article, .blog-description, .blog-post-content, .article__content, .page-content, .content-entry").forEach(function(el) {
if (!el.innerHTML) return;
if (el.dataset.faqDone === "true") return;
if (el.innerHTML.includes("|اسئلة|")) {
processFAQ(el);
}
if (el.innerHTML.includes("|جدول|")) {
processTable(el);
}
el.dataset.faqDone = "true";
});
}
function processFAQ(targetElement) {
var faqRegex = /\|اسئلة\|([\s\S]*?)\|نهاية\|/g;
targetElement.innerHTML = targetElement.innerHTML.replace(faqRegex, function(match, faqContent) {
var lines = cleanText(faqContent)
.split("\n")
.map(function(line) { return line.trim(); })
.filter(function(line) { return line.length > 0; });
var html = '
';
var q = "";
var a = "";
var schema = [];
lines.forEach(function(line) {
if (line.startsWith("س:")) {
if (q && a) {
html += faqItem(q, a);
schema.push(schemaItem(q, a));
}
q = line.replace("س:", "").trim();
a = "";
}
else if (line.startsWith("ج:")) {
a = line.replace("ج:", "").trim();
}
else if (a) {
a += " " + line;
}
});
if (q && a) {
html += faqItem(q, a);
schema.push(schemaItem(q, a));
}
html += "
";
if (schema.length && !document.getElementById("ghalia-faq-schema")) {
var script = document.createElement("script");
script.type = "application/ld+json";
script.id = "ghalia-faq-schema";
script.text = JSON.stringify({
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": schema
});
document.head.appendChild(script);
}
return html;
});
}
function schemaItem(q, a) {
return {
"@type": "Question",
"name": q,
"acceptedAnswer": {
"@type": "Answer",
"text": a
}
};
}
function faqItem(q, a) {
return '';
}
function processTable(targetElement) {
var tableRegex = /\|جدول\|([\s\S]*?)\|الجدول\|/g;
targetElement.innerHTML = targetElement.innerHTML.replace(tableRegex, function(match, tableContent) {
var lines = cleanText(tableContent)
.split("\n")
.map(function(line) { return line.trim(); })
.filter(function(line) { return line.length > 0; });
if (!lines.length) return match;
var header = lines[0].split("|").map(function(c) {
return c.trim();
}).filter(Boolean);
var html = '';
header.forEach(function(col) {
html += "| " + col + " | ";
});
html += "
";
lines.slice(1).forEach(function(line) {
var cols = line.split("|").map(function(c) {
return c.trim();
}).filter(Boolean);
if (!cols.length) return;
html += "";
cols.forEach(function(col) {
html += "| " + col + " | ";
});
html += "
";
});
html += "
";
return html;
});
}
processAllContent();
setTimeout(processAllContent, 1000);
setTimeout(processAllContent, 2500);
});