import { type AccessLevel, canEdit, canManage } from "../lib/access.ts"; import { assetUrl } from "../lib/assets.ts"; import { escapeHtml } from "../lib/html.ts"; import { type Locale, SUPPORTED_LOCALES, t } from "../lib/i18n/index.ts"; import { renderMarkdown } from "../lib/markdown.ts"; import { editNoteUrl, exportNoteUrl, exportUrl, historyNoteUrl, membersUrl, newNoteUrl, noteUrl, profileUrl, settingsUrl, sidebarEditUrl, wikiUrl, } from "../lib/urls.ts"; import { ICONS } from "./icons.ts"; import { dangerSmallButtonClass, fieldFocusClass, kbdClass, menuItemClass, menuItemFocusClass, menuPanelClass, outlineSmallButtonClass, primarySmallButtonClass, sidebarButtonClass, sidebarLinkClass, THEME, type ThemeName, themeColorMeta, themeRootStyle, touchFieldClass, USER_THEMES, type UserTheme, type WikiThemeMode, wikiThemeStyleAttr, } from "./theme/index.ts"; const LOCALE_LABELS: Record = { en: "English", fr: "Français", ja: "日本語", }; export interface LayoutOptions { scripts?: string[]; stylesheets?: string[]; session?: { did: string; handle: string; avatar?: string | null } | null; csrfToken?: string | null; wikiName?: string; wikiSlug?: string; // Handle or DID — whatever belongs in a URL. Omitted rather than "" when unknown. ownerHandle?: string; // AT-URI of the current wiki, for within-wiki search. wikiAtUri?: string; sidebarNotes?: { slug: string; title: string }[]; // Wiki-owner markdown; when set, replaces the auto note list in the sidebar. sidebarMarkdown?: string | null; currentNoteSlug?: string; pageTitle?: string; locale?: Locale; userTheme?: UserTheme; accessLevel?: AccessLevel; bookmarkHtml?: string; shareHtml?: string; ogTitle?: string; ogUrl?: string; ogDescription?: string; ogImage?: string; // AT Tags: the record this URL is the page for. Set only for public wikis. atCanonical?: { uri: string; authorDid: string }; wikiThemeMode?: WikiThemeMode; wikiTheme?: ThemeName; enableSearchShortcut?: boolean; // Set on auth-gated/restricted pages so crawlers skip login/edit/settings/access-denied. noindex?: boolean; } // Defaults so even pages without per-page overrides emit a usable Bluesky/Twitter card. const OG_DEFAULT_DESCRIPTION = "Build a knowledge base with friends. Wikis owned by you, built on ATProto."; function renderOpenGraph( options: LayoutOptions | undefined, title: string, ): string { // Absolute URLs needed; derive the origin from the per-page ogUrl, else emit a relative path. const ogUrl = options?.ogUrl; const origin = ogUrl ? new URL(ogUrl).origin : ""; const ogImage = options?.ogImage ? options.ogImage : `${origin}/public/og-home.png`; const ogTitle = options?.ogTitle ?? options?.wikiName ?? title; const ogDescription = options?.ogDescription ?? OG_DEFAULT_DESCRIPTION; const urlTag = ogUrl ? `` : ""; return ` ${urlTag} `; } // https://tangled.org/chrisshank.com/at-tags/ — the inverse of /at/:did/:collection/:rkey: // tells any atmosphere tool which record this page renders. function renderAtTags(options: LayoutOptions | undefined): string { const record = options?.atCanonical; if (!record) return ""; const uri = escapeHtml(record.uri); return ` `; } function renderSearchButton(locale: Locale = "en"): string { const msg = t(locale); return ``; } function renderWithSidebar(body: string, options: LayoutOptions): string { const locale = options.locale ?? "en"; const msg = t(locale); const notes = [...(options.sidebarNotes ?? [])].sort((a, b) => a.title.localeCompare(b.title), ); const handle = options.ownerHandle ?? ""; const slug = options.wikiSlug ?? ""; const customSidebar = (options.sidebarMarkdown ?? "").trim(); const noteLinks = customSidebar ? `` : ``; const level = options.accessLevel ?? "none"; const editLink = options.currentNoteSlug && canEdit(level) ? `${msg.wiki.edit}` : ""; const newNoteLink = canEdit(level) ? `${msg.wiki.newNote}` : ""; // The button tokens are inline-flex, and Tailwind emits .inline-flex after // .block, so a `block` on the anchor loses regardless of attribute order and // the buttons sit side by side. Flex children are blockified, which stacks // them full-width without fighting the token's display. const noteActions = [editLink, newNoteLink].filter(Boolean); const noteActionsBlock = noteActions.length ? `
${noteActions.join("\n")}
` : ""; const noteExportItem = options.currentNoteSlug ? `${msg.wiki.exportNote}` : ""; const wikiExportItem = `${msg.wiki.exportWiki}`; const historyLink = options.currentNoteSlug ? ` ${ICONS.history} ${msg.wiki.history} ` : ""; const exportDropdown = `
`; const sidebarEditLink = handle && slug ? ` ${ICONS.edit} ${msg.wiki.editSidebar} ` : ""; const membersLink = handle && slug ? ` ${ICONS.user} ${msg.access.members} ` : ""; const adminLinks = canManage(level) ? `
${historyLink} ${membersLink} ${options.shareHtml ?? ""} ${exportDropdown} ${sidebarEditLink} ${ICONS.settings} ${msg.settings.heading}
` : `
${historyLink} ${membersLink} ${options.shareHtml ?? ""} ${exportDropdown}
`; return `
${body}
`; } export function layout( title: string, body: string, options?: LayoutOptions, ): string { const extraScripts = (options?.scripts ?? []) .map((src) => ` `) .join("\n"); const extraStyles = (options?.stylesheets ?? []) .map((href) => ` `) .join("\n"); const locale = options?.locale ?? "en"; const userTheme = options?.userTheme ?? "system"; const msg = t(locale); const session = options?.session; const navBtnClass = `flex items-center justify-center gap-2 h-11 md:h-9 px-2.5 border ${THEME.border} rounded-lg ${THEME.bgSurface} ${THEME.textSecondary} ${THEME.accentSoft} cursor-pointer text-sm`; const dropdownClass = `hidden absolute right-0 mt-1 ${THEME.bgSurface} border ${THEME.border} rounded-lg shadow-lg py-1.5 z-20 w-max`; const dropdownItemClass = `flex items-center gap-2.5 px-4 py-2 text-sm ${THEME.textSecondary} ${THEME.accentSoft} ${menuItemFocusClass} rounded`; const localeItems = SUPPORTED_LOCALES.map( (l) => ``, ).join("\n"); const localePicker = `
${localeItems}
`; const themeItems = USER_THEMES.map( (t) => ``, ).join("\n"); const themePicker = `
${themeItems}
`; // Bluesky's avatar URL is already a CDN ~256px JPEG; the browser scales it to 22px. const avatarBadge = session?.avatar ? `` : ICONS.user; const profileOrLogin = session ? `
${ICONS.user} ${msg.profile.myWikis}
` : `${ICONS.login}`; return ` ${escapeHtml( options?.wikiName ? options.currentNoteSlug ? `${options.wikiName} - ${title}` : options.wikiName : "Lichen", )} ${themeColorMeta(userTheme)} ${options?.noindex ? `` : ""} ${options?.csrfToken ? `` : ""} ${renderOpenGraph(options, title)} ${renderAtTags(options)} ${extraStyles} ${extraScripts} ${escapeHtml(msg.nav.skipToContent)}

${ options?.sidebarNotes ? renderWithSidebar( `${options?.pageTitle ? `

${escapeHtml(options.pageTitle)}

` : ""}${body}`, options, ) : `
${options?.pageTitle ? `

${escapeHtml(options.pageTitle)}

` : ""}${body}
` }
`; }