How to use single quotes in gatsby frontmatter?

Viewed 523

I'd like to use ' or something easy to type from the keyboard instead of ' any ideas on how to do that? I tried to escape, but still doesn't work.

---
title: 'that's great'
---

Also the build doesn't fail if some pages have this bug so basically it misses errors and I published with some pages that are missing now.

1 Answers

That front matter is in YAML, which is a can be a bit confusing in how it escapes characters depending on the style of string that is in use. In this case, we have single quoted strings. According to the docs, in a single quoted string you'd escape a single quote by doubling it.

So your example would be:

---
title: 'that''s great'
---

If you were to instead use a double quoted string, you would use \ to escape any characters in a more traditional way (and single quotes wouldn't need any escaping at all):

---
title: "that's great, but you now need to escape \"double\" quotes, as well as any \\ characters"
---

Bearing in mind you want to just use ' as an apostrophe in your strings, maybe changing to the double quoted style in the second example above would be your best bet. You don't need to modify your strings, and the only characters that need escaping are " and \.

YAML also has a load more ways of denoting strings, including ones that can be more preserve line breaks. You can read more in the documentation for YAML. In particular, look at 4.5. Scalar Styles

Related