forked from srikat/WP-HighlightJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
73 lines (60 loc) · 2.69 KB
/
Copy pathplugin.php
File metadata and controls
73 lines (60 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* Plugin Name: WP HighlightJS
* Plugin URI: https://github.com/srikat/WP-HighlightJS
* Description: Loads HighlightJS and clipboard.js on singular pages for syntax highlighting with a 1-click copy to clipboard button.
* Version: 2.0.6
* Author: Sridhar Katakam
* Author URI: https://sridharkatakam.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* GitHub Plugin URI: srikat/WP-HighlightJS
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License version 2, as published by the Free Software Foundation. You may NOT assume
* that you can use any other version of the GPL.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
add_action( 'wp_enqueue_scripts', 'wphjs_enqueue_wphighlightjs' );
/**
* Load HighlightJS and clipboard JS on single entries.
*/
function wphjs_enqueue_wphighlightjs() {
// if this is not a singular post of any post type (post, attachment, page, custom post types), abort.
if ( ! is_singular() ) {
return;
}
wp_enqueue_style( 'highlightjs-css', plugin_dir_url( __FILE__ ) . 'assets/css/style.css' );
// wp_enqueue_script( 'clipboard', plugin_dir_url( __FILE__ ) . 'assets/js/clipboard.min.js', '', '2.0.10', true );
wp_enqueue_script( 'highlightjs', plugin_dir_url( __FILE__ ) . 'assets/js/highlight.min.js', '', '11.0.3', true );
wp_enqueue_script( 'highlightjs-init', plugin_dir_url( __FILE__ ) . 'assets/js/highlight-init.js', '', '1.0.2', true );
// wp_add_inline_script( 'highlightjs-init', 'const pluginData = ' . json_encode( array(
// 'clippyUrl' => plugins_url( 'assets/images/clippy.svg', __FILE__ ),
// ) ), 'before' );
}
add_filter( 'block_editor_settings_all', 'wphjs_add_block_editor_styles' );
/**
* Inject the HighlightJS theme CSS into the block editor canvas (iframe) so
* code blocks preview with the same dark theme as the front-end.
*
* Only the CSS is loaded — the highlight.js script is intentionally NOT run in
* the editor, since the core/code block is a contenteditable element and adding
* token <span>s to it would break editing. This styles the code-block container
* (background, font, padding, base text color); full per-token colors remain a
* front-end enhancement.
*/
function wphjs_add_block_editor_styles( $settings ) {
$css_file = plugin_dir_path( __FILE__ ) . 'assets/css/style.css';
if ( is_readable( $css_file ) ) {
$settings['styles'][] = array(
'css' => file_get_contents( $css_file ),
);
}
return $settings;
}