feat(seo): implement FAQPage schema, sub-sitemaps and local Lalezar WOFF2 font (SEO-P1-07, SEO-P1-10, SEO-P2-16)
Deploy Canina / deploy (push) Successful in 2m10s
Deploy Canina / deploy (push) Successful in 2m10s
- SEO-P1-07: extract FAQs and accordion items from blog content and inject JSON-LD FAQPage schema - SEO-P1-10: add dynamic sub-sitemaps /sitemap-blog.xml and /sitemap-wiki.xml and register in robots.ts - SEO-P2-16: host Lalezar font locally as optimized WOFF2 with preload enabled in lib/fonts.ts - Add full regression test suites with 100% pass rate
This commit is contained in:
1 parent
5cf2863987
commit
f8f9329c50
11 files changed
+556
-6
No files matched your search
@@ -0,0 +1,64 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { extractFaqsFromHtml, generateFAQPageSchema } from '../extractFaqs';
|
||||
|
||||
describe('extractFaqsFromHtml and generateFAQPageSchema (SEO-P1-07)', () => {
|
||||
it('extracts FAQs from HTML5 details and summary tags', () => {
|
||||
const html = `
|
||||
<article>
|
||||
<h1>مراقبت از مفاصل سگ</h1>
|
||||
<details>
|
||||
<summary>آیا این مکمل برای سگ نژاد بزرگ مناسب است؟</summary>
|
||||
<p>بله، دوز مشخص بر اساس وزن روی بستهبندی درج شده است.</p>
|
||||
</details>
|
||||
<details>
|
||||
<summary>مدت دوره درمان چقدر است؟</summary>
|
||||
حداقل ۴ الی ۶ هفته مصرف مداوم توصیه میشود.
|
||||
</details>
|
||||
</article>
|
||||
`;
|
||||
const faqs = extractFaqsFromHtml(html);
|
||||
expect(faqs).toHaveLength(2);
|
||||
expect(faqs[0].question).toBe('آیا این مکمل برای سگ نژاد بزرگ مناسب است؟');
|
||||
expect(faqs[0].answer).toBe('بله، دوز مشخص بر اساس وزن روی بستهبندی درج شده است.');
|
||||
expect(faqs[1].question).toBe('مدت دوره درمان چقدر است؟');
|
||||
expect(faqs[1].answer).toBe('حداقل ۴ الی ۶ هفته مصرف مداوم توصیه میشود.');
|
||||
|
||||
const schema = generateFAQPageSchema(faqs);
|
||||
expect(schema).not.toBeNull();
|
||||
expect(schema?.['@type']).toBe('FAQPage');
|
||||
expect(schema?.mainEntity).toHaveLength(2);
|
||||
expect(schema?.mainEntity[0].name).toBe('آیا این مکمل برای سگ نژاد بزرگ مناسب است؟');
|
||||
expect(schema?.mainEntity[0].acceptedAnswer.text).toBe('بله، دوز مشخص بر اساس وزن روی بستهبندی درج شده است.');
|
||||
});
|
||||
|
||||
it('extracts FAQs from accordion or FAQ class structures', () => {
|
||||
const html = `
|
||||
<div class="faq-item">
|
||||
<h4 class="accordion-title">چگونه اصالت محصول را بررسی کنیم؟</h4>
|
||||
<div class="accordion-content">با اسکن کد QR و استعلام شماره بچ در سایت شرکت کنینا.</div>
|
||||
</div>
|
||||
`;
|
||||
const faqs = extractFaqsFromHtml(html);
|
||||
expect(faqs).toHaveLength(1);
|
||||
expect(faqs[0].question).toBe('چگونه اصالت محصول را بررسی کنیم؟');
|
||||
expect(faqs[0].answer).toBe('با اسکن کد QR و استعلام شماره بچ در سایت شرکت کنینا.');
|
||||
});
|
||||
|
||||
it('extracts FAQs from heading questions followed by paragraph answers', () => {
|
||||
const html = `
|
||||
<h2>پرسش: آیا کنینا محصولات ساخت آلمان هستند؟</h2>
|
||||
<p>تمامی محصولات در کشور آلمان تحت استانداردهای دارویی اروپا تولید و بستهبندی میشوند.</p>
|
||||
`;
|
||||
const faqs = extractFaqsFromHtml(html);
|
||||
expect(faqs).toHaveLength(1);
|
||||
expect(faqs[0].question).toBe('پرسش: آیا کنینا محصولات ساخت آلمان هستند؟');
|
||||
expect(faqs[0].answer).toContain('تمامی محصولات در کشور آلمان');
|
||||
});
|
||||
|
||||
it('returns empty array and null schema when no FAQs are found', () => {
|
||||
const html = `<p>یک مقاله بدون هرگونه بخش پرسش و پاسخ.</p>`;
|
||||
const faqs = extractFaqsFromHtml(html);
|
||||
expect(faqs).toHaveLength(0);
|
||||
expect(generateFAQPageSchema(faqs)).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
|
||||
vi.mock('next/font/local', () => ({
|
||||
default: vi.fn((opts) => ({
|
||||
variable: opts.variable,
|
||||
className: 'mock-font',
|
||||
src: opts.src,
|
||||
preload: opts.preload,
|
||||
display: opts.display,
|
||||
})),
|
||||
}));
|
||||
|
||||
describe('Local font configuration and preloading (SEO-P2-16)', () => {
|
||||
it('configures Lalezar as a local font with WOFF2 and preload enabled', async () => {
|
||||
const { lalezar } = await import('../fonts');
|
||||
expect(lalezar).toBeDefined();
|
||||
expect(lalezar.variable).toBe('--font-lalezar');
|
||||
expect((lalezar as any).preload).toBe(true);
|
||||
expect((lalezar as any).display).toBe('swap');
|
||||
expect((lalezar as any).src[0].path).toContain('Lalezar-Regular.woff2');
|
||||
});
|
||||
|
||||
it('configures Vazirmatn as a local font with preload enabled', async () => {
|
||||
const { vazirmatn } = await import('../fonts');
|
||||
expect(vazirmatn).toBeDefined();
|
||||
expect(vazirmatn.variable).toBe('--font-vazir');
|
||||
expect((vazirmatn as any).preload).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,101 @@
|
||||
export interface FAQItem {
|
||||
question: string;
|
||||
answer: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts question and answer pairs from rich HTML content or structured blog data.
|
||||
* Supports HTML5 <details><summary>, CSS accordion/faq classes, and question headings with paragraph answers.
|
||||
*/
|
||||
export function extractFaqsFromHtml(html: string): FAQItem[] {
|
||||
if (!html || typeof html !== 'string') return [];
|
||||
const faqs: FAQItem[] = [];
|
||||
|
||||
// 1. HTML5 <details><summary>Question</summary>Answer...</details>
|
||||
const detailsRegex = /<details[^>]*>([\s\S]*?)<\/details>/gi;
|
||||
let detailsMatch;
|
||||
while ((detailsMatch = detailsRegex.exec(html)) !== null) {
|
||||
const block = detailsMatch[1];
|
||||
const summaryMatch = /<summary[^>]*>([\s\S]*?)<\/summary>/i.exec(block);
|
||||
if (summaryMatch) {
|
||||
const question = summaryMatch[1].replace(/<[^>]+>/g, '').trim();
|
||||
const answerHtml = block.replace(/<summary[^>]*>[\s\S]*?<\/summary>/i, '');
|
||||
const answer = answerHtml.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim();
|
||||
if (question && answer && !faqs.some((f) => f.question === question)) {
|
||||
faqs.push({ question, answer });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Class-based Accordion or FAQ items:
|
||||
// e.g. <div class="accordion-item"><h3 class="accordion-title">Q</h3><div class="accordion-content">A</div></div>
|
||||
// Use regex looking forward to next item or end of string
|
||||
const itemSplitRegex = /<(?:div|article|section|li)[^>]*class=["'][^"']*(?:faq-item|accordion-item|faq-box|qa-item)[^"']*["'][^>]*>/gi;
|
||||
const itemIndices: number[] = [];
|
||||
let itemMatch;
|
||||
while ((itemMatch = itemSplitRegex.exec(html)) !== null) {
|
||||
itemIndices.push(itemMatch.index);
|
||||
}
|
||||
|
||||
for (let i = 0; i < itemIndices.length; i++) {
|
||||
const start = itemIndices[i];
|
||||
const end = i + 1 < itemIndices.length ? itemIndices[i + 1] : html.length;
|
||||
const block = html.substring(start, end);
|
||||
|
||||
const qMatch =
|
||||
/<[^>]*class=["'][^"']*(?:question|faq-q|accordion-title|accordion-header|faq-header)[^"']*["'][^>]*>([\s\S]*?)<\/[^>]+>/i.exec(
|
||||
block
|
||||
);
|
||||
const aMatch =
|
||||
/<[^>]*class=["'][^"']*(?:answer|faq-a|accordion-content|accordion-body|faq-body)[^"']*["'][^>]*>([\s\S]*?)<\/[^>]+>/i.exec(
|
||||
block
|
||||
);
|
||||
if (qMatch && aMatch) {
|
||||
const question = qMatch[1].replace(/<[^>]+>/g, '').trim();
|
||||
const answer = aMatch[1].replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim();
|
||||
if (question && answer && !faqs.some((f) => f.question === question)) {
|
||||
faqs.push({ question, answer });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Headings with questions followed immediately by paragraph answers:
|
||||
// e.g. <h2>پرسش: آیا این مکمل برای سگ مفید است؟</h2> <p>بله، کاملاً مفید است.</p>
|
||||
const headingRegex = /<h[2-4][^>]*>([\s\S]*?)<\/h[2-4]>\s*<p[^>]*>([\s\S]*?)<\/p>/gi;
|
||||
let headingMatch;
|
||||
while ((headingMatch = headingRegex.exec(html)) !== null) {
|
||||
const rawHeading = headingMatch[1].replace(/<[^>]+>/g, '').trim();
|
||||
const rawParagraph = headingMatch[2].replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim();
|
||||
if (
|
||||
(rawHeading.endsWith('?') ||
|
||||
rawHeading.endsWith('؟') ||
|
||||
/^(?:پرسش|سوال|سؤال|آیا|چگونه|چرا|چه|کدام)\s*[:؛-]?/i.test(rawHeading)) &&
|
||||
rawParagraph.length > 5
|
||||
) {
|
||||
if (!faqs.some((f) => f.question === rawHeading)) {
|
||||
faqs.push({ question: rawHeading, answer: rawParagraph });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return faqs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a Schema.org FAQPage structured data entity if valid FAQs exist.
|
||||
*/
|
||||
export function generateFAQPageSchema(faqs: FAQItem[]) {
|
||||
if (!faqs || faqs.length === 0) return null;
|
||||
return {
|
||||
'@context': 'https://schema.org',
|
||||
'@type': 'FAQPage',
|
||||
mainEntity: faqs.map((faq) => ({
|
||||
'@type': 'Question',
|
||||
name: faq.question,
|
||||
acceptedAnswer: {
|
||||
'@type': 'Answer',
|
||||
text: faq.answer,
|
||||
},
|
||||
})),
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
import { Lalezar } from 'next/font/google';
|
||||
import localFont from 'next/font/local';
|
||||
|
||||
export const vazirmatn = localFont({
|
||||
@@ -14,10 +13,16 @@ export const vazirmatn = localFont({
|
||||
preload: true,
|
||||
});
|
||||
|
||||
export const lalezar = Lalezar({
|
||||
weight: '400',
|
||||
subsets: ['arabic'],
|
||||
export const lalezar = localFont({
|
||||
src: [
|
||||
{
|
||||
path: '../public/fonts/Lalezar-Regular.woff2',
|
||||
weight: '400',
|
||||
style: 'normal',
|
||||
},
|
||||
],
|
||||
variable: '--font-lalezar',
|
||||
display: 'swap',
|
||||
preload: false,
|
||||
fallback: ['cursive', 'system-ui', 'sans-serif'],
|
||||
preload: true,
|
||||
});
|
||||
Reference in new issue
Block a user