Skip to content

Commit 4b5422f

Browse files
committed
Better yaml parsing
Some weird situation can end up with "---" in the yaml data itself as was causing things to fail. Fix with some cleanup, but need to track down root cause.
1 parent 4014415 commit 4b5422f

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

src/mdxcomponents/frontmatterUtils.ts

+37-23
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,43 @@ export const yamlToBlogFields = (yamlstr: string): BlogFrontMatterFields => {
2929
if (yamlstr.trim().length === 0) {
3030
return BLANK_BLOG_FRONTMATTER_FIELDS;
3131
}
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+
}
5569
}
5670

5771
/** there's a pending change with the website to rename some fields */

src/service-worker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ try {
6363
const get_response = async (request) => {
6464
if (request.destination !== 'image') {
6565
// we don't cache non-images
66-
console.log(`cache: requests not for image "${request.url}"`, request);
66+
// console.log(`cache: requests not for image "${request.url}"`, request);
6767
return fetch(request);
6868
}
6969

0 commit comments

Comments
 (0)