Feature Request: Prism.highlightAll(text: string): string
See original GitHub issueI’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:
- Created 7 years ago
- Comments:5 (2 by maintainers)
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.
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.