BlogViews.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import ReactMarkdown from 'react-markdown';
  2. import remarkGfm from 'remark-gfm';
  3. import type { BlogArticle, BlogLocale } from '@/lib/blog/types';
  4. import { articlePath, blogPath, visibleArticles } from '@/lib/blog/data';
  5. import { articleSchema } from '@/lib/blog/metadata';
  6. import { copy, editorialCopy } from '@/lib/blog/translations';
  7. import { guideServices, serviceCopy } from '@/lib/blog/services';
  8. import { localizedPath } from '@/lib/i18n/routes';
  9. export function BlogIndex({ locale }: { locale: BlogLocale }) {
  10. const t = copy[locale];
  11. const e = editorialCopy[locale];
  12. const articles = visibleArticles(locale);
  13. return <>
  14. <section className="blog-hero"><p className="blog-eyebrow">{t.region}</p><h1>{t.title}</h1><p>{t.description}</p></section>
  15. <div className="blog-grid">{articles.map((article, index) => <article key={article.id} className="blog-card">
  16. <span className="blog-card-number">0{index + 1}</span><h2><a href={articlePath(article)}>{article.title}</a></h2><p>{article.description}</p><a className="blog-read" href={articlePath(article)}>{t.read} <span aria-hidden="true">↗</span></a>
  17. </article>)}</div>
  18. <section className="blog-routes">
  19. <h2 id="blog-routes-title">{e.routesTitle}</h2><p>{e.routesNote}</p>
  20. <p id="blog-table-help" className="blog-table-help">{e.scroll}</p>
  21. <div className="blog-table-scroll" role="region" aria-labelledby="blog-routes-title" aria-describedby="blog-table-help" tabIndex={0}>
  22. <table aria-labelledby="blog-routes-title"><thead><tr>{e.routeHeaders.map(label => <th key={label} scope="col">{label}</th>)}</tr></thead>
  23. <tbody>{articles.filter(article => e.routes[article.id]).map(article => <tr key={article.id}>
  24. <th scope="row"><a href={articlePath(article)}>{article.destinationLabel}</a></th>
  25. <td><a href={article.officialBooking.url}>{e.routes[article.id].process}</a></td>
  26. <td>{e.routes[article.id].location}</td>
  27. </tr>)}</tbody>
  28. </table>
  29. </div>
  30. </section>
  31. <aside className="blog-notice"><strong>{t.independent}</strong><p>{t.disclaimer}</p></aside>
  32. </>;
  33. }
  34. export function BlogArticleView({ article }: { article: BlogArticle }) {
  35. const t = copy[article.locale];
  36. const e = editorialCopy[article.locale];
  37. const service = guideServices[article.id];
  38. const conversion = serviceCopy[article.locale];
  39. const markdownLink = ({ href, children }: { href?: string; children?: React.ReactNode }) => <a href={href ? localizedPath(href, article.locale) : undefined}>{children}</a>;
  40. const related = visibleArticles(article.locale).filter(a => article.relatedIds.includes(a.id));
  41. const firstSection = article.body.search(/^## /m);
  42. const opening = firstSection < 0 ? article.body : article.body.slice(0, firstSection);
  43. const body = firstSection < 0 ? '' : article.body.slice(firstSection);
  44. const sections = body.split('\n').flatMap((line, index) => line.startsWith('## ') ? [{ title: line.slice(3), line: index + 1 }] : [])
  45. .map((section, index) => ({ ...section, id: `section-${index + 1}` }));
  46. // Only our typed, repository-owned data is serialized. Escape '<' to prevent script termination.
  47. const schema = JSON.stringify(articleSchema(article)).replace(/</g, '\\u003c');
  48. return <>
  49. <script type="application/ld+json" dangerouslySetInnerHTML={{ __html: schema }} />
  50. <nav className="blog-breadcrumb" aria-label={t.guides}><a href={blogPath(article.locale)}>{t.guides}</a><span aria-hidden="true"> / </span><span>{article.title}</span></nav>
  51. <article className="blog-article"><header className="blog-article-header"><p className="blog-eyebrow">{t.ireland} → {article.destinationLabel}</p><h1>{article.title}</h1><p className="blog-lede">{article.description}</p><p className="blog-reviewed"><span>{t.by} <a href="#blog-sources">STRATA</a></span><span>{t.reviewed}: <time dateTime={article.reviewedAt}>{new Intl.DateTimeFormat(article.locale, { dateStyle: 'long', timeZone: 'UTC' }).format(new Date(`${article.reviewedAt}T00:00:00Z`))}</time></span></p></header>
  52. <div className="blog-prose">
  53. <ReactMarkdown remarkPlugins={[remarkGfm]} skipHtml components={{ a: markdownLink }}>{opening}</ReactMarkdown>
  54. {sections.length > 0 && <nav className="blog-toc" aria-label={t.contents}><strong>{t.contents}</strong><ol>{sections.map(section => <li key={section.id}><a href={`#${section.id}`}>{section.title}</a></li>)}</ol></nav>}
  55. <ReactMarkdown remarkPlugins={[remarkGfm]} skipHtml components={{
  56. a: markdownLink,
  57. h2: ({ node, children }) => <h2 id={sections.find(section => section.line === node?.position?.start.line)?.id} tabIndex={-1}>{children}</h2>,
  58. table: ({ children }) => <><p id="blog-table-help" className="blog-table-help">{e.scroll}</p><div className="blog-table-scroll" role="region" aria-label={e.preparation} aria-describedby="blog-table-help" tabIndex={0}><table><caption>{e.preparation}</caption>{children}</table></div></>,
  59. th: ({ children }) => <th scope="col">{children}</th>,
  60. }}>{body}</ReactMarkdown>
  61. </div>
  62. <section className="blog-sources"><h2 id="blog-sources" tabIndex={-1}>{t.sources}</h2><p>{t.sourceNote}</p><ul>{article.sources.map(source => <li key={source.url}><a href={source.url} rel="noopener noreferrer">{source.title}</a></li>)}</ul><p>{e.correctionNote}</p><a href={`mailto:support@text.skin?subject=${encodeURIComponent(`${e.corrections}: ${articlePath(article)}`)}`}>{e.corrections}</a></section>
  63. </article>
  64. {service && <section className="blog-conversion" aria-labelledby="blog-conversion-title" data-service-id={service.id}>
  65. <h2 id="blog-conversion-title">{conversion.heading}</h2>
  66. <p>{conversion.description}</p>
  67. <a className="blog-service-button" href={localizedPath(`/create-order/${service.id}`, article.locale)}>{conversion.buttons[service.id]} <span aria-hidden="true">→</span></a>
  68. <p className="blog-conversion-note">{conversion.note} <a href={localizedPath('/terms', article.locale)}>{t.terms}</a> · <a href={localizedPath('/refund-policy', article.locale)}>{t.refunds}</a></p>
  69. </section>}
  70. <aside className="blog-notice"><strong>{t.independent}</strong><p>{t.disclaimer}</p><a className="blog-button" href={article.officialBooking.url}>{article.officialBooking.label} ↗</a></aside>
  71. {related.length > 0 && <section className="blog-related"><h2>{t.related}</h2><ul>{related.map(a => <li key={a.id}><a href={articlePath(a)}>{a.title} →</a></li>)}</ul></section>}
  72. </>;
  73. }