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

Getting it working with manual mode #1

Open
KiranWells opened this issue Jun 15, 2021 · 1 comment
Open

Getting it working with manual mode #1

KiranWells opened this issue Jun 15, 2021 · 1 comment

Comments

@KiranWells
Copy link

When I used this with manual highlighting and Prism.highlight, it simply did nothing. After digging around in the code for a bit, I noticed that:

  1. Fold only activates on the first "on page load" run (the 'before-all-elements-highlight' hook).
  2. Fold is removed by the Prism sanity check or highlighter.
  3. This is easily fixable from a user's standpoint, although it is a bit hacky

Here is what finally got it working for me (using the js and css files directly from GitHub):

let el = document.getElementById("code") // the code element
el.innerHTML = string // the JSON (in this case) to be parsed 
insertFolds(el) // from prism-js-fold
let newstring = el.innerHTML
let html = Prism.highlight(newstring, Prism.languages.json, 'json')
html = html.replace(/&lt;/g, "<")
html = html.replace(/&gt;/g, ">")
// fix the class, open, etc. attributes being converted to span.token.string elements
html = html.replace(/<(\w+) (\w+)=<span class="token string">"(\w*)"<\/span>/g, '<$1 $2="$3"')
el.innerHTML = html

This could possibly be fixed with an "after-highlight" hook, although I am not sure if it works the same as Prism.highlight. My results with a hook always seemed to have the details and summary elements stripped out.

@fnep
Copy link

fnep commented Jul 20, 2021

After there was no response for a while, I'm not so sure if this module will be maintained or if it's just here for reference.

First requirement will be to export the insertFolds function. I will send a pull request for this.

Then I made it working by using the insertFolds function like this:

import {insertFolds} from "prism-js-fold";

Prism.highlightElement(codeElement);
insertFolds(codeElement);

… respectively …

import {insertFolds} from "prism-js-fold";

codeElement.innerHTML = Prism.highlight(myJsonContent, Prism.languages.json, 'json');
insertFolds(codeElement);

Is this also working for you, @KiranWells?

fnep added a commit to fnep/prism-js-fold that referenced this issue Jul 20, 2021
fnep added a commit to fnep/prism-js-fold that referenced this issue Jul 22, 2021
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

2 participants