Skip to content

Commit

Permalink
Release v0.0.27
Browse files Browse the repository at this point in the history
  • Loading branch information
schickling committed Oct 8, 2021
1 parent c7196c4 commit b39e5cc
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/@contentlayer/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentlayer/cli",
"version": "0.0.26",
"version": "0.0.27",
"type": "module",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/@contentlayer/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentlayer/client",
"version": "0.0.26",
"version": "0.0.27",
"type": "module",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/@contentlayer/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentlayer/core",
"version": "0.0.26",
"version": "0.0.27",
"type": "module",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/@contentlayer/source-contentful/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentlayer/source-contentful",
"version": "0.0.26",
"version": "0.0.27",
"type": "module",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/@contentlayer/source-files/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentlayer/source-files",
"version": "0.0.26",
"version": "0.0.27",
"type": "module",
"exports": {
".": {
Expand Down
61 changes: 61 additions & 0 deletions packages/@contentlayer/source-files/src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { handleFetchDataErrors } from './aggregate.js'

export namespace FetchDataError {
export type FetchDataError =
| InvalidFrontmatterError
| InvalidMarkdownFileError
| InvalidYamlFileError
| InvalidJsonFileError
| ComputedValueError
| UnsupportedFileExtension
| NoSuchDocumentTypeError
Expand All @@ -26,6 +30,63 @@ export namespace FetchDataError {

type InvalidDataErrorKind = 'UnknownDocument' | 'ExtraFieldData' | 'MissingOrIncompatibleData' | 'Unexpected'

export class InvalidFrontmatterError
extends Tagged('InvalidFrontmatterError')<{
readonly error: unknown
readonly documentFilePath: string
}>
implements AggregatableError
{
kind: InvalidDataErrorKind = 'MissingOrIncompatibleData'

renderHeadline: RenderHeadline = ({ documentCount }) =>
`Invalid frontmatter data found for ${documentCount} documents.`

renderLine = () => `"${this.documentFilePath}" failed with ${errorToString(this.error)}`
}

export class InvalidMarkdownFileError
extends Tagged('InvalidMarkdownFileError')<{
readonly error: unknown
readonly documentFilePath: string
}>
implements AggregatableError
{
kind: InvalidDataErrorKind = 'MissingOrIncompatibleData'

renderHeadline: RenderHeadline = ({ documentCount }) => `Invalid markdown in ${documentCount} documents.`

renderLine = () => `"${this.documentFilePath}" failed with ${errorToString(this.error)}`
}

export class InvalidYamlFileError
extends Tagged('InvalidYamlFileError')<{
readonly error: unknown
readonly documentFilePath: string
}>
implements AggregatableError
{
kind: InvalidDataErrorKind = 'MissingOrIncompatibleData'

renderHeadline: RenderHeadline = ({ documentCount }) => `Invalid YAML data in ${documentCount} documents.`

renderLine = () => `"${this.documentFilePath}" failed with ${errorToString(this.error)}`
}

export class InvalidJsonFileError
extends Tagged('InvalidJsonFileError')<{
readonly error: unknown
readonly documentFilePath: string
}>
implements AggregatableError
{
kind: InvalidDataErrorKind = 'MissingOrIncompatibleData'

renderHeadline: RenderHeadline = ({ documentCount }) => `Invalid JSON data in ${documentCount} documents.`

renderLine = () => `"${this.documentFilePath}" failed with ${errorToString(this.error)}`
}

export class ComputedValueError
extends Tagged('ComputedValueError')<{
readonly error: unknown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ export const makeCacheItemFromFilePath = ({

const rawContent = yield* $(processRawContent({ fullFilePath, relativeFilePath }))

const {
tuple: [{ documentTypeDef }, warnings],
} = yield* $(
const [{ documentTypeDef }, warnings] = yield* $(
pipe(
validateDocumentData({
rawContent,
Expand All @@ -122,6 +120,7 @@ export const makeCacheItemFromFilePath = ({
options,
}),
These.toEffect,
T.map((_) => _.tuple),
),
)

Expand Down Expand Up @@ -169,7 +168,13 @@ const processRawContent = ({
relativeFilePath: string
}): T.Effect<
OT.HasTracer,
FetchDataError.UnsupportedFileExtension | fs.FileNotFoundError | fs.ReadFileError,
| FetchDataError.UnsupportedFileExtension
| FetchDataError.InvalidFrontmatterError
| FetchDataError.InvalidMarkdownFileError
| FetchDataError.InvalidJsonFileError
| FetchDataError.InvalidYamlFileError
| fs.FileNotFoundError
| fs.ReadFileError,
RawContent
> =>
pipe(
Expand All @@ -179,18 +184,22 @@ const processRawContent = ({

switch (filePathExtension) {
case 'md': {
const markdown = matter(fileContent)
const markdown = yield* $(parseMarkdown({ markdownString: fileContent, documentFilePath: relativeFilePath }))
return { kind: 'markdown' as const, fields: markdown.data, body: markdown.content }
}
case 'mdx': {
const markdown = matter(fileContent)
const markdown = yield* $(parseMarkdown({ markdownString: fileContent, documentFilePath: relativeFilePath }))
return { kind: 'mdx' as const, fields: markdown.data, body: markdown.content }
}
case 'json':
return { kind: 'json' as const, fields: JSON.parse(fileContent) }
case 'json': {
const fields = yield* $(parseJson({ jsonString: fileContent, documentFilePath: relativeFilePath }))
return { kind: 'json' as const, fields }
}
case 'yaml':
case 'yml':
return { kind: 'yaml' as const, fields: yaml.parse(fileContent) }
case 'yml': {
const fields = yield* $(parseYaml({ yamlString: fileContent, documentFilePath: relativeFilePath }))
return { kind: 'yaml' as const, fields }
}
default:
return yield* $(
T.fail(
Expand Down Expand Up @@ -239,3 +248,49 @@ const getAllRelativeFilePaths = ({
(error) => new fs.UnknownFSError({ error }),
)
}

const parseMarkdown = ({
markdownString,
documentFilePath,
}: {
markdownString: string
documentFilePath: string
}): T.Effect<
unknown,
FetchDataError.InvalidMarkdownFileError | FetchDataError.InvalidFrontmatterError,
matter.GrayMatterFile<string>
> =>
T.tryCatch(
() => matter(markdownString),
(error: any) => {
if (error.name === 'YAMLException') {
return new FetchDataError.InvalidFrontmatterError({ error, documentFilePath })
} else {
return new FetchDataError.InvalidMarkdownFileError({ error, documentFilePath })
}
},
)

const parseJson = ({
jsonString,
documentFilePath,
}: {
jsonString: string
documentFilePath: string
}): T.Effect<unknown, FetchDataError.InvalidJsonFileError, Record<string, any>> =>
T.tryCatch(
() => JSON.parse(jsonString),
(error) => new FetchDataError.InvalidJsonFileError({ error, documentFilePath }),
)

const parseYaml = ({
yamlString,
documentFilePath,
}: {
yamlString: string
documentFilePath: string
}): T.Effect<unknown, FetchDataError.InvalidYamlFileError, Record<string, any>> =>
T.tryCatch(
() => yaml.parse(yamlString),
(error) => new FetchDataError.InvalidYamlFileError({ error, documentFilePath }),
)
2 changes: 1 addition & 1 deletion packages/@contentlayer/source-sanity/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentlayer/source-sanity",
"version": "0.0.26",
"version": "0.0.27",
"type": "module",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/@contentlayer/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentlayer/utils",
"version": "0.0.26",
"version": "0.0.27",
"type": "module",
"exports": {
"./package.json": {
Expand Down
2 changes: 1 addition & 1 deletion packages/contentlayer-stackbit-yaml-generator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "contentlayer-stackbit-yaml-generator",
"version": "0.0.26",
"version": "0.0.27",
"type": "module",
"bin": "./dist/cli/index.js",
"exports": "./dist/lib/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/contentlayer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "contentlayer",
"version": "0.0.26",
"version": "0.0.27",
"bin": "./bin/cli.cjs",
"type": "module",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion packages/next-contentlayer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-contentlayer",
"version": "0.0.26",
"version": "0.0.27",
"type": "module",
"main": "./dist/cjs/index.cjs",
"exports": {
Expand Down

0 comments on commit b39e5cc

Please sign in to comment.