So I've got a Notification that can be delivered to users either via email or text messsage (via Twilio) depending on the user settings. Ideally, the notifications would be written in Markdown, so that the email versions can be delivered in a suitable way, but I would need to process the text to make it suitable for text message delivery. Any thoughts on how I would best process markdown input to as plain a text format as possible for SMS delivery?
#Converting Markdown to a format suitable for Text Message delivery
24 messages · Page 1 of 1 (latest)
Depends on how you're building the notification content but I would use 2 different view files (one markdown and one plain text) to manage this.
That much I've already got, I'm more thinking about the nitty gritty of converting the markdown text to something that can be delivered as a text message.
So if the notification were This is **a notification** delivered via email and text the email would see the bold text and the text message wouldn't.
Just have 2 different views? one for markdown (when you send an email) and one for text notifications?
Again, that's not my problem.
Depends on how you're building the notification content
Are your users writing the markdown for the notification content?
Yes.
Or, rather, the administrators who send the notifications in question.
The email version is already being passed through Stevebauman\Purify after parsedown does its thing, so I'm not worried about any nasty injections or things like that.
It sounds like you want to convert Markdown-formatted text into plain text so that it can be delivered as a text message. This can be done using a Markdown parser library, which will convert the Markdown-formatted text into HTML. You can then use a library or regular expression to strip the HTML tags from the text, leaving only the plain text content.
For example, you could use the League\HTMLToMarkdown\HtmlConverter class to convert the HTML to Markdown, and then use the strip_tags() function to remove the HTML tags.
Personally, I'd still go with my original solution which is to provide 2 options (markdown or plaintext) when the admins write the notification Content. However, you could use preg_replace or string_replace (another another package) that strips out the markdown tags .
Yeah, that's what I was thinking, too, but that breaks as soon as the markdown is a bulleted list or something similar, since the bullets will be converted to HTML which is then removed.
This is a message with a list.
- It has an item
- And another item.
converted from markdown to html and then run through strip_tags leaves just This is a message with a list. It has an item And another item.
use League\HTMLToMarkdown\HtmlConverter;
// Create a new instance of the HtmlConverter class
$converter = new HtmlConverter();
// Convert the HTML to Markdown
$markdown = $converter->convert($html);
// Strip the HTML tags from the Markdown
$plainText = strip_tags($markdown);
Converts HTML to formatted plain text
Hmm, that could work. Gonna give it a try.
According to ChatGPT it works with lists 😂
In that case, the only concern would be links, which would ideally be converted from [linktext](https://google.com) to linktext (https://google.com)
I've never used html2text myself but apparently it does this. I'd suggest writing some tests to confirm 💡
> $markdown = "This is a [link](https://google.com), followed by a list.
.
. - The first entry on the list.
. - The second entry on the list.
.
. Now let's see how this works."
= """
This is a [link](https://google.com), followed by a list.\n
- The first entry on the list.\n
- The second entry on the list.\n
Now let's see how this works.
"""
> $html = parsedown($markdown)
= """
<p>This is a <a href="https://google.com">link</a>, followed by a list.</p>\n
<ul>\n
<li>The first entry on the list.</li>\n
<li>The second entry on the list.\n
Now let's see how this works.</li>\n
</ul>
"""
> $text = new \Html2Text\Html2Text($html)
= Html2Text\Html2Text {#4598}
> $text->getText()
= """
This is a link [https://google.com], followed by a list.\n
\n
\t* The first entry on the list.\n
\t* The second entry on the list. Now let's see how this works.\n
\n
"""
Looks good enough, some filtering after Html2Text does its thing and that should work as intended..
The error in parsing the string after the second item on the list is a common parsedown issue when the input text doesn't have correctly formatted linebreaks, which my input string didn't have.
Thanks for the suggestion, @prime quiver ! I'll do some more tests with Html2Text, but it looks like it does what I need it to do.