#!/usr/bin/env bun /** * For all Markdown under docs/docs with applicationTemplate + applicationShowcase: * - Tabs containing the matching ide.intlayer.org embed get a Demo tab when missing. * - Standalone IDE iframes for that repo are removed; full Tabs (Video/Code/Demo) are inserted after the first H1 when no such Tabs exist. */ import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const DOCS_ROOT = path.join(__dirname, '..', 'docs', 'docs'); type TabLabels = { video: string; code: string; demo: string }; type FileResult = | { changed: false; reason: string } | { changed: true; body: string }; const LABELS: Record = { en: { video: 'Video', code: 'Code', demo: 'Demo' }, 'en-GB': { video: 'Video', code: 'Code', demo: 'Demo' }, fr: { video: 'Vidéo', code: 'Code', demo: 'Démo' }, de: { video: 'Video', code: 'Code', demo: 'Demo' }, es: { video: 'Vídeo', code: 'Código', demo: 'Demo' }, it: { video: 'Video', code: 'Codice', demo: 'Demo' }, pt: { video: 'Vídeo', code: 'Código', demo: 'Demo' }, pl: { video: 'Wideo', code: 'Kod', demo: 'Demo' }, ru: { video: 'Видео', code: 'Код', demo: 'Демо' }, uk: { video: 'Відео', code: 'Код', demo: 'Демо' }, ja: { video: '動画', code: 'コード', demo: 'デモ' }, zh: { video: '视频', code: '代码', demo: '演示' }, ko: { video: '동영상', code: '코드', demo: '데모' }, vi: { video: 'Video', code: 'Mã nguồn', demo: 'Bản demo' }, ar: { video: 'فيديو', code: 'كود', demo: 'تجربة' }, hi: { video: 'वीडियो', code: 'कोड', demo: 'डेमो' }, tr: { video: 'Video', code: 'Kod', demo: 'Demo' }, id: { video: 'Video', code: 'Kode', demo: 'Demo' }, cs: { video: 'Video', code: 'Kód', demo: 'Demo' }, nl: { video: 'Video', code: 'Code', demo: 'Demo' }, bn: { video: 'ভিডিও', code: 'কোড', demo: 'ডেমো' }, ur: { video: 'ویڈیو', code: 'کوڈ', demo: 'ڈیمو' }, }; const labelsFor = (locale: string): TabLabels => { return LABELS[locale] ?? LABELS.en!; }; const parseGithubRepo = (url: string) => { const m = String(url) .trim() .match(/github\.com\/([^/\s]+)\/([^/\s#?]+)/); if (!m) return null; const repo = m[2]!.replace(/\.git$/i, ''); return { owner: m[1]!, repo }; }; const youtubeId = (url: string): string | null => { if (!url) return null; const u = String(url).trim(); let m = u.match(/[?&]v=([^&]+)/); if (m) return m[1]!; m = u.match(/youtu\.be\/([^?]+)/); return m ? m[1]! : null; }; const parseFrontmatterKeys = (fm: string): Record => { const keys: Record = {}; for (const line of fm.split('\n')) { const m = line.match(/^([a-zA-Z0-9_]+):\s*(.*)$/); if (m) keys[m[1]!] = m[2]!.trim(); } return keys; }; const escapeRegex = (s: string) => { return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }; const ideSrcFromRepo = (owner: string, repo: string) => { return `https://ide.intlayer.org/${owner}/${repo}?embed=1&ctl=1&file=intlayer.config.ts`; }; const findTabsBlockForRepo = (body: string, owner: string, repo: string) => { const needle = `ide.intlayer.org/${owner}/${repo}`; let pos = 0; while (pos < body.length) { const tabsStart = body.indexOf('', tabsStart); if (tabsEnd === -1) return null; const end = tabsEnd + ''.length; const block = body.slice(tabsStart, end); if (block.includes(needle)) return { start: tabsStart, end, block }; pos = tabsStart + 5; } return null; }; const buildTabsBlock = ({ labels, videoId, ideSrc, showcaseUrl, codeIframeTitle, demoIframeTitle, }: { labels: TabLabels; videoId: string | null; ideSrc: string; showcaseUrl: string; codeIframeTitle: string; demoIframeTitle: string; }) => { const iframeClass = 'className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"'; const demoIframe = `