Skip to content

separate import, elk layout and init-js into different sub category #180

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Eymeric65
Copy link

I do think it need some quick discussion about how to treat the init-js behavior. As stated in #172, documentation may lake some information about the behavior of init-js. I do think that init-js config variable shouldn't be linked with mermaid version and elk import and following this I propose this code modification to separate these three behavior.

@Eymeric65
Copy link
Author

Eymeric65 commented Jun 20, 2025

I also found really strange behavior when using sphinx config variable as a 'running' variable like this :

    if app.config.mermaid_init_js == _MERMAID_INIT_JS_DEFAULT:
        # Update if esm is used and no custom init-js is provided
        if _mermaid_elk_js_url:
            # Add registration of ELK layouts
            app.config.mermaid_init_js = f'import mermaid from "{_mermaid_js_url}";import elkLayouts from "{_mermaid_elk_js_url}";mermaid.registerLayoutLoaders(elkLayouts);{app.config.mermaid_init_js}'
        else:
            app.config.mermaid_init_js = f'import mermaid from "{_mermaid_js_url}";{app.config.mermaid_init_js}'

    if app.config.mermaid_init_js:
        # If mermaid is local the init-call must be placed after `html_js_files` which has a priority of 800.
        priority = app.config.mermaid_init_js_priority if _mermaid_js_url is not None else 801
        app.add_js_file(None, body=app.config.mermaid_init_js, priority=priority, type="module")

it may be better to adopt a safer approach like this one :

    mermaid_init_js = app.config.mermaid_init_js

    if _mermaid_elk_js_url:
        # Add registration of ELK layouts
        mermaid_init_js = f'import elkLayouts from "{_mermaid_elk_js_url}";mermaid.registerLayoutLoaders(elkLayouts);{mermaid_init_js}'


    if mermaid_init_js:

        # Add the mermaid import from link to the init-js
        mermaid_init_js = f'import mermaid from "{_mermaid_js_url}";{mermaid_init_js}'
        
        # If mermaid is local the init-call must be placed after `html_js_files` which has a priority of 800.
        priority = app.config.mermaid_init_js_priority if _mermaid_js_url is not None else 801
        app.add_js_file(None, body=mermaid_init_js, priority=priority, type="module")

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.

1 participant