Script to add spans to h2 if they contain the string "Section"

Dayju's Avatar


23 Sep, 2022 09:03 PM

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')) })

  1. 1 Posted by Dayju on 23 Sep, 2022 09:07 PM

    Dayju's Avatar

    Backticks were removed in the code in the previous post.
    Here is the image

  2. Support Staff 2 Posted by Brett on 24 Sep, 2022 07:55 AM

    Brett's Avatar

    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. 3 Posted by Dayju on 24 Sep, 2022 12:08 PM

    Dayju's Avatar

    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 ?

  4. Support Staff 4 Posted by Brett on 24 Sep, 2022 02:19 PM

    Brett's Avatar

    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.


Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:


Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts


? 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