Skip to content

Add strikethrough support#257

Open
BentiGorlich wants to merge 1 commit intothephpleague:masterfrom
BentiGorlich:strikethrough-support
Open

Add strikethrough support#257
BentiGorlich wants to merge 1 commit intothephpleague:masterfrom
BentiGorlich:strikethrough-support

Conversation

@BentiGorlich
Copy link
Copy Markdown

Add a StrikethroughConverter which converts

  • <strike>Some Text</strike> -> ~~Some Text~~
  • <del>Some Text</del> -> ~~Some Text~~

Add a new optional converter `StrikethroughConverter`, mention it in the readme and add tests for it
Comment thread README.md
### Conversion options

> [!CAUTION]
> [!CAUTION]
Copy link
Copy Markdown
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 😅

@mare96
Copy link
Copy Markdown

mare96 commented Dec 13, 2024

@BentiGorlich Thank you, but <strike> is deprecated can you use <s> also ?

@melroy89 Do you have any thoughts on when this will be merged?

@BentiGorlich
Copy link
Copy Markdown
Author

Well it might be deprecated, but that doesn't mean that it isn't used anymore. The issue we received about the parsing problem contained the <strike> tag. I can certainly add the <s> tag :)
Since there is not a review from a maintainer yet I have no clue when this will be merged :)

@melroy89
Copy link
Copy Markdown

Do you have any thoughts on when this will be merged?

although I approved it. I don't have merge rights on this repo. Better ask @colinodell

@mrdezzods
Copy link
Copy Markdown

please merge this @colinodell

@NineBitsLTD
Copy link
Copy Markdown

Incorrect implementation. EmphasisConverter exists for this purpose. Your code loses flexible configuration settings.

@BentiGorlich
Copy link
Copy Markdown
Author

Incorrect implementation. EmphasisConverter exists for this purpose. Your code loses flexible configuration settings.

How would I solve this? And what settings are lost?

@margarizaldi
Copy link
Copy Markdown

margarizaldi commented Oct 31, 2025

Incorrect implementation. EmphasisConverter exists for this purpose. Your code loses flexible configuration settings.

How would I solve this? And what settings are lost?

@BentiGorlich I think you need to see the EmphasisConverter, not sure should merge into it or keep them separated

here's my own implementation for my project

class StrikethroughConverter implements ConverterInterface, ConfigurationAwareInterface
{
    public const STYLE_NAME = 'strikethrough_style';

    /** @var Configuration */
    protected $config;

    protected function getNormTag(?ElementInterface $element): string
    {
        if ($element !== null && ! $element->isText()) {
            $tag = $element->getTagName();
            if (in_array($tag, $this->getSupportedTags())) {
                return 'del';
            }
        }

        return '';
    }

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

    public function convert(ElementInterface $element): string
    {
        $tag = $this->getNormTag($element);
        $value = $element->getValue();

        if (! \trim($value)) {
            return $value;
        }

        $style = $this->config->getOption(self::STYLE_NAME, '~~');
        $prefix = \ltrim($value) !== $value ? ' ' : '';
        $suffix = \rtrim($value) !== $value ? ' ' : '';

        /* 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 = $this->getNormTag($element->getPreviousSibling()) === $tag ? '' : $style;
        $postStyle = $this->getNormTag($element->getNextSibling()) === $tag ? '' : $style;

        return $prefix . $preStyle . \trim($value) . $postStyle . $suffix;
    }

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants