Skip to content

Feature request: Adding diagram-type-specific classes automatically #6

@Thiti517

Description

@Thiti517

Hello there,

First of all, thank you for this awesome extension. It works incredibly well to auto-generate diagrams in Markdown (especially when used with mkdocs and mkdocs-to-pdf).

Context

In my use case, I’m post-processing Mermaid diagrams to replace <img> tags with <object> tags. This allows users to copy text in the diagram on site (in PDF user can already copy diagram text from the ). To do this, I currently rely on the presence of a class mermaid to identify relevant diagrams.

Here’s the code I use:

# .mkdocs/hooks/mermaid_img_to_obj.py
def on_page_content(html: str, **kwargs) -> str:
    """
    Replace <img.mermaid> with <object> allowing users to copy diagram text
    """
    soup = BeautifulSoup(html, 'html5lib')
    
    for img in soup.select('img.mermaid'):
        img_src = img.get('src')
        if img_src is not None:
            obj = soup.new_tag('object', attrs={'data': str(img_src), 'type': 'image/svg+xml'})
            
            # Add the <img> as a fallback to object tag (Needed for PDF since it can't display <object> data but can display it's fallback <img>)
            img_copy = copy.copy(img)
            obj.append(img_copy)

            img.replace_with(obj)

    return str(soup)

Feature Suggestion

It would be helpful if each diagram generated via Kroki automatically included a class that identifies its diagram type. This would make it easier to style or target specific diagrams.

For example:

Diagram Type Image Class
Mermaid kroki-mermaid
PlantUML kroki-plantuml
GraphViz kroki-graphviz

Benefit

Adding a kroki-<diagramtype> class would:

  • Improve customization for themes and plugins.
  • Allow finer control for layout, rendering tweaks or modification.
  • No need to specify the class on each diagram especially useful when you can have thousand of them.

In my context, automatically adding diagram-type-specific classes would remove the need to manually infer or inject those classes and would enable similar enhancements for other diagram types.


Thanks again for your great work. :D

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions