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

Mermaid block code configuration based. #7

Open
zenhaust opened this issue Mar 23, 2023 · 3 comments
Open

Mermaid block code configuration based. #7

zenhaust opened this issue Mar 23, 2023 · 3 comments

Comments

@zenhaust
Copy link

Hi.

This is a new feature for your consideration

Gitlab or Stackedit uses the syntax for mermaid code block recognition.

```mermaid
mermaid code
```

Maybe you could add a new feature to set the block delimiters.

It could be something like that.

In mermaid-diagrams.yaml#L6 (General Settings) I add two new properties

block-detector-begin: '```mermaid'
block-detector-end: '```'

In mermaid-diagrams.php#L40

I replace

                $search_mermaid = str_replace("[mermaid]", "", $search_mermaid);
                $search_mermaid = str_replace("[/mermaid]", "", $search_mermaid);

by

                $search_mermaid = str_replace($config["block-detector-begin"], "", $search_mermaid);
                $search_mermaid = str_replace($config["block-detector-end"], "", $search_mermaid);

And finaliy, the code in the method parseInjectMermaid, could look something like this

    protected function parseInjectMermaid($content, $function)
    {
        // Regular Expression for selection
        $blockBegin=$this->config->get('plugins.mermaid-diagrams.block-detector-begin'); 
        $blockEnd=$this->config->get('plugins.mermaid-diagrams.block-detector-end'); 

        $blockBegin = str_replace("[","\\[",$blockBegin);
        $blockBegin = str_replace("]","\\]",$blockBegin);
        $blockEnd = str_replace("[","\\[",$blockEnd);
        $blockEnd = str_replace("]","\\]",$blockEnd);
        $blockEnd = str_replace("/","\\/",$blockEnd);

        $regex = '/'.$blockBegin.'([\s\S]*?)'.$blockEnd.'/';
        return preg_replace_callback($regex, $function, $content);
    }

Thanks for your attention and regards.

@DanielFlaum
Copy link
Owner

DanielFlaum commented Apr 2, 2023

Yeah, I can try this out! Sorry I took so long to respond. I'll have a stab at it this week

@DanielFlaum
Copy link
Owner

So I've got this working on my local machine and it seems good. I want to make sure I understand how things are meant to work, though, and I have a question:

These lines you've added to parseInjectMermaid($content, $function)...

        $blockBegin = str_replace("[","\\[",$blockBegin);
        $blockBegin = str_replace("]","\\]",$blockBegin);
        $blockEnd = str_replace("[","\\[",$blockEnd);
        $blockEnd = str_replace("]","\\]",$blockEnd);
        $blockEnd = str_replace("/","\\/",$blockEnd);

...were added so that, if someone sets the block-detector-* configuration to [mermaid] and [/mermaid], those values will be properly sanitized to the regular expression, correct?

If I'm right about that, then I'll proceed. I am going to set the default value of the delimiters to [mermaid] [/mermaid] so as not to break the websites of people who update existing installations of our plugin.

Two last questions. First, would you prefer that I create a development branch on the repository where you can make a pull request? That way your work will be attributed to your GitHub account and stuff. If so, please write commit messages following this guide. Second, how would you like to be credited in the release notes?

Thanks for your help!

@zenhaust
Copy link
Author

zenhaust commented Apr 10, 2023

It´s correct. The str_replace function escapes the special characters, so that the regular expression works properly.
In response to your first question, no problem. It seems good to me.
Please add me to the project so I can create a new branch.
Regarding your second question, I leave it under your consideration.
Thanks and regards

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

No branches or pull requests

2 participants