Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add strikethrough support #257

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ The included `demo` directory contains an HTML->Markdown conversion form to try

### Conversion options

> [!CAUTION]
> [!CAUTION]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about this change, but PHPStork wouldn't let me revert this 😅

> By default, this library preserves HTML tags without Markdown equivalents, like `<span>`, `<div>`, `<iframe>`, `<script>`, etc. If you will be parsing untrusted input from users, **please consider setting the `strip_tags` and/or `remove_nodes` options** documented below, and also using a library (like [HTML Purifier](https://github.com/ezyang/htmlpurifier)) to provide additional HTML filtering.

To strip HTML tags that don't have a Markdown equivalent while preserving the content inside them, set `strip_tags` to true, like this:
Expand Down Expand Up @@ -197,6 +197,21 @@ $html = "<table><tr><th>A</th></tr><tr><td>a</td></tr></table>";
$markdown = $converter->convert($html);
```

### Strikethrough support

Support for Markdown strikethrough is not enabled by default, because it is not part of the original Markdown syntax. To use strikethrough add the converter explicitly:

```php
use League\HTMLToMarkdown\HtmlConverter;
use League\HTMLToMarkdown\Converter\StrikethroughConverter;

$converter = new HtmlConverter();
$converter->getEnvironment()->addConverter(new StrikethroughConverter());

$html = "<del>Some Text</del> <strike>Some Text2<strike>";
$markdown = $converter->convert($html);
```

### Limitations

- Markdown Extra, MultiMarkdown and other variants aren't supported – just Markdown.
Expand Down
58 changes: 58 additions & 0 deletions src/Converter/StrikethroughConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace League\HTMLToMarkdown\Converter;

use League\HTMLToMarkdown\Configuration;
use League\HTMLToMarkdown\ConfigurationAwareInterface;
use League\HTMLToMarkdown\ElementInterface;

class StrikethroughConverter implements ConverterInterface, ConfigurationAwareInterface
{
/** @var Configuration */
protected $config;

/**
* @return string[]
*/
public function getSupportedTags(): array
{
return ['del', 'strike'];
}

public function setConfig(Configuration $config): void
{
$this->config = $config;
}

public function convert(ElementInterface $element): string
{
$value = $element->getValue();
if (! \trim($value)) {
return $value;
}

$prefix = \ltrim($value) !== $value ? ' ' : '';
$suffix = \rtrim($value) !== $value ? ' ' : '';

$previousTagName = null;
if (($previous = $element->getPreviousSibling()) !== null) {
$previousTagName = $previous->getTagName();
}

$nextTagName = null;
if (($next = $element->getNextSibling()) !== null) {
$nextTagName = $next->getTagName();
}

/* If this node is immediately preceded or followed by one of the same type don't emit
* the start or end $style, respectively. This prevents <del>foo</del><del>bar</del> from
* being converted to ~~foo~~~~bar~~ which is incorrect. We want ~~foobar~~ instead.
*/
$preStyle = \in_array($previousTagName, $this->getSupportedTags(), true) ? '' : '~~';
$postStyle = \in_array($nextTagName, $this->getSupportedTags(), true) ? '' : '~~';

return $prefix . $preStyle . \trim($value) . $postStyle . $suffix;
}
}
12 changes: 12 additions & 0 deletions tests/HtmlConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace League\HTMLToMarkdown\Test;

use League\HTMLToMarkdown\Converter\ConverterInterface;
use League\HTMLToMarkdown\Converter\StrikethroughConverter;
use League\HTMLToMarkdown\Converter\TableConverter;
use League\HTMLToMarkdown\Environment;
use League\HTMLToMarkdown\HtmlConverter;
Expand Down Expand Up @@ -422,4 +423,15 @@ public function testInstatiationWithEnvironment(): void
$result = $markdown->convert($htmlH4);
$this->assertEquals($htmlH4, $result);
}

public function testStrikethrough(): void
{
$opt = [];
$conv = [new StrikethroughConverter()];
$this->assertHtmlGivesMarkdown('<strike>Some Text</strike>', '~~Some Text~~', $opt, $conv);
$this->assertHtmlGivesMarkdown('<del>Some Text</del>', '~~Some Text~~', $opt, $conv);
$this->assertHtmlGivesMarkdown('<del>Some</del><del> Text</del>', '~~Some Text~~', $opt, $conv);
$this->assertHtmlGivesMarkdown('<del>Some</del><strike> Text</strike>', '~~Some Text~~', $opt, $conv);
$this->assertHtmlGivesMarkdown('<del>Some</del> <strike>Text</strike>', '~~Some~~ ~~Text~~', $opt, $conv);
}
}