@@ -29,29 +29,43 @@ export const yamlToBlogFields = (yamlstr: string): BlogFrontMatterFields => {
29
29
if ( yamlstr . trim ( ) . length === 0 ) {
30
30
return BLANK_BLOG_FRONTMATTER_FIELDS ;
31
31
}
32
- const data = YamlParser . load ( yamlstr ) as Record < string , string > ;
33
-
34
- const author = data ?. author ?? data ?. byline_str ?? "" ; // fallback logic for old format
35
-
36
- // if we make every field a string, then this becomes easy.
37
- // Might be some reflection way to do this? Going to be verbose the first time.
38
- // maybe json.parse could be used more?
39
- const result : BlogFrontMatterFields = {
40
- title : data . title ?? BLANK_BLOG_FRONTMATTER_FIELDS . title ,
41
- date : data . date ?? data . dateline_str ?? BLANK_BLOG_FRONTMATTER_FIELDS . date ,
42
- readtime_minutes : parseInt ( data . readtime_minutes ?? data . readtime_str ?? BLANK_BLOG_FRONTMATTER_FIELDS . date ) ,
43
- author : author . length ? author : BLANK_BLOG_FRONTMATTER_FIELDS . author ,
44
- permalink : data . permalink ?? BLANK_BLOG_FRONTMATTER_FIELDS . permalink ,
45
- tags : [ ] , // todo: fix
46
- basename : data . basename ?? BLANK_BLOG_FRONTMATTER_FIELDS . basename ,
47
-
48
- carousel_title : data . carousel_title ?? BLANK_BLOG_FRONTMATTER_FIELDS . carousel_title ,
49
- carousel_summary : data . carousel_summary ?? BLANK_BLOG_FRONTMATTER_FIELDS . carousel_summary ,
50
- carousel_image : data . carousel_image ?? BLANK_BLOG_FRONTMATTER_FIELDS . carousel_image ,
51
- carousel_image_alt_text : data . carousel_image_alt_text ?? BLANK_BLOG_FRONTMATTER_FIELDS . carousel_image_alt_text ,
52
- carousel_show : ( data . carousel_show === "true" ? "true" : BLANK_BLOG_FRONTMATTER_FIELDS . carousel_show ) ,
53
- } ;
54
- return result ;
32
+ try {
33
+ // sanity check that the yamlstr doesn't include syntax that will throw an error
34
+ const yamlparts = yamlstr . split ( / ^ - - - $ / gm) ; // we actually go MORE text that just the yml?
35
+ if ( yamlparts . length > 2 ) {
36
+ const actual_yamlstr = yamlparts [ 1 ] ;
37
+ if ( actual_yamlstr . trim ( ) . length > 2 ) {
38
+ console . warn ( `yamlstr seems to contain "---" inside of it, this can mess up parser` ) ;
39
+ yamlstr = actual_yamlstr ;
40
+ }
41
+ }
42
+ const data = YamlParser . load ( yamlstr ) as Record < string , string > ;
43
+
44
+ const author = data ?. author ?? data ?. byline_str ?? "" ; // fallback logic for old format
45
+
46
+ // if we make every field a string, then this becomes easy.
47
+ // Might be some reflection way to do this? Going to be verbose the first time.
48
+ // maybe json.parse could be used more?
49
+ const result : BlogFrontMatterFields = {
50
+ title : data . title ?? BLANK_BLOG_FRONTMATTER_FIELDS . title ,
51
+ date : data . date ?? data . dateline_str ?? BLANK_BLOG_FRONTMATTER_FIELDS . date ,
52
+ readtime_minutes : parseInt ( data . readtime_minutes ?? data . readtime_str ?? BLANK_BLOG_FRONTMATTER_FIELDS . date ) ,
53
+ author : author . length ? author : BLANK_BLOG_FRONTMATTER_FIELDS . author ,
54
+ permalink : data . permalink ?? BLANK_BLOG_FRONTMATTER_FIELDS . permalink ,
55
+ tags : [ ] , // todo: fix
56
+ basename : data . basename ?? BLANK_BLOG_FRONTMATTER_FIELDS . basename ,
57
+
58
+ carousel_title : data . carousel_title ?? BLANK_BLOG_FRONTMATTER_FIELDS . carousel_title ,
59
+ carousel_summary : data . carousel_summary ?? BLANK_BLOG_FRONTMATTER_FIELDS . carousel_summary ,
60
+ carousel_image : data . carousel_image ?? BLANK_BLOG_FRONTMATTER_FIELDS . carousel_image ,
61
+ carousel_image_alt_text : data . carousel_image_alt_text ?? BLANK_BLOG_FRONTMATTER_FIELDS . carousel_image_alt_text ,
62
+ carousel_show : ( data . carousel_show === "true" ? "true" : BLANK_BLOG_FRONTMATTER_FIELDS . carousel_show ) ,
63
+ } ;
64
+ return result ;
65
+ } catch ( err ) {
66
+ console . error ( err ) ;
67
+ return BLANK_BLOG_FRONTMATTER_FIELDS ;
68
+ }
55
69
}
56
70
57
71
/** there's a pending change with the website to rename some fields */
0 commit comments