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

Delimeter processor doesn't run in links and images #1002

Closed
dkarlovi opened this issue Dec 6, 2023 · 3 comments
Closed

Delimeter processor doesn't run in links and images #1002

dkarlovi opened this issue Dec 6, 2023 · 3 comments
Labels
invalid Not a bug or not directly to this project question General questions about the project or usage spec compliance Issues or question about compliance with the CommonMark or GFM specs

Comments

@dkarlovi
Copy link

dkarlovi commented Dec 6, 2023

Version(s) affected

2.4.1

Description

This works:

This is an asset lookup: {{asset('assets/images/logo.svg')}}

This doesn't work:

![Logo]({{asset('assets/images/logo.svg')}})

How to reproduce

Register this delimiter processor

final class ExpressionDelimiter implements DelimiterProcessorInterface
{
    public function getOpeningCharacter(): string
    {
        return '{';
    }

    public function getClosingCharacter(): string
    {
        return '}';
    }

    public function getMinLength(): int
    {
        return 2;
    }

    public function getDelimiterUse(DelimiterInterface $opener, DelimiterInterface $closer): int
    {
        return 2;
    }

    public function process(AbstractStringContainer $opener, AbstractStringContainer $closer, int $delimiterUse): void
    {
        $opener->insertAfter($this->expression($opener, $closer));
    }

    private function expression(AbstractStringContainer $opener, AbstractStringContainer $closer): AbstractStringContainer
    {
        $expressionNode = new Expression();

        $node = $opener->next();
        while ($node !== null && $node !== $closer) {
            if ($node instanceof StringContainerInterface === false) {
                throw new \RuntimeException('Invalid node type found');
            }
            $expressionNode->append($node);
            $expressionNode->appendChild($node);
            $node = $node->next();
        }

        return $expressionNode;
    }
}

Possible solution

No response

Additional context

The idea is to allow placing expressions into a MD file (say, reference assets, images which get processed in some way before rendering the Markdown).

See sigwinhq/yassg#181

Did this project help you today? Did it make you happy in any way?

No response

@colinodell
Copy link
Member

According to the CommonMark spec, link text is allowed to contain other inlines (including delimiter-based ones) but link destinations are not. You can see this by testing ![foo](**emphasis**) in several different Markdown parsers

The idea is to allow placing expressions into a MD file (say, reference assets, images which get processed in some way before rendering the Markdown).

Ah! For that case I'd highly recommend performing that pre-processing before parsing the Markdown (so that the expressions are actually rendered into Markdown). You could do this before passing the Markdown into this library, or by listening for the DocumentPreParsedEvent.

Your syntax looks very similar to Twig - you could run the document through Twig first (to render the expressions) and then feed that final Markdown through here.

@colinodell colinodell added invalid Not a bug or not directly to this project question General questions about the project or usage spec compliance Issues or question about compliance with the CommonMark or GFM specs labels Dec 7, 2023
@colinodell colinodell closed this as not planned Won't fix, can't repro, duplicate, stale Dec 7, 2023
@dkarlovi
Copy link
Author

dkarlovi commented Dec 7, 2023

@colinodell thanks for the inspiration, I did consider doing it like that before, but it seemed kind of heavy handed because you go from not being able to do anything to being able to do way too much. 😆

I'll do it by implementing the Twig preprocessing in a sandboxed manner, meaning the Twig instance will be specifically narrowed as much as possible.

Thanks, have a good day!

@colinodell
Copy link
Member

That sounds like a great approach! Feel free to reach out again if you have any other questions 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Not a bug or not directly to this project question General questions about the project or usage spec compliance Issues or question about compliance with the CommonMark or GFM specs
Projects
None yet
Development

No branches or pull requests

2 participants