element-book
Version:
An [`element-vir`](https://npmjs.com/package/element-vir) drop-in element for building, testing, and demonstrating a collection of elements (or, in other words, a design system).
32 lines (31 loc) • 1.3 kB
JavaScript
import { check } from '@augment-vir/assert';
import { BookEntryType } from './book-entry-type.js';
import { checkControls } from './book-page/book-page-controls.js';
import { titleToUrlBreadcrumb } from './url-breadcrumbs.js';
/** Characters that are not allowed in book entry titles because they would break URL routing. */
const invalidTitleCharacters = /[/?#&=]/;
export function getPageTitleError(title) {
const invalidMatch = title.match(invalidTitleCharacters);
return title.trim()
? titleToUrlBreadcrumb(title)
? invalidMatch
? new Error(`Book page title has invalid character '${invalidMatch[0]}'.`)
: undefined
: new Error(`Book page title resolved to empty breadcrumb.`)
: new Error(`Cannot define an element-book page with an empty title.`);
}
export const bookEntryVerifiers = {
[BookEntryType.ElementExample]: () => {
/** Currently all element example checking happens on page definition. */
return [];
},
[BookEntryType.Page]: (bookPage) => {
return [
getPageTitleError(bookPage.title),
...checkControls(bookPage.controls, bookPage.title),
].filter(check.isTruthy);
},
[BookEntryType.Root]: () => {
return [];
},
};