ng-realmark
Version:
Real-time Markdown W/ Markdown three way merge
71 lines • 3.7 kB
JavaScript
// Prism Syntax Highlighter
//
// Uses the Prism syntax highlighter to highlight code blocks.
import * as Prism from 'prismjs';
import 'prismjs/components/prism-markup-templating';
import 'prismjs/components/prism-bash';
import 'prismjs/components/prism-clike';
import 'prismjs/components/prism-c';
import 'prismjs/components/prism-diff';
import 'prismjs/components/prism-json';
import 'prismjs/components/prism-php';
export function showdownPrism() {
'use strict';
return {
type: 'html',
filter: function (text, converter, options) {
// find code blocks with a class
var regex = /<pre><code class="([a-zA-Z0-9]+) language\-(.*?)">([\s\S]*?)<\/code><\/pre>/g;
var html = text;
var results = text.match(regex);
var regex2 = new RegExp(/<pre><code class="([a-zA-Z0-9]+) language\-(.*?)">([\s\S]*?)<\/code><\/pre>/g, 'i');
if (results && results[0]) {
for (var i = 0; i < results.length; i++) {
var fresult = regex2.exec(results[i]);
// get the extracted language and code
if (fresult && fresult[3]) {
var language = fresult[1];
var code = fresult[3];
// lower case the language so case does not matter
language = language.toLowerCase();
// decode HTML entities encoded by showdown
// the opposite of replacements taken from showdown's _EncodeCode
code = code.replace(/</g, "<");
code = code.replace(/>/g, ">");
code = code.replace(/&/g, "&");
// make sure to decode ampersands last otherwise you will double decode < and >
// original : < makes the '<' symbol
// encoded : &lt; makes the '<' symbol
//
// Wrong:
// replace & : < makes the '<h;' symbol
// replace < : < makes the < symbol
//
// Correct:
// replace < : &lt; makes the '<' symbol
// replace & : < makes the '<' symbol
// highlight the code with prism
// get the grammar (language supported by prism)
var grammar = Prism.languages[language];
if (!grammar) {
// the given class name is not a language supported by prism
// skip to the next code block
console.log("NO PrismJS Language: ", language, Prism.languages);
continue;
}
// do the highlighting
var highlightedCode = Prism.highlight(code, grammar, language);
// create the new HTML with the highlighted code and language class
// Prism moves the language class from the <code> element to the <pre> element
// so we will set the class on the <pre> element
var newHTML = '<pre class="language-' + language + '"><code class="language-' + language + '">' + highlightedCode + '</code></pre>';
html = html.replace(fresult.input, newHTML);
}
}
}
return html;
}
};
}
;
//# sourceMappingURL=showdownPrism.js.map