From 68cf597792ac4ec9d73049ffbc19df79bbf33039 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Fri, 27 Sep 2019 21:49:52 +0200 Subject: [PATCH 1/5] Added class adder feature to Custom Class plugin --- plugins/custom-class/index.html | 77 ++++++++-- plugins/custom-class/prism-custom-class.js | 136 ++++++++++-------- .../custom-class/prism-custom-class.min.js | 2 +- 3 files changed, 146 insertions(+), 69 deletions(-) diff --git a/plugins/custom-class/index.html b/plugins/custom-class/index.html index 2b56f21d5f..748a8fea1b 100644 --- a/plugins/custom-class/index.html +++ b/plugins/custom-class/index.html @@ -18,7 +18,7 @@

Custom Class

-

This plugin allows you to prefix Prism default classes (.comment will become .namespace--comment) or replace them with your defined ones (like .editor__comment or .comment_7sh3a).

+

This plugin allows you to prefix Prism's default classes (.comment can become .namespace--comment) or replace them with your defined ones (like .editor__comment). You can even add new classes.

@@ -28,7 +28,7 @@

Motivation

+
-

Features

- -

This plugin currently provides 2 features:

+
+

How to use

-

1. Prefix all Prism classes with a string

+

Prefix all Prism classes

- Prism.plugins.customClass.prefix('prism--') +
Prism.plugins.customClass.prefix('prism--')
-

2. Replace some Prism classes with your defined ones via an object

+

Replace some Prism classes with ones you defined

Prism.plugins.customClass.map({
 	keyword: 'special-keyword',
@@ -54,7 +57,7 @@ 

2. Replace some Prism classes with your defined ones via an object

comment: 'comment_93jsa' });
-

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

+

Object's keys are the classes you want to replace (eg: comment), with their values being the classes you want to use (eg: my-comment). Classes which are not specified will stay as they are.

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.
Example:

@@ -66,6 +69,21 @@

2. Replace some Prism classes with your defined ones via an object

} }); +

Add new classes

+ +

You can add new classes with per-token and per-language precision.

+ +
Prism.plugins.customClass.add(function (language, type, content) {
+	if (language === 'css' && type === 'property' && content === 'content') {
+		return 'content-property';
+	}
+});
+ +

Note: The given content is the inner HTML of the current token. All < and & characters are escaped and it might contain the HTML code of nested tokens.

+ +
+ +

Notes