UNPKG

9.16 kBMarkdownView Raw
1# HighlightJs Copy Code Badge Component
2This small JavaScript library complements the [highlightJs Syntax Highligher](https://highlightjs.org/) by providing a badge in the top right corner of highlightJs code snippets.
3
4* Shows active Syntax for the code block
5* Allows copying the code block to Clipboard
6
7You can install it [from NPM](https://www.npmjs.com/package/highlightjs-badge):
8
9```ps
10npm install highlightjs-badge
11```
12
13
14* [Codepen Example](https://codepen.io/rstrahl/pen/RwNZGBE)
15* [HighlightJs-Badge Blog Post](https://weblog.west-wind.com/posts/2019/Dec/30/A-HighlightJs-Copy-Code-Badge-Component)
16
17Here's what the code badge looks like attached to several highlightjs code blocks:
18
19![](ScreenShot.png)
20
21This small, single JavaScript file component can be loaded after highlightJS has been loaded. It's fully self-contained. Add the script, call the `window.highlightJsBadge()`, and you're up and running without any other configuration or dependencies.
22
23### Usage
24To use this library is very simple - you add a script file and call `highlightJsBadge()` after highlightJS has been applied.
25
26The following is a typical configuration for both highlightJs and highlightJs-Badge:
27
28```html
29<!-- load highlightjs first -->
30<link href="scripts/highlightjs/styles/vs2015.css" rel="stylesheet" />
31<script src="scripts/highlightjs/highlight.pack.js"></script>
32
33<!-- then add this badge component -->
34<script src="scripts/highlightjs-badge.min.js"></script>
35
36<script>
37 // apply HighlightJS
38 var pres = document.querySelectorAll("pre>code");
39 for (var i = 0; i < pres.length; i++) {
40 hljs.highlightBlock(pres[i]);
41 }
42
43 // add HighlightJS-badge (options are optional)
44 var options = { // optional
45 contentSelector: "#ArticleBody",
46
47 // CSS class(es) used to render the copy icon.
48 copyIconClass: "fas fa-copy",
49 // CSS class(es) used to render the done icon.
50 checkIconClass: "fas fa-check text-success"
51 };
52 window.highlightJsBadge(options);
53</script>
54```
55### Options Available
56The following options are available to pass into the function, which control the behavior of the badge generation.
57
58```js
59var options = {
60 // the selector for the badge template
61 templateSelector: "#CodeBadgeTemplate",
62
63 // base content CSS selector that is searched for snippets
64 contentSelector: "body",
65
66 // Delay in ms used for `setTimeout` before badging is applied
67 // Use if you need to time highlighting and badge application
68 // since the badges need to be applied afterwards.
69 // 0 - direct execution (ie. you handle timing
70 loadDelay: 0,
71
72 // CSS class(es) used to render the copy icon.
73 copyIconClass: "fa fa-copy",
74 // optional content for icons class (<i class="fa fa-copy"></i> or <i class="material-icons">file_copy</i>)
75 copyIconContent: "",
76
77 // CSS class(es) used to render the done icon.
78 checkIconClass: "fa fa-check text-success",
79 checkIconContent: "",
80
81 // function called before code is placed on clipboard that allows you inspect and modify
82 // the text that goes onto the clipboard. Passes text and code root element (hljs).
83 // Example: function(text, codeElement) { return text + " $$$"; }
84 onBeforeCodeCopied: null
85};
86```
87
88### Styling
89The default script includes default styling that should work great with dark themed syntax, and fairly well with light themed syntax.
90
91You can customize the styling and the layout of badge by either overriding existing styles or by:
92
93* Overriding styles
94* Copying complete styles and template into page
95
96#### Overriding styles
97The easiest way to modify behavior is to override individual styles. The stock script includes a hardcoded style sheet and you can override the existing values with hard CSS overrides.
98
99For example to override the background and icon sizing you can:
100
101```css
102<style>
103 .code-badge {
104 padding: 8px !important;
105 background: pink !important;
106 }
107 .code-badge-copy-icon {
108 font-size: 1.3em !important;
109 }
110</style>
111```
112
113#### Replace the Template and Styling Completely
114Alternately you can completely replace the template and styling. If you look at the source file at the end of the file is a commented section that contains the complete template and you can copy and paste that template into your HTML page - at the bottom near the `</body>` tag.
115
116```html
117<style>
118 "@media print {
119 .code-badge { display: none; }
120 }
121 .code-badge-pre {
122 position: relative;
123 }
124 .code-badge {
125 display: flex;
126 flex-direction: row;
127 white-space: normal;
128 background: transparent;
129 background: #333;
130 color: white;
131 font-size: 0.8em;
132 opacity: 0.5;
133 border-radius: 0 0 0 7px;
134 padding: 5px 8px 5px 8px;
135 position: absolute;
136 right: 0;
137 top: 0;
138 }
139 .code-badge.active {
140 opacity: 0.8;
141 }
142 .code-badge:hover {
143 opacity: .95;
144 }
145 .code-badge a,
146 .code-badge a:hover {
147 text-decoration: none;
148 }
149
150 .code-badge-language {
151 margin-right: 10px;
152 font-weight: 600;
153 color: goldenrod;
154 }
155 .code-badge-copy-icon {
156 font-size: 1.2em;
157 cursor: pointer;
158 padding: 0 7px;
159 margin-top:2;
160 }
161 .fa.text-success:{ color: limegreen !important}
162</style>
163<div id="CodeBadgeTemplate" style="display:none">
164 <div class="code-badge">
165 <div class="code-badge-language">{{language}}</div>
166 <div title="Copy to clipboard">
167 <i class="{{copyIconClass}} code-badge-copy-icon"></i>
168 </div>
169 </div>
170</div>
171```
172
173This is the same template that the library internally holds and injects into the page, but if `#CodeBadgeTemplate` exists in the document then that is used instead of the embedded template. When using your own template no styling is applied. so you neeed to include both the CSS and the `CodeBadgeTemplate`.
174
175You can optionally separate out the CSS into a separate file and only include the `#CodeBadgeTemplate` `<div>` element - that's sufficient for your custom template and styling to kick in.
176
177### Requirements
178This library is a self-contained JavaScript file so there are no direct external dependencies. However, there are a couple of requirements:
179
180#### Some ES6 Usage
181The library uses a couple of ES6 functions, so if you plan on using this with Internet Explorer you'll need to add these polyfills:
182
183* [Object.assign](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill)
184* [String.trim](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim#Polyfill)
185
186#### FontAwesome by Default, Custom Icon Overrides
187The library by default also uses [FontAwesome icons](https://fontawesome.com/icons?from=io) for the copy and check icons. The icon styles used work with Font Awesome 4 and 5 (both free and pro).
188
189The default rendering uses:
190
191```html
192<i class="fa fa-copy"></i>
193```
194The icon is changed to `fa-check` check after content's been copied for 2 seconds.
195
196If you want to use different icons you can use separate styling for the icon classes. For example to change the icons used you affect the `class` attribute on the icon setting.
197
198For example to use Material Icons:
199
200```javascript
201var options = {
202 // CSS class(es) used to render the copy icon.
203 copyIconClass: "material-icons",
204 copyIconContent: "copy_files",
205
206 // CSS class(es) used to render the done icon.
207 checkIconClass: "material-icons",
208 checkIconContent: "check"
209};
210window.highlightJsBadge(options);
211```
212
213### License
214Licensed under the MIT License. There's no charge to use, integrate or modify the code for this project. You are free to use it in personal, commercial, government and any other type of application.
215
216
217### Version History
218
219### v0.1.7
220
221* **Add onBeforeCopyCode hook**
222You can provide an `options.onBeforeCopyCode` function to intercept the pasted text and fix up the text before it is pasted into the clipboard.
223
224### v0.1.6
225
226* **Fix up `lang-javascript` Syntax Names**
227Fix up `lang-csharp` style syntax tags generated by various tools like DocFx and properly display the syntax. Same fixup is also applied to `language-css` style syntax.
228
229### v0.1.5
230
231* **Turn off code badge in Print Media Style**
232Added media class so that the code-badge is not shown when printing is active for print or PDF generation.
233
234* **Fix Internet Explorer missing Line Feeds**
235Fixed issue where IE 11/10 was not properly picking up line breaks in the copied text content. Still useful for those of us using the Web Browser control in Windows. Fixed by using `textContent` instead of `innerText`.
236
237* **Badge Position on Scrolled Content**
238Fixed issue where the badge overlay would not properly stay right aligned when a code block was scrolled horizontally. Fixed by moving `position:relative` up to the `<pre>` tag **via code**. Unfortunately this style feature is not directly settable via CSS so the relative style gets hardcoded when the badge is added to the page.
239
240