DanielFlaum / grav-plugin-mermaid-diagrams

Mermaid Diagrams is a Grav plugin that adds simple and powerful diagrams functionality
https://github.com/DanielFlaum/grav-plugin-mermaid-diagrams
MIT License
12 stars 1 forks source link

Mermaid block code configuration based. #7

Open zenhaust opened 1 year ago

zenhaust commented 1 year ago

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 commented 1 year ago

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

DanielFlaum commented 1 year ago

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 commented 1 year ago

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