@cloudcannon/reader
Advanced tools
Comparing version 0.0.4 to 0.0.5
{ | ||
"name": "@cloudcannon/reader", | ||
"type": "module", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Parses config, files and folder structures to create a JSON file with information about sites made with any static site generator.", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -72,14 +72,3 @@ # Reader | ||
'data-config': { | ||
authors: { | ||
// Reads the contents of this file | ||
path: 'data/authors.csv' | ||
}, | ||
offices: { | ||
// Reads the contents of each file in this directory | ||
path: 'data/offices', | ||
parser: 'json' | ||
} | ||
}, | ||
// Populates the sidebar navigation and provides metadata for the editor | ||
'collections-config': { | ||
@@ -105,8 +94,32 @@ people: { | ||
// The URL function for items in this collection | ||
url: (filePath, frontMatter, filters) => { | ||
const year = new Date(frontMatter.date).getFullYear(); | ||
const slug = filters.slugify(frontMatter.title || ''); | ||
url: (filePath, parsed, { filters }) => { | ||
const year = new Date(parsed.date).getFullYear(); | ||
const slug = filters.slugify(parsed.title || ''); | ||
return `/posts/${year}/${slug}/`; | ||
} | ||
}, | ||
data: { | ||
// Reads the contents of each file in this directory | ||
path: 'data', | ||
// How to parse the files in this collection | ||
parser: (filePath, raw, { parsers, filters }) => { | ||
const parsed = parsers['front-matter'].parse(raw); | ||
const slug = filters.slugify(parsed.title || ''); | ||
return { ...data, slug }; | ||
} | ||
} | ||
}, | ||
// Generates the data for select and multiselect inputs matching these names | ||
'data-config': { | ||
authors: { | ||
// Reads the contents of this file | ||
path: 'data/authors.csv' | ||
}, | ||
offices: { | ||
// Reads the contents of each file in this directory | ||
path: 'data/offices', | ||
parser: 'json' | ||
} | ||
} | ||
@@ -201,3 +214,3 @@ }; | ||
> | ||
> Functions are are supported with `.js` or `.cjs` files. Given file path, front matter and filters as arguments. The return value should be the slash-prefixed URL string. | ||
> Functions are are supported with `.js` or `.cjs` files. Given file path, parsed file content and an object with filters as arguments. The return value should be the slash-prefixed URL string. | ||
> | ||
@@ -260,2 +273,4 @@ > Strings are used as a template to build the URL. There are two types of placeholders available, file and data. Placeholders resulting in empty values are supported. Sequential slashes in URLs are condensed to one. | ||
Functions are are supported with `.js` or `.cjs` files. Given file path, raw file content and an object with parsers and filters as arguments. The return value should be an object representing this file. | ||
`cloudcannon-reader` exits in error if no suitable parser is found. | ||
@@ -262,0 +277,0 @@ |
import { fdir } from 'fdir'; | ||
import { join } from 'path'; | ||
import { buildUrl, filters } from '../util/url-builder.js'; | ||
import { buildUrl } from '../util/url-builder.js'; | ||
import { parseFile } from '../parsers/parser.js'; | ||
@@ -15,8 +15,2 @@ | ||
function getCollectionItemUrl(itemPath, collectionConfig, data) { | ||
return (typeof collectionConfig.url === 'function') | ||
? collectionConfig.url(itemPath, data, filters) | ||
: buildUrl(itemPath, data, collectionConfig.url); | ||
} | ||
async function readCollectionItem(filePath, collectionConfig, key, source) { | ||
@@ -32,3 +26,3 @@ const data = await parseFile(filePath, collectionConfig.parser); | ||
collection: key, | ||
url: getCollectionItemUrl(itemPath, collectionConfig, data) | ||
url: buildUrl(itemPath, data, collectionConfig.url) | ||
}; | ||
@@ -35,0 +29,0 @@ } |
@@ -9,2 +9,3 @@ import { readFile } from 'fs/promises'; | ||
import { parse as parseProperties } from './properties.js'; | ||
import { filters } from '../util/url-builder.js'; | ||
@@ -38,3 +39,3 @@ const defaultParsers = { | ||
if (typeof parser === 'function') { | ||
return parser(raw, filePath); | ||
return parser(filePath, raw, { parsers, filters }); | ||
} | ||
@@ -41,0 +42,0 @@ |
@@ -38,2 +38,6 @@ import { parse } from 'path'; | ||
if (typeof urlTemplate === 'function') { | ||
return urlTemplate(filePath, data, { filters }); | ||
} | ||
const fileTemplated = processFileTemplates(urlTemplate, filePath); | ||
@@ -40,0 +44,0 @@ const templated = processDataTemplates(fileTemplated, data); |
19142
309
273