From 99706729575a00b347df869f9088920648c3f46f Mon Sep 17 00:00:00 2001 From: Mecid Date: Wed, 25 Nov 2026 14:19:57 +0100 Subject: [PATCH] feat(www): show board instructions from armbian.github.io on board pages Board pages now render the Markdown install instructions published from armbian/armbian.github.io/board-instructions (board_instructions in armbian-images.json) instead of relying only on the CMS flash guide. - schemas/api: carry board_instructions through as instructions_url - www: fetch from cache.armbian.com (1h revalidate), render with marked, sanitize, and show inline as a "How to flash" section with a link to the source file on GitHub - the CMS flash guide modal remains as a fallback for boards without repository instructions Closes #67 --- apps/api/src/services/normalizer.ts | 2 + apps/www/package.json | 1 + .../[locale]/boards/[slug]/page.tsx | 75 +++++++++++++++---- apps/www/src/lib/board-instructions.ts | 44 +++++++++++ apps/www/src/lib/sanitize.ts | 18 ++++- packages/config/src/urls.ts | 5 ++ packages/schemas/src/board.ts | 2 + packages/schemas/src/raw.ts | 2 + pnpm-lock.yaml | 10 +++ 9 files changed, 145 insertions(+), 14 deletions(-) create mode 100644 apps/www/src/lib/board-instructions.ts diff --git a/apps/api/src/services/normalizer.ts b/apps/api/src/services/normalizer.ts index d16afe9..87a82d8 100644 --- a/apps/api/src/services/normalizer.ts +++ b/apps/api/src/services/normalizer.ts @@ -327,6 +327,8 @@ export class Normalizer { !enrichment.boardConfigSlugs?.size || enrichment.boardConfigSlugs.has(slug) ? buildCommand(slug) : null, + instructions_url: + boardImages.find((img) => img.board_instructions)?.board_instructions || null, legacy_paths: this.getLegacyPaths(slug, enrichment.redirects), maintainers: boardMaintainers, kernel_branches: kernelBranches, diff --git a/apps/www/package.json b/apps/www/package.json index 26779f7..5419981 100644 --- a/apps/www/package.json +++ b/apps/www/package.json @@ -25,6 +25,7 @@ "embla-carousel-react": "^8.6.0", "jose": "^6.0.0", "lucide-react": "^1.7.0", + "marked": "18.1.3", "motion": "^12.38.0", "next": "^16.4.5", "next-intl": "^4.9.2", diff --git a/apps/www/src/app/(frontend)/[locale]/boards/[slug]/page.tsx b/apps/www/src/app/(frontend)/[locale]/boards/[slug]/page.tsx index eea6901..b3c23f0 100644 --- a/apps/www/src/app/(frontend)/[locale]/boards/[slug]/page.tsx +++ b/apps/www/src/app/(frontend)/[locale]/boards/[slug]/page.tsx @@ -12,6 +12,7 @@ import { ARMBIAN_URLS, vendorLogoUrl, formatBytes, + boardInstructionsSourceUrl, } from '@armbian/config'; import { ApiClientError } from '@armbian/api-client'; import { DonationBanner } from '@/components/board/donation-banner'; @@ -19,7 +20,7 @@ import { BoardJsonLd } from '@/components/board/board-jsonld'; import { BoardImage as BoardImageWithFallback } from '@/components/board/board-image'; import { Link } from '@/i18n/navigation'; import type { BoardSummary, Image as BoardImage } from '@armbian/schemas'; -import { Download, BookOpen, Code, ArrowRight, ArrowLeft } from 'lucide-react'; +import { Download, BookOpen, Code, ArrowRight, ArrowLeft, ArrowDown } from 'lucide-react'; import { SiGithub } from '@icons-pack/react-simple-icons'; import type { Metadata } from 'next'; import { BoardPageDownloads } from '@/components/board/board-page-downloads'; @@ -28,6 +29,7 @@ import { SiblingBoardsCarousel } from '@/components/board/sibling-boards-carouse import { getPayload } from 'payload'; import config from '@payload-config'; import { renderLexicalContent } from '@/lib/cms-lexical'; +import { loadBoardInstructions } from '@/lib/board-instructions'; export const dynamic = 'force-dynamic'; @@ -181,10 +183,10 @@ export default async function BoardPage({ params }: Props) { throw err; } - // Sibling boards (for the "More from X" carousel) and the Payload flash - // guide both depend only on `board` and are independent — fire them in - // parallel to save a round trip on every page render. - const [siblingResult, flashGuideResult] = await Promise.allSettled([ + // Sibling boards (for the "More from X" carousel), the repo install + // instructions and the Payload flash guide all depend only on `board` and + // are independent — fire them in parallel to save round trips. + const [siblingResult, instructionsResult, flashGuideResult] = await Promise.allSettled([ api.getBoards({ vendor: board.vendor_slug, sort: 'popularity', @@ -192,6 +194,7 @@ export default async function BoardPage({ params }: Props) { // current board if it appears in the popularity ordering. limit: 13, }), + loadBoardInstructions(board.instructions_url), (async () => { const payload = await getPayload({ config }); return payload.find({ @@ -211,8 +214,14 @@ export default async function BoardPage({ params }: Props) { siblingBoards = siblingResult.value.data.filter((b) => b.slug !== board.slug).slice(0, 12); } + // Install instructions from armbian/armbian.github.io/board-instructions are + // the source of truth. The CMS flash guide is only a fallback for boards + // that have no instructions in the repository yet. + const instructionsHtml = + instructionsResult.status === 'fulfilled' ? instructionsResult.value : null; + let flashGuide: { title: string; content: string; prerequisites: string[] } | null = null; - if (flashGuideResult.status === 'fulfilled') { + if (!instructionsHtml && flashGuideResult.status === 'fulfilled') { const doc = flashGuideResult.value.docs[0]; if (doc) { const prereqs = Array.isArray(doc.prerequisites) @@ -381,13 +390,24 @@ export default async function BoardPage({ params }: Props) { {t('build_docs')} - {flashGuide && ( - + {instructionsHtml ? ( + + + {t('flash_guide')} + + + ) : ( + flashGuide && ( + + ) )} @@ -404,6 +424,35 @@ export default async function BoardPage({ params }: Props) { boardGithubUrl={board.github_url ?? undefined} /> + {instructionsHtml && ( + +
+
+
+
+ +
+

