Skip to content
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

Move <pre> data attributes to <code> blocks #3655

Open
gabyx opened this issue Jul 10, 2024 · 0 comments
Open

Move <pre> data attributes to <code> blocks #3655

gabyx opened this issue Jul 10, 2024 · 0 comments

Comments

@gabyx
Copy link

gabyx commented Jul 10, 2024

I am using the following code to move all data-* attributes from <pre> tags onto <code> blocks before Reveal.intialize(...):

function moveDataAttributesToCodeElement() {
  const codeElements = document.querySelectorAll("pre > code")
  codeElements.forEach((code, index) => {
    console.log(
      `Moving all 'data-' attributes of code block ${index} to <code>`,
      code,
    )
    const pre = code.parentNode
    console.log(pre)

    Array.from(pre.attributes).forEach((attr) => {
      if (attr.name.startsWith("data-")) {
        code.setAttribute(attr.name, attr.value)
        pre.removeAttribute(attr.name)
      }
    })
  })
}

This works nicely. But somehow data-fragment-index="1" gets changed to ="0" with this initialization function:

      moveDataAttributesToCodeElement()

      // Full list of configuration options available at:
      // https://revealjs.com/config/
      Reveal.initialize({
        plugins: [
          RevealNotes,
          RevealSearch,
          RevealZoom,
          RevealHighlight,
          RevealMath.MathJax3,
          RevealMenu
        ]
      })

I am wondering what I am doing wrong when changeing the DOM just before Reveal initializes. It seems to not take up that
changes correctly?

Is there a better approach?

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

No branches or pull requests

1 participant