Activity API: Create line breaks in comment body

Hi !

In the web UI’s activity feed, we can enter comments containing line returns.

How could we also use line returns in the body of a POST request ?

url = f"{server}/api/projects/{project_name}/task/{task_ayon_id}/activities"
headers = {"Authorization": f"apiKey {api_key}"}

activities = {
    "activityType": "comment", 
    "body": r"my comment, \n on two lines"
}
print("Checking note content:", activities)

# Push the comment to Ayon.
response = requests.post(url, headers=headers, json=activities)

I tried and failed with those :

\n
\r\n
<br/>

I have tested lots of permutations, to get a line break in a comment, that shows up correctly in the UI.

Does not work :

\n
\r
\r \n
\\r \\n
\\n
\\r
\\n \\n

Success : double line break :

\n \r
\r \r

Success : single line break :

\\\n
\\\r

This actually has nothing to do with the server. The activity comment body field is just a string that can have markdown in it. It’s up to the frontend client to decide how the markdown is rendered.

I just created a new comment with multiple lines in it.

body: "Comment on lines\n\nLine 1\n\nLine 2\n\nLine 3"

Usually however in markdown a single new-line still makes it appear on a new line, right?

So the fact that the web editor is adding two new lines may just be a side-effects but it doesn’t seem like ‘regular markdown’?

Although this StackOverflow question does confuse me about what is right or wrong :slight_smile: which hint to e.g. adding two trailing spaces on the first lines, etc. but that feels even worse. Maybe I’m just used to writing markdown in editors that automatically ‘duplicate’ the new lines.

Maybe it depends on the method you use to post the comment (I use ayon_api.post).
For me, \n or \\n does not work, I need to use \\\n or \n\n

Fun thing : if I add a bullet point, using a - at the beginning of a line, I get a visible backslash in the UI. So I remove one of the 3 backslashes, and now the line break does not work anymore :grin: (but it still works with \n\n)

import ayon_api
project_name = "UNREAL_POC_PRODUCTS"
entity_type = "task"
entity_id = "2d19d850751b11ef9c4a4d40db747a47"
payload = {
    "activityType": "comment",
    "body": "First line. In *italic* or in **bold**.\\\n- Second line.\\\n- Third line.",
}
result = ayon_api.post(f"projects/{project_name}/{entity_type}/{entity_id}/activities", **payload)

From the above, if I remove the - strings, I get 3 correct lines in the UI (thanks to a triple backslash on each linebreak).

Also note, when using ayon_api.post :
\n\n displays a double linebreak in the UI (same as manually typing in the comment UI)
\\\n displays a single linebreak in the UI (only works by script, not manually in UI)

Now, let’s see what happens when I don’t use any script.
I just manually enter text in the UI :

Do be aware that you’re creating a string there where visually it has three backslashes - but in the resulting string there would be fewer in Python, because you’re also dealing with Python’s escaping.

For example:

print("hello\\\nworld")

Prints:

hello\
world

Whereas passing it the raw string directly:

print(r"hello\\\nworld")

Prints:

hello\\\nworld

Which is something you’d likely have to deal with less if you were to read the string from a text or json file to begin with.

I am using python escaping, on purpose.
In my case it’s a lot simpler to just create the string without reading external files, as I just want to build texts depending on variables and context (as opposed to a long and fixed text).

I don’t use raw string here, as it treats backslashes as standard characters (as you detailed), so it would never be interpreted by the UI as linebreaks.

I like the \\\n solution, as it allows to have a single linebreak in Ayon’s UI.
It takes less space in the UI, than texts that you manually enter in Ayon’s text editor (which for some reason always result in having 2 line breaks, or maybe it’s the equivalent of a paragraph cut).