@@ -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+ )
0 commit comments