1. cronoklee

    29.11.2009

    0 ↑
    0 ↓
    Hi,
    I'm trying to apply this great script to an online code editor. I have a solution that works OK but I need to remove the highlighting after the user submits the form, so that the highlight markup is not included in the submission. Is this possible please?

    Thanks!

    <div contenteditable="true" style="padding:5px; border:solid 1px #CCC" id="editorDiv">
    <pre><code>
    write html code here
    </code></pre>
    </div>
  2. cronoklee

    29.11.2009

    0 ↑
    0 ↓
    I've just noticed that new code that is typed in the box is not highlighted. (code that is already in the box on page load works fine) Is it possible also to run the highlighting function on an interval? How do I go about this?

    Thanks a lot
  3. Иван Сагалаев

    29.11.2009

    0 ↑
    0 ↓

    Hm... To be honest I don't think it would be possible. And even if it would I still don't think that using highlight.js for on-the-fly highlighting would be feasible. It's just too slow for this task if you have a moderately big chunk of text to highlight.

  4. cronoklee

    29.11.2009

    0 ↑
    0 ↓
    Hi, Thanks for the reply. I'm thinking of maybe refreshing it every second or less so it might be possible if the code is reasonably short.
    Regards removing the markup: I was thinking of adding an extra class to every span added by the script. That way, it could be removed after the content is submitted.

    It's complicated but I might run some tests and see how I get on. Any idea how I could add a second class to every dynamically generated span?
  5. cronoklee

    29.11.2009

    0 ↑
    0 ↓
    No need for that even! I've had a look at your markup and I think this simple regex should catch all dynamically added code:

    Replace:
    <span class="(keyword|comment|variable|string|number|comment|escape)">(.*?)</span>

    With:
    $2

    Is there any way to rerun the coloring function on an interval or would I have to use the regex to remove the markup and add it again every second or two?

    I suppose I'd be happy to just have the color coding applied on page load and require a refresh to update coloring.
  6. Иван Сагалаев

    01.12.2009

    0 ↑
    0 ↓

    This regexp won't be enough. There are plenty classnames for different languages. You may actually try to remove all the markup altogether.

    Is there any way to rerun the coloring function on an interval or would I have to use the regex to remove the markup and add it again every second or two?

    I'm not sure I understand what you mean... However there are a couple of functions that you can call manually:

    hljs.highlight(language_name, text) highlights a text with a language and returns a result object with three attributes

    • result.keyword_count — number of keywords
    • result.relevance — heuristic relevance
    • result.value — highlighted text

    A more high-level hljs.highlightBlock(block, tabReplace) takes a <code> node as "block" and replaces its content with highlighted content based on block's classname.

  7. cronoklee

    01.12.2009

    0 ↑
    0 ↓
    Great, thanks! I'll try running a function every few seconds which removes all markup and then uses hljs.highlightBlock(block, tabReplace) to replace it. Hopefully it won't be too memory intensive!
    I'll report back with results in a few days.
    Cheers!

Внимание! Это довольно старый топик, посты в него не попадут в новые, и их никто не увидит. Пишите пост, если хотите просто дополнить топик, а чтобы задать новый вопрос — начните новый.