دوره‌ی سئو برای مهندس‌ها
۰۵
✓ پاسخ‌نامه

پاسخ‌نامه‌ی درس ۰۵

اول خودت حل کن. پاسخ‌نامه برای مقایسه است، نه برای شروع.

۵-۱

  • blue-runner: قیمت با رقم فارسی و جداکننده (۲۲٬۰۰۰٬۰۰۰) که عدد معتبر نیست؛ قیمت داده (۲۲ میلیون ریال) با قیمت صفحه (۲٫۵ میلیون تومان = ۲۵ میلیون ریال) نمی‌خواند؛ aggregateRating با ۱۲٬۰۰۰ نظر که روی صفحه هیچ نظری نیست (نقض قوانین؛ ریسک manual action).
  • red-trail، white-court و محصول سبز: JSON-LD معتبر است، ولی image یک تصویر عمومی برای همه است و sku و brand ندارند (توصیه‌شده).

۵-۲

{
  "@context": "https://schema.org",
  "@graph": [
    {
      "@type": "Product",
      "@id": "https://broken-shop.example/products/blue-runner#product",
      "name": "کفش دویدن آبی رانر",
      "image": ["https://broken-shop.example/img/blue-runner-1x1.jpg"],
      "description": "کفش سبک دویدن با زیره‌ی فوم، مناسب مسافت طولانی و تمرین روزانه.",
      "sku": "BR-001",
      "brand": { "@type": "Brand", "name": "رانر" },
      "offers": {
        "@type": "Offer",
        "url": "https://broken-shop.example/products/blue-runner",
        "priceCurrency": "IRR",
        "price": "25000000",
        "availability": "https://schema.org/InStock",
        "itemCondition": "https://schema.org/NewCondition"
      }
    },
    {
      "@type": "BreadcrumbList",
      "itemListElement": [
        { "@type": "ListItem", "position": 1, "name": "خانه", "item": "https://broken-shop.example/" },
        { "@type": "ListItem", "position": 2, "name": "کفش دویدن", "item": "https://broken-shop.example/shoes/running" },
        { "@type": "ListItem", "position": 3, "name": "کفش دویدن آبی رانر" }
      ]
    }
  ]
}

(آخرین ListItem می‌تواند item نداشته باشد.)

۵-۳

{
  "@context": "https://schema.org",
  "@graph": [
    { "@type": "Organization", "@id": "https://example.com/#org", "name": "شرکت نمونه",
      "url": "https://example.com/", "logo": "https://example.com/logo.png" },
    { "@type": "Person", "@id": "https://example.com/authors/sara#person", "name": "سارا احمدی",
      "url": "https://example.com/authors/sara", "jobTitle": "مهندس ارشد بک‌اند",
      "sameAs": ["https://github.com/sara-example", "https://www.linkedin.com/in/sara-example"] },
    { "@type": "BlogPosting", "@id": "https://example.com/blog/postgres-indexing#article",
      "headline": "راهنمای عملی ایندکس‌گذاری در PostgreSQL",
      "image": ["https://example.com/img/pg-index-16x9.png"],
      "datePublished": "2026-10-02T08:00:00+03:30",
      "dateModified": "2026-11-15T10:20:00+03:30",
      "author": { "@id": "https://example.com/authors/sara#person" },
      "publisher": { "@id": "https://example.com/#org" },
      "mainEntityOfPage": "https://example.com/blog/postgres-indexing" }
  ]
}

۵-۴

def check_breadcrumb(obj, path, errs):
    items = obj.get("itemListElement", [])
    positions = [int(i.get("position", 0)) for i in items]
    if positions != list(range(1, len(items) + 1)):
        errs.append(f"{path}: positions must be 1..n in order, got {positions}")
    for i, it in enumerate(items[:-1]):
        item = it.get("item")
        url = item.get("@id") if isinstance(item, dict) else item
        if not (isinstance(url, str) and url.startswith(("http://", "https://"))):
            errs.append(f"{path}.itemListElement[{i}]: item must be an absolute URL")
# در check(): if t == "BreadcrumbList": check_breadcrumb(obj, path, errs)

۵-۵

export function jsonLdScript(data: unknown): string {
  const json = JSON.stringify(data)
    .replace(/</g, "\\u003c")
    .replace(/>/g, "\\u003e")
    .replace(/&/g, "\\u0026")
    .replace(/
/g, "\\u2028")
    .replace(/
/g, "\\u2029");
  return `<script type="application/ld+json">${json}</script>`;
}

// test
const html = jsonLdScript({ "@type": "Product", name: "</script><script>alert(1)</script>" });
console.assert(!html.slice(0, -9).includes("</script>"), "script breakout!");
console.assert(JSON.parse(html.slice(35, -9)).name.includes("alert"), "data preserved");

JSON.parse نسخه‌ی < را دوباره به < تبدیل می‌کند، پس داده برای موتور دقیقاً همان است.