Files
documentation/scripts/prerender.mjs
2026-11-19 19:00:51 -08:00

422 lines
13 KiB
JavaScript

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const rootDir = path.join(__dirname, '..');
// Site tree from main.js
const SITE_TREE = [
{
"id": "vyla",
"title": "Ecosystem",
"path": "/start-here/introduction",
"groups": [
{
"title": "Start here",
"items": [
{
"title": "Introduction",
"file": "/start-here/introduction.md",
"path": "/start-here/introduction",
"hasMedia": true
},
{
"title": "Architecture",
"file": "/start-here/architecture.md",
"path": "/start-here/architecture"
}
]
},
{
"title": "Products",
"items": [
{
"title": "Vyla Home",
"file": "/products/home.md",
"path": "/products/home",
"hasMedia": true
},
{
"title": "Vyla Go",
"file": "/products/go.md",
"path": "/products/go",
"hasMedia": true
},
{
"title": "Vyla TV",
"file": "/products/tv.md",
"path": "/products/tv",
"hasMedia": true
}
]
}
]
},
{
"id": "sdk",
"title": "SDK",
"path": "/sdk",
"groups": [
{
"title": "Getting Started",
"items": [
{
"title": "SDK Overview",
"file": "/sdk/vyla-sdk/index.md",
"path": "/sdk",
"hasMedia": true
},
{
"title": "SDK Quickstart",
"file": "/sdk/vyla-sdk/quickstart.md",
"path": "/sdk/vyla-sdk/quickstart"
}
]
},
{
"title": "Reference",
"items": [
{
"title": "API Reference",
"file": "/sdk/vyla-sdk/reference.md",
"path": "/sdk/vyla-sdk/reference"
}
]
},
{
"title": "Development",
"items": [
{
"title": "Contributing Sources",
"file": "/sdk/vyla-sdk/contributing.md",
"path": "/sdk/vyla-sdk/contributing",
"hasMedia": true
}
]
}
]
},
{
"id": "stream-api",
"title": "Stream API",
"path": "/stream-api/overview",
"groups": [
{
"title": "Overview",
"items": [
{
"title": "Stream API Overview",
"file": "/stream-api/overview/index.md",
"path": "/stream-api/overview"
}
]
},
{
"title": "Reference",
"items": [
{
"title": "Paths & Playback",
"file": "/stream-api/reference/paths.md",
"path": "/stream-api/reference/paths"
},
{
"title": "Media Data",
"file": "/stream-api/reference/media-data.md",
"path": "/stream-api/reference/media-data",
"hasMedia": true
},
{
"title": "Operations & Health",
"file": "/stream-api/reference/operations.md",
"path": "/stream-api/reference/operations"
}
]
},
{
"title": "Deployment",
"items": [
{
"title": "Self Hosting",
"file": "/stream-api/deployment/self-hosting.md",
"path": "/stream-api/deployment/self-hosting"
}
]
}
]
},
{
"id": "resources",
"title": "Resources",
"path": "/misc/project/support-us",
"groups": [
{
"title": "Support Vyla",
"items": [
{
"title": "Support Us",
"file": "/misc/project/support-us.md",
"path": "/misc/project/support-us",
"hasMedia": true
}
]
},
{
"title": "Help & Questions",
"items": [
{
"title": "FAQ",
"file": "/resources/faq.md",
"path": "/resources/faq"
}
]
},
{
"title": "Project",
"items": [
{
"title": "Project Story",
"file": "/misc/project/story.md",
"path": "/misc/project/story"
},
{
"title": "Changelog",
"file": "/misc/project/changelog.md",
"path": "/misc/project/changelog"
},
{
"title": "Credits",
"file": "/misc/project/credit.md",
"path": "/misc/project/credit"
},
{
"title": "DMCA Policy",
"file": "/misc/project/DMCA.md",
"path": "/misc/project/dmca"
}
]
}
]
}
];
function generateStaticHtml(route, mdContent) {
const isHome = route.item.path === '/';
const pageTitle = isHome
? "Vyla Entertainment - Free & Decentralized Media Platform"
: `${route.item.title} - Vyla Docs`;
const pageUrl = `https://vyla.cc${route.item.path}`;
// Extract description from markdown
let desc = "";
if (mdContent) {
const firstPara = mdContent.match(/^#\s+[^\n]+\n+([^\n#]+)/m);
if (firstPara) {
desc = firstPara[1].replace(/\[([^\]]+)\]\([^)]+\)/g, "$1").trim().substring(0, 160);
}
}
if (!desc) {
desc = isHome
? "Vyla Entertainment is a community-built, decentralized media platform and developer ecosystem."
: `${route.item.title} guide and developer documentation for the Vyla Ecosystem.`;
}
const jsonLd = JSON.stringify({
"@context": "https://schema.org",
"@graph": [
{
"@type": isHome ? "WebSite" : "TechArticle",
"@id": pageUrl + (isHome ? "#website" : "#article"),
"url": pageUrl,
"name": isHome ? "Vyla Entertainment" : route.item.title,
"headline": isHome ? "Vyla Entertainment" : route.item.title,
"description": desc,
"inLanguage": "en-US",
"publisher": {
"@id": "https://vyla.cc/#organization"
}
},
{
"@type": "BreadcrumbList",
"@id": pageUrl + "#breadcrumb",
"itemListElement": [
{
"@type": "ListItem",
"position": 1,
"name": "Home",
"item": "https://vyla.cc/"
},
!isHome ? {
"@type": "ListItem",
"position": 2,
"name": route.tab.title,
"item": "https://vyla.cc" + route.tab.path
} : null,
!isHome ? {
"@type": "ListItem",
"position": 3,
"name": route.item.title,
"item": pageUrl
} : null
].filter(Boolean)
}
]
});
return `<!doctype html>
<html lang="en" class="dark">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<meta name="theme-color" content="#070709" />
<meta name="color-scheme" content="dark" />
<meta name="robots" content="index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1" />
<link rel="canonical" href="${pageUrl}" />
<title>${pageTitle}</title>
<meta name="description" content="${desc}" />
<meta name="keywords" content="Vyla, Vyla Entertainment, self-hosted media, media player, streaming platform, open source streaming, Android TV streaming, stream API, developer SDK, decentralized media" />
<meta name="author" content="Vyla Entertainment" />
<meta property="og:locale" content="en_US" />
<meta property="og:type" content="website" />
<meta property="og:title" content="${pageTitle}" />
<meta property="og:description" content="${desc}" />
<meta property="og:url" content="${pageUrl}" />
<meta property="og:site_name" content="Vyla Entertainment" />
<meta property="og:image" content="https://vyla.cc/src/assets/images/thumbnail.png" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="${pageTitle}" />
<meta name="twitter:description" content="${desc}" />
<meta name="twitter:image" content="https://vyla.cc/src/assets/images/thumbnail.png" />
<script type="application/ld+json">${jsonLd}</script>
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<link rel="icon" type="image/png" sizes="16x16" href="/src/assets/images/favicon-16x16.png" />
<link rel="icon" type="image/png" sizes="32x32" href="/src/assets/images/favicon-32x32.png" />
<link rel="apple-touch-icon" sizes="180x180" href="/src/assets/images/apple-touch-icon.png" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" />
<link rel="stylesheet" href="/src/css/bundle.min.css" />
<link rel="manifest" href="/manifest.webmanifest" />
<link rel="ard" href="/.well-known/ard.json" />
</head>
<body class="${isHome ? 'is-home' : ''}">
<script>
// Initial route for SPA
window.__INITIAL_ROUTE__ = ${JSON.stringify(route)};
</script>
<div id="header-container">
<header id="navbar">
<div class="nav-top">
<a class="brand" href="/" aria-label="Vyla Homepage"><img src="/src/assets/images/logo.png"
alt="Vyla Logo" id="brand-logo" width="28" height="28" decoding="async" /></a>
<button id="search-trigger" type="button" onclick="openSearch()" aria-label="Search"></button>
<div class="links">
<button id="settings-trigger" type="button" onclick="openSettings()"
aria-label="Personalize preferences"></button>
<button id="theme-toggle" type="button" onclick="toggleThemeMenu(event)"
aria-label="Toggle theme"></button>
</div>
</div>
<nav class="tabs" id="nav-tabs" aria-label="Main Navigation"></nav>
</header>
</div>
<div id="layout">
<nav id="sidebar" aria-label="Documentation Navigation"></nav>
<div id="content-wrapper">
<div id="content-col">
<div id="main-wrap">
<main class="prose" id="main-content">
<!-- Content will be loaded by client-side JS -->
<div class="skeleton-wrap" aria-hidden="true">
<div class="skeleton-line skeleton-eyebrow"></div>
<div class="skeleton-line skeleton-title"></div>
<div class="skeleton-line skeleton-desc"></div>
<div class="skeleton-line skeleton-text w-90"></div>
<div class="skeleton-line skeleton-text w-100"></div>
<div class="skeleton-line skeleton-text w-75"></div>
</div>
</main>
</div>
</div>
</div>
</div>
<script src="/src/js/main.js"></script>
</body>
</html>`;
}
function resolveRoute(pathname) {
const path = pathname || '/';
if (!path.startsWith('/')) return '/' + path;
const cleanPath = path.replace(/\/+$/, '') || '/';
if (cleanPath === '/') {
return { tab: SITE_TREE[0], group: { title: 'Vyla' }, item: { title: 'Home', path: '/' } };
}
for (const tab of SITE_TREE) {
for (const grp of tab.groups) {
for (const item of grp.items) {
const itemPath = item.path.replace(/\/+$/, '') || '/';
if (itemPath.toLowerCase() === cleanPath.toLowerCase()) {
return { tab, group: grp, item };
}
}
}
}
return null;
}
async function buildStaticPages() {
console.log('==> Building static pages for SEO...');
const pages = [];
// Add home page
pages.push({
route: { tab: SITE_TREE[0], group: { title: 'Vyla' }, item: { title: 'Home', path: '/' } },
outputPath: path.join(rootDir, 'index.html')
});
// Add all pages from SITE_TREE
for (const tab of SITE_TREE) {
for (const grp of tab.groups) {
for (const item of grp.items) {
const route = { tab, group: grp, item };
const cleanPath = item.path.replace(/^\//, '').replace(/\/$/, '') || 'index';
const outputPath = path.join(rootDir, cleanPath + '.html');
// Create directory if needed
const dir = path.dirname(outputPath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
pages.push({ route, outputPath });
}
}
}
// Generate HTML for each page
for (const { route, outputPath } of pages) {
try {
const html = generateStaticHtml(route, null);
fs.writeFileSync(outputPath, html, 'utf-8');
console.log(` Generated: ${outputPath}`);
} catch (err) {
console.error(` Error generating ${outputPath}:`, err.message);
}
}
console.log(`==> Generated ${pages.length} static pages`);
}
buildStaticPages().catch(console.error);