-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom Class: New class adder feature (#2075)
This adds a new `add` function to the Custom Class plugin which gives users the ability to easily add their own specific classes for existing tokens.
- Loading branch information
1 parent
baa7877
commit dab7998
Showing
5 changed files
with
150 additions
and
70 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,95 @@ | ||
(function(){ | ||
(function () { | ||
|
||
if ( | ||
(typeof self === 'undefined' || !self.Prism) && | ||
(typeof global === 'undefined' || !global.Prism) | ||
) { | ||
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: defaultClassMap, | ||
prefixString: '' | ||
}; | ||
if ( | ||
(typeof self === 'undefined' || !self.Prism) && | ||
(typeof global === 'undefined' || !global.Prism) | ||
) { | ||
return; | ||
} | ||
|
||
Prism.plugins.customClass = { | ||
/** | ||
* Maps all class names using the given object or map function. | ||
* @callback ClassMapper | ||
* @param {string} className | ||
* @param {string} language | ||
* @returns {string} | ||
* | ||
* This does not affect the prefix. | ||
* @callback ClassAdder | ||
* @param {ClassAdderEnvironment} env | ||
* @returns {undefined | string | string[]} | ||
* | ||
* @param {Object<string, string> | ClassMapper} classMap | ||
* @typedef ClassAdderEnvironment | ||
* @property {string} language | ||
* @property {string} type | ||
* @property {string} content | ||
*/ | ||
map: function map(classMap) { | ||
if (typeof classMap === 'function') { | ||
options.classMap = classMap; | ||
} else { | ||
options.classMap = function (className) { | ||
return classMap[className] || className; | ||
}; | ||
|
||
// options | ||
|
||
/** @type {ClassAdder | undefined} */ | ||
var adder; | ||
/** @type {ClassMapper | undefined} */ | ||
var mapper; | ||
/** @type {string} */ | ||
var prefixString = ''; | ||
|
||
|
||
Prism.plugins.customClass = { | ||
/** | ||
* Sets the function which can be used to add custom aliases to any token. | ||
* | ||
* @param {ClassAdder} classAdder | ||
*/ | ||
add: function (classAdder) { | ||
adder = classAdder; | ||
}, | ||
/** | ||
* Maps all class names using the given object or map function. | ||
* | ||
* This does not affect the prefix. | ||
* | ||
* @param {Object<string, string> | ClassMapper} classMapper | ||
*/ | ||
map: function map(classMapper) { | ||
if (typeof classMapper === 'function') { | ||
mapper = classMapper; | ||
} else { | ||
mapper = function (className) { | ||
return classMapper[className] || className; | ||
}; | ||
} | ||
}, | ||
/** | ||
* Adds the given prefix to all class names. | ||
* | ||
* @param {string} string | ||
*/ | ||
prefix: function prefix(string) { | ||
prefixString = string || ''; | ||
} | ||
}, | ||
/** | ||
* 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 === defaultClassMap && !options.prefixString) { | ||
return; | ||
} | ||
Prism.hooks.add('wrap', function (env) { | ||
if (adder) { | ||
var result = adder({ | ||
content: env.content, | ||
type: env.type, | ||
language: env.language | ||
}); | ||
|
||
if (Array.isArray(result)) { | ||
env.classes.push.apply(env.classes, result); | ||
} else if (result) { | ||
env.classes.push(result); | ||
} | ||
} | ||
|
||
if (!mapper && !prefixString) { | ||
return; | ||
} | ||
|
||
env.classes = env.classes.map(function (c) { | ||
return options.prefixString + options.classMap(c, env.language); | ||
env.classes = env.classes.map(function (c) { | ||
return prefixString + mapper ? mapper(c, env.language) : c; | ||
}); | ||
}); | ||
}); | ||
|
||
})(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.