Skip to content

Commit

Permalink
Custom class: Added mapper functions for language specific transforma…
Browse files Browse the repository at this point in the history
…tions (#1873)

This adds support for mapper function to the `map` function, allowing for language-specific transformations.
  • Loading branch information
RunDevelopment committed May 8, 2019
1 parent 632ce00 commit acceb3b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
20 changes: 11 additions & 9 deletions plugins/custom-class/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,20 @@ <h2>2. Replace some Prism classes with your defined ones via an object</h2>
keyword: 'special-keyword',
string: 'string_ch29s',
comment: 'comment_93jsa'
})</code></pre>
});</code></pre>

<p>Object's keys are the tokens you want to replace (eg: <code>comment</code>), with their values being the classes you want to use (eg: <code>my-comment</code>). Tokens which are not specified will stay the same.</p>

<p>Alternatively you can also pass a function that takes the original class and returns the mapped class. This function can also be used implement language specific mapped classes.<br>Example:</p>

<pre class="language-js"><code>Prism.plugins.customClass.map(function (className, language) {
if (language === 'css') {
return cssSpecificMap[className] || className;
} else {
return className;
}
});</code></pre>

<h1>Notes</h1>

<ul>
Expand Down Expand Up @@ -113,14 +123,6 @@ <h2>Output</h2>
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</section>

<section>
<h1>Todo</h1>
<ul>
<li>Take a function as the transformation (<a href="https://github.com/PrismJS/prism/pull/950#issuecomment-224289585">Comment in #950</a></li>
<li>Allow to custom tokens per language, using nested object (<a href="https://github.com/PrismJS/prism/issues/947#issuecomment-216979932">Comment in #947</a>)</li>
</ul>
</section>

<footer data-src="templates/footer.html" data-type="text/html"></footer>

<script src="prism.js"></script>
Expand Down
49 changes: 43 additions & 6 deletions plugins/custom-class/prism-custom-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,61 @@ if (
return;
}

/**
* @callback ClassMapper
* @param {string} className
* @param {string} language
* @returns {string}
*/
/**
* @typedef CustomClassOptions
* @property {ClassMapper} classMap
* @property {string} prefixString
*/

/** @type {ClassMapper} */
var defaultClassMap = function (className) { return className; };

/** @type {CustomClassOptions} */
var options = {
classMap: {}
classMap: defaultClassMap,
prefixString: ''
};

Prism.plugins.customClass = {
map: function map(cm) {
options.classMap = cm;
/**
* Maps all class names using the given object or map function.
*
* This does not affect the prefix.
*
* @param {Object<string, string> | ClassMapper} classMap
*/
map: function map(classMap) {
if (typeof classMap === 'function') {
options.classMap = classMap;
} else {
options.classMap = function (className) {
return classMap[className] || className;
};
}
},
/**
* Adds the given prefix to all class names.
*
* @param {string} string
*/
prefix: function prefix(string) {
options.prefixString = string;
}
}

Prism.hooks.add('wrap', function (env) {
if (!options.classMap && !options.prefixString) {
if (options.classMap === defaultClassMap && !options.prefixString) {
return;
}
env.classes = env.classes.map(function(c) {
return (options.prefixString || '') + (options.classMap[c] || c);

env.classes = env.classes.map(function (c) {
return options.prefixString + options.classMap(c, env.language);
});
});

Expand Down
2 changes: 1 addition & 1 deletion plugins/custom-class/prism-custom-class.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit acceb3b

Please sign in to comment.