import { execFileSync } from 'node:child_process';
import { existsSync, readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { defineConfig } from 'vitepress';
// The API reference sidebar, written by site/tools/reference.php (npm run docs:reference).
const referenceFile = new URL('../.generated/reference.json', import.meta.url);
const reference = existsSync(referenceFile) ? JSON.parse(readFileSync(referenceFile, 'utf8')) : [];
// Guides include the repository's Markdown files (THEMING.md, ACCESSIBILITY.md?), whose
// links are relative to the repository root. Root documents are linked to their page on
// this site, other paths (theme/presets/, tests/?) to GitHub.
const REPOSITORY = 'https://github.com/kivudesign/caelrik';
const ROOT_PAGES = {
'THEMING.md': '/guides/theming',
'ACCESSIBILITY.md': '/guides/accessibility',
'SECURITY.md': '/guides/security',
'TESTING.md': '/guides/testing',
'ROADMAP.md': '/guides/roadmap',
};
function repositoryLinks(md) {
md.core.ruler.push('caelrik-repository-links', (state) => {
for (const block of state.tokens) {
for (const token of block.children ?? []) {
const href = token.type === 'link_open' ? token.attrGet('href') : null;
// Absolute URLs, anchors, site paths and ./ or ../ links are the site's own.
if (!href || /^([a-z]+:|#|\/|\.)/i.test(href)) {
continue;
}
const [path, hash] = href.split('#');
const page = ROOT_PAGES[path];
token.attrSet('href', page ? `${page}${hash ? `#${hash}` : ''}` : `${REPOSITORY}/blob/main/${href}`);
}
}
});
}
// A guide that includes a repository file (<!--@include: ../../THEMING.md-->) was last
// updated when that file was: VitePress only looks at the guide's own git history.
const siteDir = fileURLToPath(new URL('..', import.meta.url));
function lastCommitTime(file) {
try {
const seconds = execFileSync('git', ['log', '-1', '--format=%ct', '--', file], { encoding: 'utf8' }).trim();
return seconds ? Number(seconds) * 1000 : 0;
} catch {
return 0;
}
}
function includedLastUpdated(pageData) {
const page = join(siteDir, pageData.relativePath);
const includes = [...readFileSync(page, 'utf8').matchAll(/<!--@include:\s*([^\s>]+?)\s*-->/g)]
.map(([, path]) => join(dirname(page), path));
if (includes.length > 0) {
pageData.lastUpdated = Math.max(pageData.lastUpdated ?? 0, ...includes.map(lastCommitTime));
}
}
// Documentation site: npm run docs:dev, npm run docs:build (output: site/.vitepress/dist).
// DOCS_BASE is the path the site is served from: /caelrik/ on GitHub Pages.
export default defineConfig({
title: 'Caelrik UI',
description: 'Accessible, Bootstrap-based PHP UI components for plain PHP, Laravel and Symfony.',
lang: 'en',
base: process.env.DOCS_BASE ?? '/caelrik/',
cleanUrls: true,
lastUpdated: true,
transformPageData: includedLastUpdated,
srcExclude: ['**/README.md'],
markdown: {
config: repositoryLinks,
// High-contrast highlighting: every token at 4.5:1 or more on the code background.
theme: { light: 'github-light-high-contrast', dark: 'github-dark-high-contrast' },
},
head: [
['link', { rel: 'icon', type: 'image/svg+xml', href: `${process.env.DOCS_BASE ?? '/caelrik/'}logo.svg` }],
['meta', { name: 'theme-color', content: '#0d6efd' }],
],
themeConfig: {
logo: '/logo.svg',
nav: [
{ text: 'Guide', link: '/guide/getting-started', activeMatch: '/guide/' },
{ text: 'Components', link: '/components/', activeMatch: '/components/' },
{ text: 'Guides', link: '/guides/theming', activeMatch: '/guides/' },
{ text: 'Reference', link: '/reference/', activeMatch: '/reference/' },
{ text: 'GitHub', link: 'https://github.com/kivudesign/caelrik' },
],
sidebar: {
'/reference/': [{ text: 'API reference', link: '/reference/' }, ...reference],
'/guides/': [
{
text: 'Guides',
items: [
{ text: 'Theming', link: '/guides/theming' },
{ text: 'Security', link: '/guides/security' },
{ text: 'Accessibility', link: '/guides/accessibility' },
{ text: 'Testing', link: '/guides/testing' },
{ text: 'Testing an integration', link: '/guides/integration-testing' },
{ text: 'Roadmap', link: '/guides/roadmap' },
{ text: 'Changelog', link: '/guides/changelog' },
],
},
],
'/components/': [
{
text: 'Components',
items: [
{ text: 'Overview', link: '/components/' },
{ text: 'Grid', link: '/components/grid' },
{ text: 'List view', link: '/components/listview' },
{ text: 'Tree view', link: '/components/treeview' },
{ text: 'Tree list', link: '/components/treelist' },
{ text: 'Task board', link: '/components/taskboard' },
{ text: 'Actions', link: '/components/actions' },
{ text: 'Chart', link: '/components/chart' },
{ text: 'Form', link: '/components/form' },
{ text: 'Rich text editor', link: '/components/editor' },
{ text: 'File upload', link: '/components/upload' },
{ text: 'Scheduler', link: '/components/scheduler' },
{ text: 'Admin layout', link: '/components/admin-layout' },
{ text: 'Menus and toolbar', link: '/components/menus' },
{ text: 'Accordion, card, splitter', link: '/components/containers' },
{ text: 'Tile layout', link: '/components/tilelayout' },
{ text: 'Record view', link: '/components/recordview' },
{ text: 'Stats and dashboards', link: '/components/stats' },
{ text: 'Badges, chips, tooltips, progress', link: '/components/display' },
{ text: 'Timeline', link: '/components/timeline' },
{ text: 'Carousel', link: '/components/carousel' },
{ text: 'Gauges and sparklines', link: '/components/gauge' },
{ text: 'Gantt chart', link: '/components/gantt' },
{ text: 'Map', link: '/components/map' },
{ text: 'Org chart', link: '/components/orgchart' },
{ text: 'Diagram', link: '/components/diagram' },
{ text: 'QR code and barcode', link: '/components/qrcode' },
{ text: 'PDF viewer', link: '/components/pdfviewer' },
{ text: 'Media player', link: '/components/mediaplayer' },
{ text: 'Modal', link: '/components/modal' },
{ text: 'Tabs', link: '/components/tabs' },
{ text: 'Notification', link: '/components/notification' },
{ text: 'File manager', link: '/components/file-manager' },
{ text: 'Theme builder', link: '/components/theme-builder' },
],
},
],
'/guide/': [
{
text: 'Introduction',
items: [
{ text: 'Getting started', link: '/guide/getting-started' },
{ text: 'How it works', link: '/guide/how-it-works' },
{ text: 'Data endpoints', link: '/guide/data' },
],
},
{
text: 'Setup',
items: [
{ text: 'Plain PHP', link: '/guide/plain-php' },
{ text: 'Laravel', link: '/guide/laravel' },
{ text: 'Symfony', link: '/guide/symfony' },
{ text: 'Content Security Policy', link: '/guide/csp' },
],
},
],
},
search: { provider: 'local' },
outline: { level: [2, 3] },
editLink: {
// Guides that include a repository file are edited there (THEMING.md?). The function
// runs in the browser, so it cannot use this module's constants.
pattern: ({ filePath }) => {
const included = {
'guides/theming.md': 'THEMING.md',
'guides/accessibility.md': 'ACCESSIBILITY.md',
'guides/security.md': 'SECURITY.md',
'guides/testing.md': 'TESTING.md',
'guides/roadmap.md': 'ROADMAP.md',
}[filePath];
return `https://github.com/kivudesign/caelrik/edit/main/${included ?? `site/${filePath}`}`;
},
text: 'Edit this page on GitHub',
},
socialLinks: [{ icon: 'github', link: 'https://github.com/kivudesign/caelrik' }],
footer: {
message: 'Released under the MIT License.',
copyright: 'Caelrik UI contributors',
},
},
});
|