-
Notifications
You must be signed in to change notification settings - Fork 29
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
The line numbering and the syntax highlighter doesn't work properly. #34
Comments
@sahilrl Have you looked at the JavaScript console of your browser while loading the page, to make sure all the proper assets are loading? That'd be the first place to check. |
Hi, @FlipperPA I just checked and in the HTML, the The problem is occurring even when the following files are loading. JS CSS All the spans on the code are sometimes missed on reload.
|
@sahilrl Just to clarify, you're saying the SPANs are in the HTML source, but they're not being highlighted? Is there anything in the JavaScript console like an error that might help us narrow it down? The loading mechanism used on the template side is kind of funky; since multiple code blocks can appear on a page, it uses JavaScript to load JavaScript libraries necessary, only once. I'm wondering if that's where it is getting hung up. |
@FlipperPA No, actually the spans are not appearing at all. |
Can confirm this issue happens at random. Only console error is |
I also encounter the same thing as @sahilrl . It only seems to happen with the Python syntax.. Any workarounds? |
It sounds like some kind of race condition may be going on, and because of how I'm dynamically loading libraries... it is tough to debug. I'm due to upgrade the underlying version of PrismJS as well. Let me do that and see if it improves things at all, because these kinds of issues are so hard to reproduce. |
Thanks you for the quick reply. I also observe that in our live version it occurs less than in my local testing environment supporting your suspicion that this is a racing condition. |
@frankcorneliusmartin @sahilrl @cesaregarza I'm wondering if any of you have a solid reproduction for this that you could pass along to me. We use v1.29.0.0 very heavily at my workplace, and we haven't had any reports of issues like this. If you could set up a git repo with a minimal repro, that would be amazing. Also, what version of Wagtail and |
Hello @FlipperPA,
To reproduce: Step1: Clone repository Step2: Create virtual environment Step3: Install dependencies Step4: Run server Step5: In the browser, navigate to Refresh multiple times and notice that at random, the syntax highlighting doesn't work. |
@sahilrl Thank you! I'll take a look into this. Is everyone else using Wagtail 5.x? My workplace is still on 4.x, wondering if that's the problem. |
@FlipperPA my best guess is that the lines plugin is being parsed by the browser just before the actual core library in some cases. Here's a starting point for a refined script that will wait for the core library to be loaded and only then will it try to load the other plugins. See {% load static wagtailcodeblock_tags %}
...
if(typeof loadPrismLanguage != 'function') {
window.loadPrismLanguage = function(language) {
var libraries = [
/*Since there is no html.min.js this check makes sure we
bypass the loading of the script when syntax is set to HTML */
{% if val != "html" %}
,
{
"id": "code-block-prismjs-" + language,
"url": "//cdnjs.cloudflare.com/ajax/libs/prism/{% prism_version %}/components/prism-" + language + ".min.js"
}
{% endif %}
{% line_numbers_js %}
{% toolbar_js %}
{% copy_to_clipboard_js %}
];
var core = {
"id": "code-block-prismjs",
"url": "//cdnjs.cloudflare.com/ajax/libs/prism/{% prism_version %}/prism.min.js"
};
// here we create a promise that will resolve immediately if the element is in the DOM
// or it will wait until the script is loaded before the rest of the plugins run
// another approach may be to do a setTimeout (with no delay) IF the script is in the DOM already - this way we know we will not try to run the other libraries until the current JS has run.
new Promise((resolve) => {
if (document.getElementById(core['id'])) resolve();
var s = document.createElement("script");
s.id = core["id"];
s.type = "text/javascript";
s.src = core["url"];
s.async = false;
s.addEventListener('load', resolve, {once: true});
}).then(() => {
for(const library of libraries) {
if(document.getElementById(library["id"]) == null) {
var s = document.createElement("script");
s.id = library["id"];
s.type = "text/javascript";
s.src = library["url"];
s.async = false;
document.body.appendChild(s);
}
}
});
};
}
loadPrismLanguage('{{ val }}');
language_class_name = 'language-{{ val }}';
</script> However, a long term solution would probably be to provide a single template tag that needs to be included in pages that may contain the codeblock. This could still async load the JS as needed, maybe seeing if a DOM element appears in the dom with the mutation observer or using Wagtail 5.2's support for Stimulus ;) This central script would bootstrap itself once any code block with a specific data attribute appears and then use classes/data attributes to determine what other libraries should load as needed. |
Yes indeed, we are on 5. |
Also on 5.x |
When I reload the website sometimes the syntax highlighting and the numbering doesn't appear. If I keep reloading the webpage multiple times, then it appears after several tries. And if I reload it one more time, then the highlighting and numbering disappear again.
It's an issue only encountered when rendering on templates, not the admin panel.
Appearing now
on next reload, disappearing.
The text was updated successfully, but these errors were encountered: