How to handle multiline data properties? eg. Markdown

Been dabbling with fauna for quite some time and I love it more and more! So that is awesome :smiley:

I have a markdown editor that outputs markdown. This output is sent to a Nuxt 3 (check it out it is very cool!) api then sent to fauna. It works best when i stringify on client and parse it back to json on the api to be sent to fauna as it only adds \n on new lines.

I have tested adding the data directly to the document and adding `` like this:

`
```mermaid
flowchart TD 
  Start --> Stop
gantt
title A Gantt Diagram
dateFormat  YYYY-MM-DD
section Section
A task  :a1, 2014-01-01, 30d
Another task  :after a1, 20d
stateDiagram-v2
  s1 --> s2
erDiagram
  CAR ||--o{ NAMED-DRIVER : allows
  PERSON ||--o{ NAMED-DRIVER : is

`

But this only works if i dont have those backticks in the data, as you can see from the formatting of the post... hahaha

But it works with plain text, the data will be stored multiline without any added newlines unicodes.

Do anyone know how i would work around this? For now it works and maybe I should be satisfied with that.

But if anyone know how to add data to fauna using ` `insted of " " for multiline support without the added \n would be great so i know how to handle a situation like that in the future:) 

Thanks!

You can certainly store strings in Fauna documents. The problem that you seem to be having is parsing the string correctly in Javascript.

Using backticks to define strings is no problem, but you cannot simply embed Markdown backticks as is. If you try to embed β€œ```”, the first backtick would terminate the string, the second would start another, and the third would terminate the second string. Anything that followed would be expected to be JavaScript source, not Markdown content.

So, you need to escape the backticks in your Markdown. This is a valid JavaScript string definition that includes Markdown backticks:

var myMarkdownString = `
# Document

\`\`\`mermaid
flowchart TD 
  Start --> Stop
\`\`\`
`

You can then use that string in a query:

client.query(
  q.Create(
    q.Collection('posts'),
    {
      data: {
        title: "Markdown test",
        body: myMarkdownString,
      }
    }
  )
)
.then((result) => console.log(result))
.catch((error) => console.log("Error:" , error))
1 Like

Totally get that. Just wanted to know if there was a way to do it without writing a parser to modify the markdown even though that wouldnt be that hard since it seems only the back ticks are the problem. I just have to settle with the added new lines since they dont interupt the markdown displayed on the frontend.

Thanks for the quick reply! And very nice to hear from Fauna engineer!

The escaping is only required if you are composing a string that contains Markdown backticks. If you acquire the Markdown from storage or from a web request, it should already be a string.

BTW: I’m a documentation engineer. I can’t help very much with database internals, or even driver internals, but I do curate/automate the 4,000+ code examples (across all 7 supported languages) that appear in our documentation, so I’m familiar with most client app development issues.

It is already working as expected, I just noticed that a string with new lines adds \n in when sent to the server. But that is because of JSON.stringify, and as ive learned json dont handle new lines very well. But thats ok. Its removed on JSON.parse anyways.

Documentation or not you are an engineer and a great one! The amazing documentation thought me how to learn Fauna, so you answered more questions than anyone can do in a lifetime.
I am able to compose FQL queries just by knowing what I want the result to be using the Fauna documentations. So thank you Ewan:D

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.