Mandrill ignores line breaks (\n) - mandrill

Using the send-template method and the Lutung Mandrill Java API implementation, when a template variable contains line breaks of the form \n, they are totally ignored or stripped on generated emails. The Strings containing \n characters are kept untouched untill the GET send-template method is executed. So I guess there is some String analysis and conversion on Mandrill servers which removes special characters.
How can I make work again line breaks on Mandrill emails?

In my case I am using Handlebars for mandrill templates and I have solved this by sending HTML to the template <br/> instead of \n by replacing \n in my string with <br/> something like: .Description.Replace("\n", "<br/>");
And then on the mandrill template I put the variable inside {{{ variable }}} instead of {{ variable }}
http://blog.mandrill.com/handlebars-for-templates-and-dynamic-content.html
HTML escaping
Handlebars HTML-escapes values returned by an
{{expression}}. If you don't want Handlebars to escape a value, use
the "triple-stash", "{{{.
In your template:
<div> {{{html_tag_content}}} </div> In your API request:
"global_merge_vars": [ {
"name": "html_tag_content",
"content": "This example<br>is all about<br>the magical world of handlebars" } ]
The result:
This example
is all about
the magical world of handlebars

If you want to send more complex content, be it html, or variables with break lines and whatnot, you can first render the template and then send the message, instead of directly using send-template.
Render the template with a call to templates.render:
{
"key": "example key",
"template_name": "Example Template",
"template_content": [
{
"name": "editable",
"content": "<div>content to inject *|MERGE1|*</div>"
}
],
"merge_vars": [
{
"name": "merge1",
"content": "merge1 content\n contains line breaks"
}
]
}
Save the result to a variable rendered_content.
Send the message using the rendered template with messages.send:
{
"message": {
"html": rendered_content,
...
...
}

Related

Graph API Unable to read JSON request payload

In power automate using Invoke an HTTP request, I am unable to make a graph API call for email sent(Post: https://graph.microsoft.com/v1.0/me/sendMail) with a customized email body (HTML elements and tags). It does not allow the below custom HTML code and errors as "Unable to read JSON request payload. Please ensure the Content-Type header is set and payload is of valid JSON format."  Please find the code below. In addition, HTML code is
Please find the code below.
And HTML Code is
This error occurs because your payload is not in a valid json format. You have to escape the quotation marks. Use \" intead of ".
{
"data": "<html><head>The title attribute example</head><body><h3 title=\"Hello HTML!\">Titled head tag example</h3></body></html>"
}
Like #Optimal already mentioned. It is good to escape the quotation marks of the HTML Body value.
However, I see the request body also has a saveToSentItems boolean property. Can you try it with that boolean as well?
Try something like below:
{
"message": {
"subject": "#{triggerBody()['text']}",
"body": {
"contentType": "HTML",
"content": "#{triggerBody()['text_1']}"
},
"toRecipients": [
{
"emailAddress": {
"address": "#{triggerBody()['text_2']}"
}
}
]
},
"saveToSentItems": "true"
}

Remark: How to parse HTML tags and their content in MDAST

I'm trying to parse a GitHub-flavoured markdown file using Unified and Remark-Parse to generate a MDAST. I'm able to parse most of it correctly and easily, however I'm having trouble parsing the HTML tags and their content from the AST.
In the AST, HTML tags and their contents are represented as siblings, not parent-child. For example <sub>hi</sub> is parsed into
[
{
"type": "paragraph",
"children": [
{
"type": "html",
"value": "<sub>",
},
{
"type": "text",
"value": "hi",
},
{
"type": "html",
"value": "</sub>",
}
]
}
]
Ideally, I would want it to be parsed like
[
{
"type": "paragraph",
"children": [
{
"type": "html",
"value": "sub",
"children": [
{
"type": "text",
"value": "hi",
},
]
},
]
}
]
so that I can access the tag type and its content. (Specifically, my goal is to just skip over the tags and their content as they are not needed for my purposes)
This is the configuration I am using currently:
import unified from 'unified';
import markdown from 'remark-parse';
import type {Block} from '#notionhq/client/build/src/api-types';
import {parseRoot} from './internal';
import gfm from 'remark-gfm';
export function parseBody(body: string): Block[] {
const tokens = unified().use(markdown).use(gfm).parse(body);
return parseRoot(tokens);
}
So, my question is: Is there a way of configuring Remark to do so / is there a Remark plugin to do this? If not, how would I go about creating a plugin that does so?
Thanks.
first: why the AST looks as it does and why Remark most likely does not have an option to do it differently
The reason that the AST represents it that way is because that is what the CommonMark specification specifies for raw inline HTML and for HTML blocks. Specifically, CommonMark specifies that HTML tags are passed through, not parsed.
For inline HTML, the spec supports inline HTML tags, which is not the same as supporting inline HTML. Tags are simply passed through as-is. There is no matching of opening and closing tags. The reasons for this are:
performance
parser complexity
HTML tags are only supported as a "use at your own risk" "last resort" option when Markdown doesn't have a feature you need.
For a small number of HTML tags, open and close tag matching is supported at the block-level. pre, script, style, and textarea, the latter only added recently in v0.30 of the spec.
You can read the above linked parts of the spec, and search the discussions in the CommonMark forum to get more understanding of the whys, but to get right to the point, read:
This explanation within the spec for the choices made.
Skip to [the Raw HTML section of this forum]( the https://talk.commonmark.org/t/beyond-markdown/2787?u=vas) post by the CommonMark spec author and maintainer, John MacFarlane (#jgm).
This forum question and also this one and #jgm's answers.
second: what you can do about it
Remark is "part of the unified collective", which is an infrastructure centered around the processing of AST (abstract syntax trees). From your question, it sounds like you already get this.
There is lot's of help on unified's pages for how to write plugins:
https://github.com/unifiedjs/unified
https://unifiedjs.com/learn/guide/create-a-plugin/
But the best way to both learn how to do this and to get a quick jump on an implementation is to look at the many existing mdast-specific manipulators.

Rails receive params as string

I'm building an API with Ruby on Rails. Because of that, I can't force the params to follow sone restriction.
Currently, I'm having an error with params that contained a Windows newline, because the newline isn't escaped.
The request body looks like below:
{
"name": "Jon Do",
"address": "6th Street\r\nDistrict City\r\nNation"
}
And it generated JSON::ParserError: 784: unexpected token
How to correctly process that request so params[:address] can be called without error?
When passing the data from front-end, make it JSON.stringify, and parse from back-end.
#Front-end
address: JSON.stringify(objects)
#Back-end
JSON.parse(params[:address])

How to format code blocks in descriptions in Swagger UI 3.x?

I would like to put a Markdown code block in the description of my API but the Swagger UI seems to be reading as though it was a single line code snippet. I currently have:
description: |
This API was created to allow interaction with the Image Status Database (ISD)
## Requests
## Responses
In the case of a successful response, you will always receive a `data` key
that contains your data.
```
{
"meta": {
"code": 200
},
"data": {
...
},
"pagination": {
"next_url": "...",
"next_max_id": "13872296"
}
}
```
This gets displayed as:
The Swagger Editor, however, displays the proper code block:
Is this not supported by the Swagger UI?
The code block formatting issue was fixed in Swagger UI 3.22.0 and Swagger Editor 3.6.26. Code blocks are displayed properly in these versions:
Note the line break between "a data key" and "that contains" in the text - it is caused by the | literal block style, which preserves the line breaks in YAML multi-line strings. To avoid that line break you can either 1) remove it in your YAML, or 2) use the > folded style and also indent the code block (to prevent it from being folded), as shown below:
description: >
This API was created to allow interaction with the Image Status Database (ISD)
## Requests
## Responses
In the case of a successful response, you will always receive a `data` key
that contains your data.
```
{
"meta": {
"code": 200
},
"data": {
...
},
"pagination": {
"next_url": "...",
"next_max_id": "13872296"
}
}
```

Why is my JSON which contains HTML causing errors?

My JSON that is being returned from my ASP.NET MVC application looks like this:
{code: "1", error: "0", output: "<div class="a1"><div class="b1">this is fasta's</div></div>}
It is not working because I am not escaping it properly.
I'm not using a JSON library, can someone recommend a function that would clean up my HTML so this works?
I tried escaping for \ but it still doesn't work.
If I HTML encode it, should it work fine?
There will be user generated content so this has to work for all potential inputs by the user.
Are you able to use single quotes to wrap your HTML attribute values? I think that should work if you are able to do that.
For example,
{code: "1", error: "0", output: "<div class='a1'><div class='b1'>this is fasta's</div></div>"}
If that doesn't work, try using 2 backslashes to escape the double quotes.
For example,
{code: "1", error: "0", output: "<div class=\\"a1\\"><div class=\\"b1\\">this is fasta's</div></div>"}
You need to escape the internal double quotes, like this:
{code: "1", error: "0", output: "<div class=\"a1\"><div class=\"b1\">this is fasta's</div></div>"}

Resources