Skip to content

Commit b39e5cc

Browse files
committed
Release v0.0.27
1 parent c7196c4 commit b39e5cc

File tree

12 files changed

+136
-20
lines changed

12 files changed

+136
-20
lines changed

packages/@contentlayer/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/cli",
3-
"version": "0.0.26",
3+
"version": "0.0.27",
44
"type": "module",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

packages/@contentlayer/client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/client",
3-
"version": "0.0.26",
3+
"version": "0.0.27",
44
"type": "module",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

packages/@contentlayer/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/core",
3-
"version": "0.0.26",
3+
"version": "0.0.27",
44
"type": "module",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

packages/@contentlayer/source-contentful/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/source-contentful",
3-
"version": "0.0.26",
3+
"version": "0.0.27",
44
"type": "module",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

packages/@contentlayer/source-files/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/source-files",
3-
"version": "0.0.26",
3+
"version": "0.0.27",
44
"type": "module",
55
"exports": {
66
".": {

packages/@contentlayer/source-files/src/errors/index.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { handleFetchDataErrors } from './aggregate.js'
66

77
export namespace FetchDataError {
88
export type FetchDataError =
9+
| InvalidFrontmatterError
10+
| InvalidMarkdownFileError
11+
| InvalidYamlFileError
12+
| InvalidJsonFileError
913
| ComputedValueError
1014
| UnsupportedFileExtension
1115
| NoSuchDocumentTypeError
@@ -26,6 +30,63 @@ export namespace FetchDataError {
2630

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

33+
export class InvalidFrontmatterError
34+
extends Tagged('InvalidFrontmatterError')<{
35+
readonly error: unknown
36+
readonly documentFilePath: string
37+
}>
38+
implements AggregatableError
39+
{
40+
kind: InvalidDataErrorKind = 'MissingOrIncompatibleData'
41+
42+
renderHeadline: RenderHeadline = ({ documentCount }) =>
43+
`Invalid frontmatter data found for ${documentCount} documents.`
44+
45+
renderLine = () => `"${this.documentFilePath}" failed with ${errorToString(this.error)}`
46+
}
47+
48+
export class InvalidMarkdownFileError
49+
extends Tagged('InvalidMarkdownFileError')<{
50+
readonly error: unknown
51+
readonly documentFilePath: string
52+
}>
53+
implements AggregatableError
54+
{
55+
kind: InvalidDataErrorKind = 'MissingOrIncompatibleData'
56+
57+
renderHeadline: RenderHeadline = ({ documentCount }) => `Invalid markdown in ${documentCount} documents.`
58+
59+
renderLine = () => `"${this.documentFilePath}" failed with ${errorToString(this.error)}`
60+
}
61+
62+
export class InvalidYamlFileError
63+
extends Tagged('InvalidYamlFileError')<{
64+
readonly error: unknown
65+
readonly documentFilePath: string
66+
}>
67+
implements AggregatableError
68+
{
69+
kind: InvalidDataErrorKind = 'MissingOrIncompatibleData'
70+
71+
renderHeadline: RenderHeadline = ({ documentCount }) => `Invalid YAML data in ${documentCount} documents.`
72+
73+
renderLine = () => `"${this.documentFilePath}" failed with ${errorToString(this.error)}`
74+
}
75+
76+
export class InvalidJsonFileError
77+
extends Tagged('InvalidJsonFileError')<{
78+
readonly error: unknown
79+
readonly documentFilePath: string
80+
}>
81+
implements AggregatableError
82+
{
83+
kind: InvalidDataErrorKind = 'MissingOrIncompatibleData'
84+
85+
renderHeadline: RenderHeadline = ({ documentCount }) => `Invalid JSON data in ${documentCount} documents.`
86+
87+
renderLine = () => `"${this.documentFilePath}" failed with ${errorToString(this.error)}`
88+
}
89+
2990
export class ComputedValueError
3091
extends Tagged('ComputedValueError')<{
3192
readonly error: unknown

packages/@contentlayer/source-files/src/fetchData/fetchAllDocuments.ts

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ export const makeCacheItemFromFilePath = ({
110110

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

113-
const {
114-
tuple: [{ documentTypeDef }, warnings],
115-
} = yield* $(
113+
const [{ documentTypeDef }, warnings] = yield* $(
116114
pipe(
117115
validateDocumentData({
118116
rawContent,
@@ -122,6 +120,7 @@ export const makeCacheItemFromFilePath = ({
122120
options,
123121
}),
124122
These.toEffect,
123+
T.map((_) => _.tuple),
125124
),
126125
)
127126

@@ -169,7 +168,13 @@ const processRawContent = ({
169168
relativeFilePath: string
170169
}): T.Effect<
171170
OT.HasTracer,
172-
FetchDataError.UnsupportedFileExtension | fs.FileNotFoundError | fs.ReadFileError,
171+
| FetchDataError.UnsupportedFileExtension
172+
| FetchDataError.InvalidFrontmatterError
173+
| FetchDataError.InvalidMarkdownFileError
174+
| FetchDataError.InvalidJsonFileError
175+
| FetchDataError.InvalidYamlFileError
176+
| fs.FileNotFoundError
177+
| fs.ReadFileError,
173178
RawContent
174179
> =>
175180
pipe(
@@ -179,18 +184,22 @@ const processRawContent = ({
179184

180185
switch (filePathExtension) {
181186
case 'md': {
182-
const markdown = matter(fileContent)
187+
const markdown = yield* $(parseMarkdown({ markdownString: fileContent, documentFilePath: relativeFilePath }))
183188
return { kind: 'markdown' as const, fields: markdown.data, body: markdown.content }
184189
}
185190
case 'mdx': {
186-
const markdown = matter(fileContent)
191+
const markdown = yield* $(parseMarkdown({ markdownString: fileContent, documentFilePath: relativeFilePath }))
187192
return { kind: 'mdx' as const, fields: markdown.data, body: markdown.content }
188193
}
189-
case 'json':
190-
return { kind: 'json' as const, fields: JSON.parse(fileContent) }
194+
case 'json': {
195+
const fields = yield* $(parseJson({ jsonString: fileContent, documentFilePath: relativeFilePath }))
196+
return { kind: 'json' as const, fields }
197+
}
191198
case 'yaml':
192-
case 'yml':
193-
return { kind: 'yaml' as const, fields: yaml.parse(fileContent) }
199+
case 'yml': {
200+
const fields = yield* $(parseYaml({ yamlString: fileContent, documentFilePath: relativeFilePath }))
201+
return { kind: 'yaml' as const, fields }
202+
}
194203
default:
195204
return yield* $(
196205
T.fail(
@@ -239,3 +248,49 @@ const getAllRelativeFilePaths = ({
239248
(error) => new fs.UnknownFSError({ error }),
240249
)
241250
}
251+
252+
const parseMarkdown = ({
253+
markdownString,
254+
documentFilePath,
255+
}: {
256+
markdownString: string
257+
documentFilePath: string
258+
}): T.Effect<
259+
unknown,
260+
FetchDataError.InvalidMarkdownFileError | FetchDataError.InvalidFrontmatterError,
261+
matter.GrayMatterFile<string>
262+
> =>
263+
T.tryCatch(
264+
() => matter(markdownString),
265+
(error: any) => {
266+
if (error.name === 'YAMLException') {
267+
return new FetchDataError.InvalidFrontmatterError({ error, documentFilePath })
268+
} else {
269+
return new FetchDataError.InvalidMarkdownFileError({ error, documentFilePath })
270+
}
271+
},
272+
)
273+
274+
const parseJson = ({
275+
jsonString,
276+
documentFilePath,
277+
}: {
278+
jsonString: string
279+
documentFilePath: string
280+
}): T.Effect<unknown, FetchDataError.InvalidJsonFileError, Record<string, any>> =>
281+
T.tryCatch(
282+
() => JSON.parse(jsonString),
283+
(error) => new FetchDataError.InvalidJsonFileError({ error, documentFilePath }),
284+
)
285+
286+
const parseYaml = ({
287+
yamlString,
288+
documentFilePath,
289+
}: {
290+
yamlString: string
291+
documentFilePath: string
292+
}): T.Effect<unknown, FetchDataError.InvalidYamlFileError, Record<string, any>> =>
293+
T.tryCatch(
294+
() => yaml.parse(yamlString),
295+
(error) => new FetchDataError.InvalidYamlFileError({ error, documentFilePath }),
296+
)

packages/@contentlayer/source-sanity/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/source-sanity",
3-
"version": "0.0.26",
3+
"version": "0.0.27",
44
"type": "module",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

packages/@contentlayer/utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/utils",
3-
"version": "0.0.26",
3+
"version": "0.0.27",
44
"type": "module",
55
"exports": {
66
"./package.json": {

packages/contentlayer-stackbit-yaml-generator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "contentlayer-stackbit-yaml-generator",
3-
"version": "0.0.26",
3+
"version": "0.0.27",
44
"type": "module",
55
"bin": "./dist/cli/index.js",
66
"exports": "./dist/lib/index.js",

0 commit comments

Comments
 (0)