import Nast from 'notajs-types/nast' import Notion from 'notajs-types/notion' import { COLLECTION_VIEW_TYPES, COLLECTION_ITEM_PROPERTY_TYPES, CSS } from '../constants' import { raiseWarning } from '../log-utils' import { escapeString, renderTitle } from '../render-utils' function renderCollection( node: Nast.Collection ): string { let viewMeta = node.views.find(view => view.id === node.defaultViewId) if (typeof viewMeta === 'undefined') { raiseWarning(`Cannot find view of ${node.defaultViewId}`) return '' } let pages = node.blocks let sortQueries = viewMeta.query.sort if (sortQueries != null) { let sortFunc = factorySortFunc(sortQueries) pages = pages.sort(sortFunc) } switch (viewMeta.type) { case COLLECTION_VIEW_TYPES.table: return renderTable(node, pages, viewMeta.format) case COLLECTION_VIEW_TYPES.gallery: return renderGallery(node, pages, viewMeta.format) default: raiseWarning(`No render function for collection view "${viewMeta.type}".`) return '' } } function renderTable( node: Nast.Collection, pages: Nast.Page[], viewFormat: Notion.CollectionViewFormat ): string { const title = escapeString(node.name) // const wrap = (typeof viewFormat.table_wrap === 'undefined') // ? viewFormat.table_wrap : true const viewSchema = (viewFormat.table_properties || []) .filter(colViewInfo => colViewInfo.visible) /** * Some collection views have "ghost" properties that don't exist * in collection schema. */ .filter(colViewInfo => node.schema[colViewInfo.property]) .map(colViewInfo => { const colId = colViewInfo.property return { id: colId, width: colViewInfo.width, name: node.schema[colId].name, type: node.schema[colId].type, options: node.schema[colId].options } }); const theadHTML = viewSchema .map(col => `