|
| 1 | +import type { Context, Page } from '@/types' |
| 2 | +import type { PageTransformer, TemplateData, Section, LinkData } from './types' |
| 3 | +import { renderContent } from '@/content-render/index' |
| 4 | +import { loadTemplate } from '@/article-api/lib/load-template' |
| 5 | +import { getAllTocItems, flattenTocItems } from '@/article-api/lib/get-all-toc-items' |
| 6 | + |
| 7 | +interface RecommendedItem { |
| 8 | + href: string |
| 9 | + title?: string |
| 10 | + intro?: string |
| 11 | +} |
| 12 | + |
| 13 | +interface BespokeLandingPage extends Omit<Page, 'featuredLinks'> { |
| 14 | + featuredLinks?: Record<string, Array<string | { href: string; title: string; intro?: string }>> |
| 15 | + children?: string[] |
| 16 | + recommended?: RecommendedItem[] |
| 17 | + rawRecommended?: string[] |
| 18 | + includedCategories?: string[] |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Transforms bespoke-landing pages into markdown format. |
| 23 | + * Handles recommended carousel and full article listings. |
| 24 | + * Note: Unlike discovery-landing, bespoke-landing shows ALL articles |
| 25 | + * regardless of includedCategories. |
| 26 | + */ |
| 27 | +export class BespokeLandingTransformer implements PageTransformer { |
| 28 | + templateName = 'landing-page.template.md' |
| 29 | + |
| 30 | + canTransform(page: Page): boolean { |
| 31 | + return page.layout === 'bespoke-landing' |
| 32 | + } |
| 33 | + |
| 34 | + async transform(page: Page, pathname: string, context: Context): Promise<string> { |
| 35 | + const templateData = await this.prepareTemplateData(page, pathname, context) |
| 36 | + |
| 37 | + const templateContent = loadTemplate(this.templateName) |
| 38 | + |
| 39 | + const rendered = await renderContent(templateContent, { |
| 40 | + ...context, |
| 41 | + ...templateData, |
| 42 | + markdownRequested: true, |
| 43 | + }) |
| 44 | + |
| 45 | + return rendered |
| 46 | + } |
| 47 | + |
| 48 | + private async prepareTemplateData( |
| 49 | + page: Page, |
| 50 | + _pathname: string, |
| 51 | + context: Context, |
| 52 | + ): Promise<TemplateData> { |
| 53 | + const bespokePage = page as BespokeLandingPage |
| 54 | + const sections: Section[] = [] |
| 55 | + |
| 56 | + // Recommended carousel |
| 57 | + const recommended = bespokePage.recommended ?? bespokePage.rawRecommended |
| 58 | + if (recommended && recommended.length > 0) { |
| 59 | + const { default: getLearningTrackLinkData } = await import( |
| 60 | + '@/learning-track/lib/get-link-data' |
| 61 | + ) |
| 62 | + |
| 63 | + let links: LinkData[] |
| 64 | + if (typeof recommended[0] === 'object' && 'title' in recommended[0]) { |
| 65 | + links = recommended.map((item) => ({ |
| 66 | + href: typeof item === 'string' ? item : item.href, |
| 67 | + title: (typeof item === 'object' && item.title) || '', |
| 68 | + intro: (typeof item === 'object' && item.intro) || '', |
| 69 | + })) |
| 70 | + } else { |
| 71 | + const linkData = await getLearningTrackLinkData(recommended as string[], context, { |
| 72 | + title: true, |
| 73 | + intro: true, |
| 74 | + }) |
| 75 | + links = (linkData || []).map((item: { href: string; title?: string; intro?: string }) => ({ |
| 76 | + href: item.href, |
| 77 | + title: item.title || '', |
| 78 | + intro: item.intro || '', |
| 79 | + })) |
| 80 | + } |
| 81 | + |
| 82 | + const validLinks = links.filter((l) => l.href && l.title) |
| 83 | + if (validLinks.length > 0) { |
| 84 | + sections.push({ |
| 85 | + title: 'Recommended', |
| 86 | + groups: [{ title: null, links: validLinks }], |
| 87 | + }) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Articles section: recursively gather ALL descendant articles |
| 92 | + // This matches the behavior of the site which uses genericTocFlat/genericTocNested |
| 93 | + // Note: For bespoke-landing pages, the site shows ALL articles regardless of includedCategories |
| 94 | + // (includedCategories only filters for discovery-landing pages) |
| 95 | + if (bespokePage.children && bespokePage.children.length > 0) { |
| 96 | + const tocItems = await getAllTocItems(page, context, { |
| 97 | + recurse: true, |
| 98 | + renderIntros: true, |
| 99 | + }) |
| 100 | + |
| 101 | + // Flatten to get all leaf articles (excludeParents: true means only get articles, not category pages) |
| 102 | + const allArticles = flattenTocItems(tocItems, { excludeParents: true }) |
| 103 | + |
| 104 | + if (allArticles.length > 0) { |
| 105 | + sections.push({ |
| 106 | + title: 'Articles', |
| 107 | + groups: [{ title: null, links: allArticles }], |
| 108 | + }) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + const intro = page.intro ? await page.renderProp('intro', context, { textOnly: true }) : '' |
| 113 | + const title = await page.renderTitle(context, { unwrap: true }) |
| 114 | + |
| 115 | + return { |
| 116 | + title, |
| 117 | + intro, |
| 118 | + sections, |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments