question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Prism.highlightAll(text: string): string

See original GitHub issue

I’m currently implementing a node wrapper for prism that takes in an html file as input and passes its content to Prism.highlight. The issue is the file may or may not contain more than one language/code blocks. So the following doesn’t work:

#! /usr/bin/env node

import * as fs from "fs";
import * as path from "path";
let Prism = require("prismjs");  // prism isn't typescript friendly just import it

// console.log(prism === undefined && "prism is undefined" || "prism is defined");

// console.log(process.argv.length);
console.log("Args passed:");
process.argv.forEach( arg => console.log(arg));
if (process.argv.length === 2) {
    console.log("Usage: np <path to input.html file> <path to output html file>");
}

// resolve input/output paths
let input = path.resolve(process.argv[3]);
let output = path.resolve(process.argv[4]);

// read input into buffer
let inBuff = fs.readFileSync(input);

// highlight file's content
let outBuff = Prism.highlight(inBuff.toString(), Prism.languages.javascript); //<== limited to only 1 language

Because Prism.highlight limits highlighting to a single language per call it makes using it for what I can only assume to be a common use case difficult. I can iteratively call Prism.highlight for each language needing highlighting, passing it the output from the previous call but in my opinion this use case would be better served having this implemented by Prism itself.

Also it would be advantageous if Prism supported a method that didn’t require the caller to pass it any language specifications at all, but rather would identify the languages dynamically as it parses the text input, such as what Prism.highlightAll does for DOM but for the text input instead.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Golmotecommented, Jun 24, 2016

The highlightAll method selects pre/code blocks in the DOM. The highlight method process the contents of those blocks, one by one. It’s meant to process a string of code, not HTML (well, except to highlight markup…).

Prism.highlight('var i = 4', Prism.languages.javascript)

You’ll have to make the replacement yourself. Maybe you should use the help of a templating library here.

0reactions
jeffschwartzcommented, Jun 24, 2016

Thank you @Golmote for the clarification. I looked through the source and now I understand what highlight is actually doing. Obviously it will not satisfy my use case so I am closing out this issue. Thanks again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prism
Download. Prism is a lightweight, extensible syntax highlighter, built with modern web standards in mind. It's used in millions of websites, including some ......
Read more >
Top 5 prismjs Code Examples - Snyk
Learn more about how to use prismjs, based on prismjs code examples created from the most popular ways it is used in public...
Read more >
Faster syntax highlighting using Prism - Davidsekar.com
Prism is a lightweight syntax highlighter that makes your code snippets look cool/beautiful in your blogs and websites.
Read more >
Highlight your code syntax with PrismJS - Publii
How to add Prism.js syntax highlighting to any website? The implementation of PrismJS is really very simple and does not require much effort ......
Read more >
Refreshing string attribute (loaded from database) within prism ...
I want to display a string, which I've loaded from my database, ... line-numbers"> <textarea #textArea class="text-area-code-editor" ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found