Skip to content

material-encryptcontent: reload all subscriptions #81

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

Merged
merged 1 commit into from
Mar 1, 2025

Conversation

joapuiib
Copy link
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
Copy link
Contributor Author

joapuiib commented Feb 21, 2025

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
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.

None yet

2 participants