Skip to content

material-encryptcontent: reload all subscriptions#81

Merged
unverbuggt merged 1 commit into
unverbuggt:version3from
joapuiib:reload-material-theme
Mar 1, 2025
Merged

material-encryptcontent: reload all subscriptions#81
unverbuggt merged 1 commit into
unverbuggt:version3from
joapuiib:reload-material-theme

Conversation

@joapuiib

Copy link
Copy Markdown
Contributor

I'm working with this plugin to publish exams. In order to get mermaid working, I had to follow the instructions specified in Mermaid.js section in README.md.

This works, but will only reload Mermaid, not other JS features (code.copy, for example).


Relevant issue in mkdocs-material: squidfunk/mkdocs-material#5816 (comment)

Since, all JS associated with document content changes are subscribed to document$:

document$.subscribe(function() {
  console.log("Put your code here") // Executes on `DOMContentLoaded` and on instant navigation
})

We can trigger all subscriptions after a decryption:

function theme_run_after_decryption() {
    document$.next(document);
}

I've tested it in my site, and it works, but it might need further testing.

@joapuiib

joapuiib commented Feb 21, 2025

Copy link
Copy Markdown
Contributor Author

Also, maybe, it could be included in the code directly so no extra_javascript configuration is needed:

// any post processing on the decrypted content should be done here
{% if arithmatex -%}
if (typeof MathJax === 'object') { MathJax.typesetPromise();};
{%- endif %}
{% if mermaid2 -%}
if (typeof mermaid === 'object') { mermaid.contentLoaded();};
{%- endif %}
{% if hljs -%}
decrypted_content.querySelectorAll('pre code').forEach((block) => {
hljs.highlightElement(block);
});
{%- endif %}
{% if reload_scripts | length > 0 -%}
let reload_scripts = {{ reload_scripts }};
for (let i = 0; i < reload_scripts.length; i++) {
{% if webcrypto %}await {% endif %}reload_js(reload_scripts[i]);
}
{%- endif %}
if (typeof theme_run_after_decryption !== 'undefined') {
theme_run_after_decryption();
}

@unverbuggt

Copy link
Copy Markdown
Owner

Great solution. Way better than the one I came up with for #58. I'll add it to the decrypt-contents js template

@unverbuggt
unverbuggt merged commit a52f82d into unverbuggt:version3 Mar 1, 2025
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.

2 participants