Reputation: 67
I am working on a Next.js blog and trying to use react-markdown to read Markdown data, but I found that I cant store Markdown in JSON files.
./data/hello.json
{
"thumbnail" : "https://dummyimage.com/722x402",
"slug" : "hello",
"title" : "This is hello",
"description" : "This is description",
"category" : "hello",
"content" : "
#Hello
**How are you**
",
}
Upvotes: 2
Views: 5584
Reputation: 13933
I'm late to the thread here, but to make any string JSON storable (this includes Markdown) you can use the built-in JSON library in Javascript. If you're doing this at runtime in a web application, you can simply call JSON.stringify(SomeMarkdownText)
. This will solve all the problems noted above including automatically escaping things like newlines, backslashes, quotes, etc.
If you need to do this simply to get the string value you can also just run this in the debug console in a web browser, a nodejs environment running locally, etc etc.
Upvotes: 1
Reputation: 943586
Literal new lines are not allowed in the middle of JSON strings. Use \n
to represent them instead.
Upvotes: 5