{t('flash_guide')}

+ + + {t('github')} + +
+ {/* instructionsHtml is sanitized in loadBoardInstructions() */} +
+
+
+
+ )} + {board.build_command && (
diff --git a/apps/www/src/lib/board-instructions.ts b/apps/www/src/lib/board-instructions.ts new file mode 100644 index 0000000..3288a43 --- /dev/null +++ b/apps/www/src/lib/board-instructions.ts @@ -0,0 +1,44 @@ +import { Marked } from 'marked'; +import { ARMBIAN_URLS } from '@armbian/config'; +import { sanitizeCmsHtml } from './sanitize'; + +const FETCH_TIMEOUT_MS = 5_000; +/** Instructions change rarely; let Next's data cache serve them for an hour. */ +const REVALIDATE_SECONDS = 3_600; +const ALLOWED_ORIGIN = new URL(ARMBIAN_URLS.CDN).origin; + +const marked = new Marked({ gfm: true, async: false }); + +/** + * Fetch a board's Markdown install instructions (published from + * armbian/armbian.github.io/board-instructions) and render them to sanitized + * HTML. Returns null when the URL is missing, off the Armbian CDN, or the + * fetch fails, so the board page simply omits the section. + */ +export async function loadBoardInstructions( + url: string | null | undefined, +): Promise { + if (!url) return null; + let parsed: URL; + try { + parsed = new URL(url); + } catch { + return null; + } + // The URL comes from upstream data; only fetch from our own CDN. + if (parsed.origin !== ALLOWED_ORIGIN) return null; + + try { + const res = await fetch(parsed, { + signal: AbortSignal.timeout(FETCH_TIMEOUT_MS), + next: { revalidate: REVALIDATE_SECONDS }, + }); + if (!res.ok) return null; + const markdown = await res.text(); + if (!markdown.trim()) return null; + const html = sanitizeCmsHtml(marked.parse(markdown) as string, { externalLinksInNewTab: true }); + return html.trim() ? html : null; + } catch { + return null; + } +} diff --git a/apps/www/src/lib/sanitize.ts b/apps/www/src/lib/sanitize.ts index a7c4b02..6c66a34 100644 --- a/apps/www/src/lib/sanitize.ts +++ b/apps/www/src/lib/sanitize.ts @@ -1,6 +1,11 @@ import sanitizeHtml from 'sanitize-html'; -export function sanitizeCmsHtml(html: string): string { +interface SanitizeOptions { + /** Open absolute http(s) links in a new tab with a safe `rel`. */ + externalLinksInNewTab?: boolean; +} + +export function sanitizeCmsHtml(html: string, options: SanitizeOptions = {}): string { return sanitizeHtml(html, { allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img', @@ -27,5 +32,16 @@ export function sanitizeCmsHtml(html: string): string { a: ['href', 'target', 'rel'], }, allowedSchemes: ['https', 'mailto'], + transformTags: options.externalLinksInNewTab + ? { + a: (tagName, attribs) => + /^https?:\/\//i.test(attribs['href'] ?? '') + ? { + tagName, + attribs: { ...attribs, target: '_blank', rel: 'noopener noreferrer' }, + } + : { tagName, attribs }, + } + : undefined, }); } diff --git a/packages/config/src/urls.ts b/packages/config/src/urls.ts index 5e9201a..c1efeb7 100644 --- a/packages/config/src/urls.ts +++ b/packages/config/src/urls.ts @@ -146,3 +146,8 @@ export function boardForumUrl(slug: string): string { export function buildCommand(slug: string): string { return `./compile.sh BOARD=${slug} RELEASE=trixie BUILD_DESKTOP=no BUILD_MINIMAL=yes KERNEL_CONFIGURE=no`; } + +/** Source of a board's install instructions in armbian/armbian.github.io (for "edit" links). */ +export function boardInstructionsSourceUrl(slug: string): string { + return `${ARMBIAN_URLS.GITHUB_ORG}/armbian.github.io/blob/main/board-instructions/${slug}.md`; +} diff --git a/packages/schemas/src/board.ts b/packages/schemas/src/board.ts index 7c6c1e7..b76ef90 100644 --- a/packages/schemas/src/board.ts +++ b/packages/schemas/src/board.ts @@ -111,6 +111,8 @@ export const BoardDetailSchema = BoardSummarySchema.extend({ forum_url: z.string().url(), github_url: z.string().url().nullable(), build_command: z.string().nullable(), + /** Markdown install instructions from armbian.github.io/board-instructions, or null. */ + instructions_url: z.string().url().nullable().default(null), legacy_paths: z.array(z.string()).default([]), maintainers: z.array(BoardMaintainerSchema).default([]), kernel_branches: z.array(KernelBranchInfoSchema).default([]), diff --git a/packages/schemas/src/raw.ts b/packages/schemas/src/raw.ts index e483061..a0ea9cd 100644 --- a/packages/schemas/src/raw.ts +++ b/packages/schemas/src/raw.ts @@ -15,6 +15,8 @@ export const RawImageAssetSchema = z.object({ company_name: z.string(), company_website: z.string(), company_logo: z.string(), + /** Absolute URL of the board's install-instructions Markdown, or '' when none exists. */ + board_instructions: z.string().optional(), armbian_version: z.string(), file_url: z.string(), file_url_asc: z.string(), diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b96cf9d..6319c69 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -150,6 +150,9 @@ importers: lucide-react: specifier: ^1.7.0 version: 1.7.0(react@19.2.4) + marked: + specifier: 18.1.3 + version: 18.1.3 motion: specifier: ^12.38.0 version: 12.38.0(react-dom@19.2.4(react@19.2.4))(react@19.2.4) @@ -2684,6 +2687,11 @@ packages: engines: {node: '>= 18'} hasBin: true + marked@18.1.3: + resolution: {integrity: sha512-9ChMH8EVVgjgID49nsL6IWxWk5c6GtAsq/xw7U1ggkjkU5AGuv68oLTVlFRtvcUYZtpbNyjyvGo3YDu0fGapiw==} + engines: {node: '>= 20'} + hasBin: true + md5@2.3.0: resolution: {integrity: sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==} @@ -6033,6 +6041,8 @@ snapshots: marked@14.0.0: {} + marked@18.1.3: {} + md5@2.3.0: dependencies: charenc: 0.0.2