Script to add spans to h2 if they contain the string "Section"
I am using the following Script to add spans to H2 elements if they contain the String "Section"
It works only when I right click and select refresh, but not when I make any changes to the Markdown and save the file.
document.addEventListener("DOMContentLoaded", ()=>{ let nl = document.querySelectorAll('h2') const nlArray = Array.from(nl) // console.log("Array", nlArray) nlArray.forEach(item => { if(item.firstChild.textContent.indexOf("Section") !== -1){ // console.log(item.children[0]) item.innerHTML =<span class='section'>${item.firstChild.textContent}</span> } }) // console.log(document.querySelectorAll('h2')) })
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
1 Posted by Dayju on 23 Sep, 2022 09:07 PM
Backticks were removed in the code in the previous post.
Here is the image
Support Staff 2 Posted by Brett on 24 Sep, 2022 07:55 AM
This is because as of version 2.6, Marked no longer does a page refresh when updating, it simply injects the modified/updated content into the DOM directly. This makes for much faster and smoother refreshes, but it means scripts that rely on DOM load won't get triggered unless a refresh is forced.
I may add "hooks" at some point that scripts like this could subscribe to which would be triggered on a content update, but I don't have immediate plans for that.
The alternative would be to use Marked's custom preprocessor or processor functionality to inject the spans directly into the markup. You could use Python or Ruby to grep for ## lines containing Section and inject the spans before it ever got to the browser window.
3 Posted by Dayju on 24 Sep, 2022 12:08 PM
Thanks for the Reply.
Never thought about using pre-processors for Markdown. Just curious, but can I use gulpJS as a pre-processor for Markdown ?
Support Staff 4 Posted by Brett on 24 Sep, 2022 02:19 PM
You can use anything that you can execute on the commands line, as long as it can accept input on STDIN and return on STDOUT.
Thanks,
Brett