Skip to content

Commit

Permalink
! Fix ILA in PBE mails. e.g. dont link both child image and parent link
Browse files Browse the repository at this point in the history
! Consolidate somewhat the line length wrapping decision
! Set PBE to gen a base64 image.png
! Drop link image
! line end image and href links to cut down line lengths
  • Loading branch information
Spuds committed Mar 29, 2021
1 parent 4a5b820 commit 629e5f6
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions sources/subs/Html2Md.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,12 @@ public function get_markdown()
// Wordwrap?
if (!empty($this->body_width))
{
$this->_check_line_lenght($this->markdown);
$this->markdown = $this->_utf8_wordwrap($this->markdown, $this->body_width, $this->line_end);
}

return $this->markdown;
// The null character will trigger a base64 version in outbound email
return $this->markdown . "\n\x00";
}

/**
Expand Down Expand Up @@ -402,7 +404,10 @@ private function _convert_to_markdown($node)
switch ($tag)
{
case 'a':
$markdown = $this->line_end . $this->_convert_anchor($node);
if ($node->getAttribute('data-lightboximage') || $node->getAttribute('data-lightboxmessage'))
$markdown = '~`skip`~';
else
$markdown = $this->line_end . $this->_convert_anchor($node) . $this->line_end;
break;
case 'abbr':
$markdown = $this->_convert_abbr($node);
Expand Down Expand Up @@ -449,7 +454,7 @@ private function _convert_to_markdown($node)
$markdown = $this->_convert_header($tag, $this->_get_value($node));
break;
case 'img':
$markdown = $this->_convert_image($node);
$markdown = $this->_convert_image($node) . $this->line_end;
break;
case 'ol':
case 'ul':
Expand Down Expand Up @@ -587,14 +592,9 @@ private function _convert_anchor($node)
}
else
{
// This will trigger a base64 version in our outbound email
$link = "\xF0\x9F\x94\x97";
$markdown = '[' . $value . '](' . $href . ' ' . $link . ')';
$markdown = '[' . $value . ']( ' . $href . ' )';
}

// Some links can be very long and if we wrap them they break
$this->_check_link_lenght($markdown);

return $markdown;
}

Expand Down Expand Up @@ -680,7 +680,7 @@ private function _convert_code($node)
{
// Adjust the word wrapping since this has code tags, leave it up to
// the email client to mess these up ;)
$this->_check_link_lenght($markdown, 5);
$this->_check_line_lenght($markdown, 5);

$markdown .= str_repeat(' ', 4) . $line . $this->line_end;
}
Expand Down Expand Up @@ -772,12 +772,6 @@ private function _convert_image($node)
$markdown = '![' . $alt . '](' . $src . ')';
}

// Adjust width if needed to maintain the image
$this->_check_link_lenght($markdown);

// Adjust width if needed to maintain the image
$this->_check_link_lenght($markdown);

return $markdown;
}

Expand Down Expand Up @@ -911,7 +905,7 @@ private function _convert_table($node)
}

// Adjust the word wrapping since this has a table, will get mussed by email anyway
$this->_check_link_lenght($rows[1], 2);
$this->_check_line_lenght($rows[1], 2);

// Return what we did so it can be swapped in
return implode($this->line_end, $rows);
Expand Down Expand Up @@ -1190,22 +1184,26 @@ private function _has_ticks($node, $value)
* @param string $markdown
* @param bool|int $buffer
*/
private function _check_link_lenght($markdown, $buffer = false)
private function _check_line_lenght($markdown, $buffer = false)
{
// Some links can be very long and if we wrap them they break
$line_strlen = Util::strlen($markdown) + (!empty($buffer) ? (int) $buffer : 0);
// Some Lines can be very long and if we wrap them they break
$lines = explode($this->line_end, $markdown);
foreach ($lines as $line)
{
$line_strlen = Util::strlen($line) + (!empty($buffer) ? (int) $buffer : 0);
if ($line_strlen > $this->body_width)
{
$this->body_width = $line_strlen;
}
}
}

/**
* Helper function to find and wrap plain text links in MD format
*/
private function _convert_plaintxt_links()
{
$this->markdown = preg_replace_callback('/[^\(\/\]]((https?):\/\/|www\.)[-\p{L}0-9+&@#\/%?=~_|!:,.;]*[\p{L}0-9+&@#\/%=~_|]/iu', array($this, '_plaintxt_callback'), $this->markdown);
$this->markdown = preg_replace_callback('/((?<!\]\( |\]\()https?:\/\/|(?<!\]\( |\]\(|:\/\/)www)[-\p{L}0-9+&@#\/%?=~_|!:,.;]*[\p{L}0-9+&@#\/%=~_|]/iu', array($this, '_plaintxt_callback'), $this->markdown);
}

/**
Expand All @@ -1219,7 +1217,6 @@ private function _plaintxt_callback($matches)
global $txt;

$replacement = $this->line_end . '[' . $txt['link'] . '](' . trim($matches[0]) . ')';
$this->_check_link_lenght($replacement);

return $replacement;
}
Expand Down

0 comments on commit 629e5f6

Please sign in to comment.